echo '' ;
C Programming Language

C – Operators

Arithmetic +, -, *, /, %
Assignment=
Augmented Assignment+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
Bitwise Logic~, &, |, ^
Bitwise Shifts<<, >>
Boolean Logic!, &&, ||
Conditional Evaluation: ? :
Equality Testing==, !=
calling functions( ) (function) -Every function has a pair of parentheses ()

 
main() – main is a function. Every function has a pair of parentheses (). Can a program be compiled without main() function?: Yes, it can be but cannot be executed, as the execution requires main() function definition.
increment and decrement++, —
member selection., ->
object sizesizeof
Order relations<, <=, >, >=
reference and dereference& (Address of Operator) – It cannot be used on constants. It cannot be used on a variable that is declared using the register storage class.

, *, [ ]
sequencing,
subexpression grouping( )
type conversion(typename)
Semicolon; (Semicolon) Any C statement always ends with a ;

Arithmetic OperatorsUnary Operators

Binary Operators
Unary Operators – Eg: +5 , -8 .

Binary Operators
X = 5+6; (Addition + Operator)
X = 20-10; (Subraction – Operator
X = 5*3; (Multiplication * Operator)
X = 5/3; (Division / Operator)
X = 5%3; (Modular % Operator) -remainder
Relational OperatorsA>B; (Greater than)
A<B; (lesser than)
A>=B; (Greater than equal to)
A<=B; (Lesser than equal to)
A==B; (equal to)
A!=B; (not equal to)
Logical OperatorA && B; (AND)
A || B; (OR)
!A=A; (NOT)
Increment and Decrements OperatorsIncrement Operator
++x; (pre increments)
x++; (post increments)

Decrements Operator
–x; (pre Decrements)
x–; (post Decrements)
S++; // ++, as it is single machine instruction (INC) internally.(recommended)
S = S+1; // ++, as it wil take two machine cycle internally.(not recommended)
Short hand assignment Operators
conditional operators (Ternary Operator)

C program structure model explanation

main() -functionmain() – main is a function. Every function has a pair of parentheses ()
Can a program be compiled without main() function?
Yes, it can be but cannot be executed, as the execution requires main() function definition.
{} scope or block
A local block is any portion of a C program that is enclosed by the left brace { and the right brace } .
anything between the two braces is contained in a local block.
() function() – function -Every function has a pair of parentheses ()
semicolon : ; Any C statement always ends with a ;
, Comma OperatorComma Operator – can be used to separate two or more expressions
Eg: printf(“hi”) , printf(“Hello”);
& Address of Operator Address of Operator – It cannot be used on constants. It cannot be used on a variable that is declared using the register storage class.

Incremental and Decremental Operators

  • S++; // ++, as it is single machine instruction (INC) internally.(recommended)
  • S = S+1; // ++, as it wil take two machine cycle internally.(not recommended)
Increment OperatorDecrements Operator
++x (pre increments)–x (pre Decrements)
x++ (post increments)x– (post Decrements)

Example:

Code:

#include<stdio.h>
int main()
{
	int a=5, b=6;
	printf("A : %d \n", a);
	printf("B : %d \n\n", b);
	int c = a++ + ++b;
	printf("A : %d \n", a);
	printf("B : %d \n\n", b);

	printf("C : %d \n", c);
	printf("A : %d \n", a);
	printf("C : %d \n", c);
	return 0;
}

Output:

A : 5
B : 6
A : 6
B : 7
C : 12
A : 6
B : 7


Relational Operator

Code-1:

#include <stdio.h>

int main()
{
	int y;
	int x = y ==10; //int x = y == y; or int x = y ;  
	printf("%d",x);
	
	return 0;
}


//Output : 0

Code-2:

#include<stdlib.h>
 
int main()
{
	int x =10;
	x ==10;
	printf("%d",x);
      return 0;
}
//Output : 1

Boolean Operator

  • == Equal
  • != Not equal
  • > Greater than
  • >= Greater than or equal
  • < Less than
  • <= Less than or equal
  • && Logical AND
  • || Logical OR
  • ! Logical NOT

Conditional operators (Ternary Operator)

syntax : expression 1 ? expression 2 : expression 3;

  • Rule
    • if expression 1 is true then expression 2 is executed
    • else expression 1 is false then expression 3 is executed
  • Advantage : Using ?: reduce the number of line codes and improve the performance of application.
  • Example Invalid:In this below example this is an error in this line i>45? return(*P): return (*q); We cannot use return keyword in the terenary operators.
    #include<stdio.h>
    int check (int, int);
    
    int main()
    {
        int c;
        c = check(10, 20);
        printf("c=%d\n", c);
        return 0;
    }
    int check(int i, int j)
    {
        int *p, *q;
        p=&i;
        q=&j;
        i>=45 ? return(*p): return(*q);
    }

  • Example Valid : In this below example a is lesser than b . so the b value
    a= 5;
    b=7;
    a>b?a:b;
  • More examples :
    • Find largest number among 3 numbers using ternary operator

Associativity

Associativity is only needed when the operators in an expression have the same precedence. Usually + and – have the same precedence.

Consider the expression 7 – 4 + 2. The result could be either (7 – 4) + 2 = 5 or 7 – (4 + 2) = 1. The former result corresponds to the case when + and – are left-associative, the latter to when + and – are right-associative.

Usually, the addition, subtraction, multiplication, and division operators are left-associative, while the exponentiation, assignment, and conditional operators are right-associative. To prevent cases where operands would be associated with two operators or no operator at all, operators with the same precedence must have the same associativity.

BIT Operations

Bit OperationsOperator usageExample
Set bitUse the bitwise OR operator (|)aew_value = aew_value | 1<< set_bit
Clear BitUse the bitwise AND operator (&) and NOT operator (~).aew_value = aew_value & (~(1<< set_bit)
Toggle BitUse the XOR operator (^)Use the bitwise AND operator (&) and NOT operator (~).

How do find the status of a particular bit is SET or NOT?

#include <stdio.h>

int main(void)
{
  //How to find the status of a particular bit is SET or NOT?
    int value = 5;
    int n = 2;
    if (value & (1 << (n - 1)))
        printf("SET");
    else
        printf("NOT SET");

    return 0;
}

Print the Multiplication of the given N number?

#include <stdio.h>
#define N 4
int main()
{
    
    printf("Method-1: 1*%d = %d\n",N,(N));
    printf("Method-1: 2*%d = %d\n",N,(N<<1));
    printf("Method-1: 3*%d = %d\n",N,(N<<1)+N);
    printf("Method-2: 3*%d = %d\n",N,(N<<2)-N);
    printf("Method-1: 4*%d = %d\n",N,(N<<2));
    printf("Method-1: 5*%d = %d\n",N,(N<<2)+N);
    printf("Method-1: 8*%d = %d\n",N,(N<<3));
    printf("Method-1: 9*%d = %d\n",N,(N<<3)+N);
    printf("Method-1: 10*%d = %d\n",N,(N<<3)+(N<<1));
    printf("Method-1: 15*%d = %d\n",N,(N<<4)-N);
    printf("Method-1: 16*%d = %d\n",N,(N<<4));
    printf("Method-1: 40*%d = %d\n",N,(N<<5)+(N<<3));
    return 0;
}

OutPut

Method-1: 1*4 = 4
Method-1: 2*4 = 8
Method-1: 3*4 = 12
Method-2: 3*4 = 12
Method-1: 4*4 = 16
Method-1: 5*4 = 20
Method-1: 8*4 = 32
Method-1: 9*4 = 36
Method-1: 10*4 = 40
Method-1: 15*4 = 60
Method-1: 16*4 = 64
Method-1: 40*4 = 160


...Program finished with exit code 0
Press ENTER to exit console.

NEXT

C - Basic
C - Operator
C - Decision making, Branching, Looping
C - Functions
C - Storage Class
C - Extern
C - Array
C - Pointer
C - Memory Allocation
C - Structure
C - Union
C - Structure and Union
C - Macros
C – Preprocessor and Macros
C - File Management
C - Coding Standard
C - Compilers
C - Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading