C Operators
Below is the table of all operators in c
| Section | Operator |
|---|---|
| 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 size | sizeof |
| 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 ; |
| C Operators | Type | Operator |
|---|---|---|
| Arithmetic Operators | Unary 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 Operators | A>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 Operator | A && B; (AND) A || B; (OR) !A=A; (NOT) | |
| Increment and Decrements Operators | Increment 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 for C Operators
- Please refer the hello world example, below table explain the operator how its used.
| Operator | Explanation |
|---|---|
| main() -function | 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. |
| {} 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 Operator | Comma 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++; Here ++, as it is single machine instruction (INC) internally.(recommended)
- S = S+1; Here +, as it will take two machine cycle internally.(not recommended)
| Increment Operator | Decrements Operator |
|---|---|
| ++x (pre increments) | –x (pre Decrements) |
| x++ (post increments) | x– (post Decrements) |
Below is the code of incremental and decremental operators
#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: Output is Zero
#include <stdio.h>
int main()
{
int y;
int x = y ==10; //int x = y == y; or int x = y ;
printf("%d",x);
return 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 false, the program executes expression 3; otherwise, it executes expression 2. if expression 1 is true then exp
- 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.
<pre class="lang:c decode:true">
#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;<br>b=7;<br>a>b?a:b;
- More examples :
- Find largest number among 3 numbers using ternary operator
Associativity
We use associativity only for operators of 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 two operators share an operand or an operand lacks an operator, we assign the same associativity to all operators with the same precedence.
BIT Operations
| Bit Operations | Operator usage | Example |
|---|---|---|
| Set bit | Use the bitwise OR operator (|) | aew_value = aew_value | 1<< set_bit |
| Clear Bit | Use the bitwise AND operator (&) and NOT operator (~). | aew_value = aew_value & (~(1<< set_bit) |
| Toggle Bit | Use the XOR operator (^) | Use the bitwise AND operator (&) and NOT operator (~). |
Is bit set?
#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.
Previous & Next
- Previous: C Compilers
- Next: C Decision Making Branching Looping