Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

TRIPATHI DOC

C Language
Operator in C:- An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in operators and provides the
following types of operators.

Type of Operator in C
Name of Operator Description
Arithmetic Operators +, -, *, /, %
Increments/Decrements Operators ++, --
Relational Operators ==, !=, =, < =, > =, <, >
Logical Operators &&, ||, !
Bitwise Operators &, ^, |, ~, >>, <<
Assignment Operators =, +=, -+, *=, /=, %=, <<=, >>=, &=, ||=,
^=
Other Operators ?:, &, *, sizeof(), comma(,)

Arithmetic Operators
+, - , *, /, %

Addition, Sub Mul Div Modulus

All are binary Operators means two Operands are required to perform operations
Ex 2+3, 4/2 e.t.c.

// C program to demonstrate
// working of binary arithmetic
// operators
#include <stdio.h>

int main()
{
int a, b, res;

// printing a and b
printf("a is %d and b is %d\n", a, b);
scanf(“%d”, &a,&b);

res = a + b; // addition
printf("a+b is %d\n", res);

res = a - b; // subtraction
printf("a-b is %d\n", res);

res = a * b; // multiplication
printf("a*b is %d\n", res);

res = a / b; // division
printf("a/b is %d\n", res);

res = a % b; // modulus
printf("a%%b is %d\n", res);

return 0;
}

O/P:- a is 10 and b is: 4


a+b is: 14
a-b is: 6
a*b is: 40
a/b is: 2
a%b is: 2

Operators Precedence and Associativity


Precedence
Operators Associativity
*, / , % Left to Right
+, - Left to Right

Associativity used when two or more operators having same precedence.


For ex:- +, -

#include<stdio.h>
#include<conio.h>
Void main()
{
Int a, b, c, d;
Clrscr();
printf(“enter the num”);
scanf(“%d”, &a,&b,&c,&d);
printf(“a * b /c =%d\n”, a*b/c);
printf(“a + b -c =%d\n”, a+b-c);
printf(“a + b * d – c %% a =%d\n”, a+b*d-c%a);
getch();
}

Increment/Decrement Operators

C programming has two operators increment ++ and decrement -- to change


the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the
value by 1. These two operators are unary operators, meaning they only
operate on a single operand.

// Working of increment and decrement operators


#include<stdio.h>
#include<conio.h>
void main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
clrscr();
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
getch();
}

Output

++a = 11
--b = 99
++c = 11.500000
--d = 99.500000

#include<stdio.h>
#include<conio.h>
void main() {
int var1 = 5, var2 = 5;
clrscr();

// 5 is displayed
// Then, var1 is increased to 6.
printf("%d\n", var1++);

// var2 is increased to 6
// Then, it is displayed.
printf("%d\n", ++var2);
getch();
}

Output

5
6

Note:- 1. Increment and Decrement Operator are uniary Operator.


2. (a+b)++ , ++(a+b) are invalid compiler treat its as a expression.

Relational Operators
A relational operator checks the relationship between two operands. If the
relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.

Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1


Operator Meaning of Operator Example

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

Ex:-

Consider a program segment:

If(n%2==0)
{
printf(“even num”);
}
else
{
printf(“odd num”);
}
If the condition in the if statement is true(1) it executed the body of if and
print a msg even num otherwise it return false(0) and executed the body of
else statement and print the msg odd num.

Assignment Operators

Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/
Operator Example Same as

%= a %= b a = a%b

// Working of assignment operators


#include<stdio.h>
#iclude<conio.h>
void main()
{
int a = 5, c;
clrscr();
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

getch();
}

Output

c = 5
c = 10
c = 5
c = 25
c = 5
c = 0

sizeof() Operator
The sizeof is a unary operator that returns the size of data such as-
1. Variable
2. Constant
3. Expression
4. Data type such as int, char, float e.t.c
#include <stdio.h>
int main()
{
printf("%lu\n", sizeof(char));
printf("%lu\n", sizeof(int));
printf("%lu\n", sizeof(float));
printf("%lu", sizeof(double));
return 0;
}
The above program find out the size of data type.

Output:
1
4
4
8

When sizeof() is used with the expression, it returns size of the expression.
Let see example:
#include <stdio.h>

int main()
{
int a = 0;
double d = 10.21;
printf("%lu", sizeof(a + d));
return 0;
}

Output:
8

You might also like