Operators in c programming language

C Arithmetic Operators

An arithmetic operator performs mathematical operations equivalent to addition, subtraction, multiplication, division and many others on numerical values (constants and variables). this is operator in c language

Example 1: Arithmetic Operators

// Working of arithmetic operators

#include <stdio.h>

int main()

int x = 5,y = 4, z;

x = y+z;

printf(“x+y = %d \n”,z);

z = x-y;

printf(“x-y = %d \n”,z);

z = x*y;

printf(“x*y = %d \n”,z);

z = x/y;

printf(“x/y = %d \n”,z);

z = x%y;

printf(“Remainder = %d \n”,z);

return 0;

Output

x+y = 9

x-y = 1

x*y = 20

x/y = 1

Remainder =1

C Increment and Decrement Operators

C programming has two operators increment ++ and decrement — to change value by 1.

Increment ++ will increase by 1 whereas decrement — decreases by 1.

Example 2: Increment and Decrement Operators

// Working of increment and decrement operators

#include<stdio.h>

int main()

{

int x = 20, y = 200;

float z = 20.5, m = 200.5;

printf(“++x = %d \n”, ++x);

printf(“–y = %d \n”, –y);

printf(“++z = %f \n”, ++z);

printf(“–m = %f \n”, –m);

return 0;

}

Output

++x = 21

–y = 199

++z = 21.500000

–m = 199.500000

C Assignment Operators

An task operator is used for assigning a worth to a variable. The most typical task operator is =

Example 3: Assignment Operators

// Working of assignment operators

#include<stdio.h>

int main()

{

int x = 3, y;

y = x;      // y is 5

printf(“y = %d\n”, y);

y += x;     // y is 10

printf(“y = %d\n”, y);

y -= x;     // y is 5

printf(“y = %d\n”, y);

y *= x;     // y is 25

printf(“y = %d\n”, y);

y /= x;     // y is 5

printf(“y = %d\n”, y);

y %= x;     // y = 0

printf(“y = %d\n”, y);

return 0;

}

Output

y = 3

y = 6

y = 3

y = 9

y = 3

y = 0

C Relational Operators

A relational operator checks the connection between two operands. If the relation is true, it returns 1; if the relation is false, it returns worth 0.

Example 4: Relational Operators

// Working of relational operators

#include<stdio.h>

int main()

{

int x = 5, y = 5, z = 10;

printf(“%d == %d is %d \n”, x, y, x == y);

printf(“%d == %d is %d \n”, x, z, x == z);

printf(“%d > %d is %d \n”, x, y, x > y);

printf(“%d > %d is %d \n”, x, z, x > z);

printf(“%d < %d is %d \n”, x, y, x < y);

printf(“%d < %d is %d \n”, x, z, x < z);

printf(“%d != %d is %d \n”, x, y, x != y);

printf(“%d != %d is %d \n”, x, z, x != z);

printf(“%d >= %d is %d \n”, x, y, x >= y);

printf(“%d >= %d is %d \n”, x, z, x >= z);

printf(“%d <= %d is %d \n”, x, y, x <= y);

printf(“%d <= %d is %d \n”, x, z, x <= z);

return 0;

}

Output

5 == 5 is 1

5 == 10 is 0

5 > 5 is 0

5 > 10 is 0

5 < 5 is 0

5 < 10 is 1

5 != 5 is 0

5 != 10 is 1

5 >= 5 is 1

5 >= 10 is 0

5 <= 5 is 1

5 <= 10 is 1

C Logical Operators An expression containing logical operator returns both 0 or 1 in decision making in C programming.

Example 5: Logical Operators

// Working of logical operators

(x == y) && (z > y) is 1

(x == y) && (z < y) is 0

(x == y) || (z < y) is 1

(c != y) || (z < y) is 0

!(x != y) is 1

!(x == y) is 0

C Bitwise Operators

Other Operators in c language

Comma Operator

int a, c = 5, d;

comma operator separate the variable with value using(,)

The sizeof operator

Example 6: sizeof Operator

#include <stdio.h>

int main()

{

int x;

float y;

double z;

char c;

printf(“Size of int=%lu bytes\n”,sizeof(x));

printf(“Size of float=%lu bytes\n”,sizeof(y));

printf(“Size of double=%lu bytes\n”,sizeof(z));

printf(“Size of char=%lu byte\n”,sizeof(c));

return 0;

}

Output

Size of int = 4 bytes

Size of float = 4 bytes

Size of double = 8 bytes equal to Size of char = 1 byte

this tutorial based in Operators in c language

Categories: c language