Operators in C
Operators in C
Arithmetic Operators:
The binary arithmetic operators are +, -, *, /, and the modulus operator %. The % operator cannot be applied to a float or double.
Operator Meaning example
+ Addition or unary Plus x + y
- Subtraction or unary minus x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
#include <stdio.h>
int main() {
int x, y, z;
x = 20, y = 5;
printf("%d %d %d %d %d \n", x+y, x-y, x*y, x/y, x%y);
return 0;
}
Relational Operators:
Relation operators check the relation between two operands. If the relation is true, it returns 1 or it returns 0. The relational operators are >, >= , <, <=, == ,!=
Operator Meaning example
> Greater than x > y
>= Greater than or equal to x >= y
< Less Than x < y
<= Less Than or equal to x <= y
== Equal to x == y
!= Not equal to x != y
#include <stdio.h>
int main() {
int x, y, z;
x = 20, y = 5;
printf("%d %d %d %d \n", (x>y), (x<y), (x==y), (x!=y));
return 0;
}
Increment and Decrement Operators:
Operators in C, The increment and decrement operators are ++, —
Operator Meaning example
++ Increment x++ or ++x
-- Decrement x-- or --x
x++, x– are post-increment/decrement, first value assign to x, later increment or decrement.
++x, –x are pre-increment/decrement, first increment or decrement the value, later value assign to x.
#include<stdio.h>
int main() {
int x = 5;
printf("%d ", x++);
printf("%d ", ++x);
printf("%d ", x--);
printf("%d \n", --x);
return 0;
}
Bitwise Operators:
operates individual bits of a given operand. operands should be of type (char, short, long, int)
Operator Meaning example
& Bitwise AND x & y
| Bitwise OR x | y
^ Bitwise exclusive OR x ^ y
<< Left Shift x << y
>> Right shift x >> y
~ one's complement ~x
AND: If both operand bits are 1’s the corresponding bit result is 1, otherwise, the result is 0.
OR: If anyone operand bit is 1’s the corresponding bit is 1, otherwise, the result is 0.
exclusive OR: If both bits are 1’s or 0’s the result is 0, otherwise, the result is 1.
Left shift: Shifts the bits to the given operand value to the right and assign the vacate bits with 0, can use in the place of ‘/’ operator if the right operand of ‘/’ can be represented to 2 power n format, shift left operand of ‘/’ operator by n times.
Right shift: Shifts the bits to the given operand value to the left and assign the vacate bits with 0, can use in the place of ‘*’ operator if any operand ‘*’ can be represented to 2 power n format, shift the operand of ‘*’ operator by n times.
one’s complement (unary): Invert all of the bits of the operand 0 to 1 and 1 to 0.
#include<stdio.h>
int main() {
int x = 2, y=10, z=-6;
printf("%d %d %d %d %d %d\n",x&y,x|y,x<<2, y>>1,x^y,~z);
return 0;
}
Assignment Operators and Expressions:
Assignment operators use to assigning value to a variable. An expression such as
i = i + 2
In which the variable on the left side repeat immediately on the right, can be written in the compressed form
i += 2
The operator += is calling an assignment operator. Most binary operators (operators like + that have a left and right operand) have a corresponding assignment operator op=, where op is one of + – * / % << >> & ^ |
i *= 2;
#include<stdio.h>
int main() {
int x = 2;
x += 2;
printf("%d ", x);
x -= 2;
printf("%d ", x);
x *= 2;
printf("%d ", x);
x /= 2;
printf("%d ", x);
x %= 2;
printf("%d ", x);
x &= 2;
printf("%d ", x);
x ^= 2;
printf("%d ", x);
x |= 2;
printf("%d ", x);
x <<= 2;
printf("%d ", x);
x >>= 2;
printf("%d ", x);
return 0;
}
Logical Operator:
An expression containing a logical operator returns either 0 or 1 depending upon whether the expression results in true or false. The logical operators are && (logical AND), || (logical OR), ! (logical NOT).
#include<stdio.h>
int main() {
int a = 5, b =2, c = 10;
if ((a>b) && (a < c)) {
printf("Logical AND\n");
}
if ((a >c) || (b<a)) {
printf("Logical OR\n");
}
if(!(a == b)) {
printf("Logical NOT\n");
}
return 0;
}
Ternary operator:
The conditional expression, written with the ternary operator “?:”
#include<stdio.h>
int main() {
int a = 10, b=5;
printf("%d \n", (a>b)?a:b);
return 0;
}
Typecast operator:
This operator uses to convert from one type to another type.
#include<stdio.h>
int main() {
int a = 50, b = 5;
float c;
c = (float)(a/b);
printf("%f \n",c);
return 0;
}
Comma Operator:
The Comma operator uses to link related expressions together.
int i=0, j=2, z=6;
Sizeof Operator:
The Sizeof operator uses to get the size of the variables like constants, variables, arrays, structures, etc.
#include<stdio.h>
int main() {
int a = 5;
struct x {
int q;
char w;
};
char d;
int arr[] = {1,2,3,4,5};
printf("%ld %ld %ld %ld \n", sizeof(a), sizeof(struct x), sizeof(d), sizeof(arr));
return 0;
}
Recent Comments