Arithmetic operators are used to perform arithmetic operations on variables and data.
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo Operation (Remainder after division) |
++ | Increment |
-- | Decrement |
Assignment operators are used to assign values to variables.
Operator | Operation | Equivalent to |
---|---|---|
= | a=b | a=b; |
+= | a+=b | a=a+b |
-= | a-=b | a=a-b |
*= | a*=b | a=a*b |
/= | a/=b | a=a/b |
%= | a%=b | a=a%b |
A relational operator is used to check the relationship between two operands.
Operator | Meaning |
---|---|
< | Less Than |
> | Greater Than |
<= | Greater Than or Equal To |
>= | Less Than or Equal To |
== | is Equal To |
!= | Not Equal To |
Logical operators are used to check whether an expression is true or false.
Operator | Meaning |
---|---|
&& | Logical AND. True only if all the operands are true. |
|| | Logical OR. True if at least one of the operands is true. |
! | Logical NOT. True only if the operand is false. |
If an expression contains different operators then they are evaluated according to their precedence.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | a++ a-- |
Suffix/postfix increment Suffix/postfix decrement |
Left to Right |
2 | ++a --a |
Prefix increment Prefix decrement |
Right to Left |
3 | a * b a / b a % b |
Multiplication Division Modulus |
Left to Right |
4 | a + b a - b |
Addition Subtraction |
Left to Right |
5 | < <= > >= |
Less than Less than or equal to Greater than Greater than or equal to |
Left to Right |
6 | == != |
Equal to Not equal to |
Left to Right |
7 | && | Logical AND | Left to Right |
8 | || | Logical OR | Left to Right |
#include⁢stdio.h> main() { int a,b,c; printf("Enter 2 numbers "); scanf("%d %d",&a,&b); c=a+b; printf("Sum is %d",c); }
Enter 2 numbers 45 55 Sum is 100
#include⁢stdio.h> main() { int a,b,c,d,e,t; printf("Enter marks for 5 subjects "); scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); t=a+b+c+d+e; printf("Total is %d",t); }
Enter marks for 5 subjects 50 60 55 60 65 Total is 290
#include⁢stdio.h> main() { float L, B, A, P ; printf("Enter Length and Breadth"); scanf("%f %f",&L,&B); A=L*B; P=2*(L+B); printf("Area is %f",A); printf("\nPerimeter is %f",P); }
Enter Length and Breadth 5.2 3.1 Area is 16.12 Perimeter is 16.6
#include⁢stdio.h> main() { float r, v ; printf("Enter Radius "); scanf( "%f" , &r ); v = 4 / 3 * 3.14 * r * r * r; printf("Volume of Sphere is %f",v); }
Enter Radius 2.5 Volume of Sphere is 65.416666
#include⁢stdio.h> main() { int a , b; int temp; printf("Enter 2 Numbers "); scanf( "%d %dā , &a , &b ); printf("\nValue of a=%d b=%d",a,b); temp = a; a = b; b = temp; printf("\nValue of a=%d b=%d",a,b); }
Enter 2 Numbers 5 7 Value of a = 5 b = 7 Value of a = 7 b = 5
#include⁢stdio.h> main() { int a; int x; printf("Enter a Number "); scanf("%d",&a); x = a%10 ; printf("Last digit is %d",x); }
Enter a Number 28 Last digits is 8