C Language | Operators | Start Your Journey with C | Part 3
![]() |
1. Arithmetic operators :
These are used to perform some arithmetic or mathematical operations on operands.
The following table shows all the basic arithmetic or mathematical operators used in c programming :
Operator | Description |
---|---|
+ | adds two operands |
- | subtract second operands from first |
* | multiply two operand |
/ | divide numerator by denominator |
% | remainder of division |
++ | Increment operator - increases integer value by one |
Here is a basic C program to understand how this operators works :
#include<stdio.h>
int main()
{
int a = 5, b = 4, result;
printf("a is %d and b is %d\n", a, b);
result = a + b; // addition
printf("a+b is %d\n", result);
result = a - b; // subtraction
printf("a-b is %d\n", result);
result = a * b; // multiplication
printf("a*b is %d\n", result);
result = a / b; // division
printf("a/b is %d\n", result);
result = a % b; // modulus
printf("a%b is %d\n", result);
return 0;
}
2. Assignment operators :
Assignment operators are used to assign a value to a variable. It follows a simple syntax, in the left-side of the assignment operator, a variable is placed and in the right-side of the assignment operator, a value is placed which will be stored in the left-side variable.
Both the value and the variable must of same data-type, else it will cause an error.
These are all assignment operators supported by C language :
Operator | Description | Example |
---|---|---|
= | assigns values from right side operands to left side operand | a=b |
+= | adds right operand to the left operand and assign the result to left | a+=b is same as a=a+b |
-= | subtracts right operand from the left operand and assign the result to left operand | a-=b is same as a=a-b |
*= | multiply left operand with the right operand and assign the result to left operand | a*=b is same as a=a*b |
/= | divides left operand with the right operand and assign the result to left operand | a/=b is same as a=a/b |
%= | calculate modulus using two operands and assign the result to left operand | a%=b is same as a=a%b |
3. Increment and Decrement operators :
- Increment operators :
The ‘++’ operator is used to increment the value of an integer. When placed before a variable (pre-increment), its value is incremented instantly. For example, ++x.
And when it is placed after a variable (post-increment), its value is temporarily stored until the execution of this statement and it gets updated before the execution of the next statement. For example, x++.
- Decrement operators :
The ‘ – – ‘ operator is used to decrement the value of an integer. When placed before a variable (pre-decrement), its value is decremented instantly. For example, – – x.
And when it is placed after a variable (post-decrement), its value is stored temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x – –.
Here is a basic C program to understand how this operators works :
#include<stdio.h>
int main()
{
int a = 5, b = 4, result;
result = a++; // post-increment
printf("a is %d and result is %d\n", a, result);
result = ++a; // pre-increment
printf("a is %d and result is %d\n", a, result);
result = a--; // post-decrement
printf("a is %d and result is %d\n", a, result);
result = --a;// pre-decrement
printf("a is %d and result is %d\n", a, result);
return 0;
}
4. Relational operators :
Operator | Type |
< | less. than |
<= | less than or equal to |
= = | equal to |
! = | Not equal to |
> | Greater than |
>= | Greater than or equal to |
Basic syntax with example :
#include<stdio.h>
main()
{
int a,b;
printf ("Enter values for a and b\n") ;
scanf("%d%d",&a,&b) ;
if(a<b)
printf ("%d is less than %d\n", a, b) ;
if(a<=b)
printf ("%d is less than or equal to %d\n", a,b) ;
if(a==b)
printf("%d is equal to %d\n", a,b);
if(a!=b)
printf("%d is not equal to %d\n", a,b);
if(a>b)
printf("%d is greater than %d\n", a,b);
if (a>=b)
printf("%d is greater than or equal to %d\n",a,b);
}
/* in case (a=5) , these type of operator call assignment function.
5. Logical operators :
Logical operators are used to combine two or more conditions and returns either True or False ( 0 or 1 ). There are three logical operators, they are as follows :
- AND (&&) Operator :
The '&&' operator returns True or 1 only when both two or more then two conditions combined together get satisfied, else it returns False or 0 .
- OR ( || ) Operator :
The ' || ' operator returns True or 1 when at least one of the all conditions get satisfied, and return False or 0 only when all the conditions combined does not get satisfied.
- NOT ( ! ) Operator :
The ' ! ' operator returns True or 1 when a condition does not get satisfied and return False or 0 when the condition get satisfied.
Here is a C program to understand how these logical operators works :
#include<stdio.h>
int main()
{
int a=5, b=2, c = 5, d = 10;
// logical AND example
if (a>b && c==d)
printf("a is greater than b AND c is equal to d\n");
else
printf("AND condition not satisfied\n");
// logical AND example
if (a>b || c==d)
printf("a is greater than b OR c is equal to d\n");
else
printf("Neither a is greater than b nor c is equal to d\n");
// logical NOT example
if (!a)
printf("a is zero\n");
else
printf("a is not zero");
return 0;
}
6. Conditional operator :
Concept :
(Condition) ? True expression : False expressionHere, when the condition gets satisfied True expression is executed and when it does not get satisfied, the False expression is executed.
Basic Syntax:
#include<stdio.h>
void main()
{
int a=6, b=5, c=0;
( c = a > b ? a : b );
printf("%d", c);
}
7. Comma (,) operator :
Comma operator as a separator :
int a,b,c;
Comma operator in scanf and printf :
scanf("%d%d%d",&a,&b,&c);
printf("%d%d%d",a,b,c);
8. sizeof operator :
Sizeof is an unary operator. This operator gives the size of its operand in terms of bytes. The operator can be a variable or constant of any data-type (int, float, char etc)
#include<stdio.h>
main( )
{
int var;
printf("Size of int %d" ,sizeof(int) ) ;
printf("Size of float %d",sizeof(float));
printf("size of var = %d" , sizeof (var)) ;
printf("Size of an integer constant = %d",sizeof(45));
}
Size of int = 2
Size of float = 4
Size of var = 2
Size of an integer constant = 2
9. Bitwise operators:
C language has the ability to support the manipulation of data at the bit level. Bitwise operators are used for operations on individual bits. Bitwise operators operate on integers only.Bitwise operators | |
& | bitwise AND |
| | bitwise OR |
~ | one's -complement |
<< | left shift |
>> | right shift |
^ | bitwise XOR |
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)