Operators & Expressions
- Operators are used to carryout the arithmetic and logical operations.
- Operators act as connectors.
- The values that can be operated by these operators are called operands.
- C operators – unary operators, binary operators, ternary operators.
- An operator that acts upon only one operand is known as a unary operator.
- Egs – unary minus, logical NOT, bitwise complementation.
- Binary operators act upon two operands.
- Classified into four – arithmetic operators, logical operators, relational operators, bitwise operators.
- Arithmetic operators are used to perform basic arithmetic operations such as addition, subtraction, multiplication and division.
- An expression involving arithmetic operators is called an arithmetic expression.
- Increment operator – used to increment the value of an integer quantity by one.
- Represented by ‘++’.
- Decrement operator – used to reduce the value of an integer by one.
- Represented by ‘- -’.
Program 1
/*Program to illustrate the basic arithmetic operations*/
#include<stdio.h>
void main()
{
int n1,n2; /*variable declaration*/
int sum,diff,prod,quotient,remainder;
n1=5;
n2=3;
sum=n1+n2;
diff=n1-n2;
prod=n1*n2;
quotient=n1/n2;
remainder=n1%n2;
printf(”SUM = %d\n”,sum);
printf(”DIFFERENCE = %d\n”,diff);
printf(”PRODUCT = %d\n”,prod);
printf(”QUOTIENT = %d\n”,quotient);
printf(”REMAINDER = %d\n”,remainder);
}
Program 2
/*Program to illustrate the use of an increment operator*/
#include<stdio.h>
void main()
{
int a=3,b=6;
printf(”a = %d\n”,a++);
printf(”b = %d\n”,++b);
}
Program 3
/*Program to illustrate the use of a decrement operator*/
#include<stdio.h>
void main()
{
int m=6,n=8;
printf(”m = %d\n”,- -m);
printf(”n = %d\n”,n- -);
}
- Relational operators – used to compare two operands given in an expression.
- Result in either a TRUE(non-zero,1) or a FALSE(0) value.
- Six relational operators – <, <=, >, >=, ==, !=.
- A relational expression can be defined as a meaningful combination of operands and relational operators.
Program 4
/*Program to check whether given two integers are equal*/
#include<stdio.h>
void main()
{
int x=2,y=2;
if (x= =y)
printf(”x and y are equal\n”);
}
Program 5
/*Program to compare two values*/
#include<stdio.h>
void main()
{
float m=2.333, n=0.0232;
if (m>n)
printf(”m is greater than n\n”);
}
- Logical operators – This class of C operators is used to make decisions.
- AND, OR, NOT.
- The result of these operators is either TRUE or FALSE.
- The logical operators are used to connect one or more relational expressions.
- Expresions involving the logical operators are called the logical expressions.
Program 6
/*Program to illustrate the logical ANDing and ORing operations*/
#include<stdio.h>
void main()
{
int a,b,c;
a = 4;
b = 3;
c = a && b;
b = a || b || c;
a = a && b || c;
printf(”%d,%d,%d”,a,b,c);
}
- Bitwise operators – To perform the bitwise operations, C provides six bitwise operators.
- & – Bitwise AND
- | – Bitwise OR
- ^ – Exclusive-OR(XOR)
- ~ – 1’s complement
- << – Left shifting of bits
- >> – Right shifting of bits
Program 7
/*Program to illustrate the bitwise operators*/
#include<stdio.h>
void main()
{
unsigned int x,y;
x=128;
y=32;
x=x>>1;
printf(”After right-shifting by 1, x = %d\n”,x);
y=y<<2;
printf(”After left-shifting by 2, y = %d\n”,y);
}
- Conditional operator – This is used to test the relationship between two variables.
- It is used in conjunction with a relational expression.
- Syntax – <expression>?<value1>:<value2>;
Program 8
/*Program to illustrate the conditional operator*/
#include<stdio.h>
void main()
{
int a=4,b=5,result1,result2;
result1=a>b?a:b;
printf(”The result1 = %d\n”,result1);
result2=a<b?a:b;
printf(”The result2 = %d\n”,result2);
}
Special operators of C
- Comma operator
- sizeof() operator
- Address operator
- Dereferencing operator
- Dot operator
- Arrow operator
Program 9
/*Program to illustrate the use of the comma operator in inter-changing the values of two variables*/
#include<stdio.h>
void main()
{
int a,b,temp;
b=(a=20,a+10);
printf(”a = %d and b = %d(before)\n”,a,b);
temp=a,a=b,b=temp;
printf(”a = %d and b = %d(after)\n”,a,b);
}
- The sizeof() operator returns the size(i.e, number of bytes) of an operand.
- It is a function and therefore has to be written in lowercase letters.
Program 10
/*Program to illustrate the use of sizeof() operator*/
#include<stdio.h>
void main()
{
int x;
float y;
char ch=’y';
x=10;
y=1000;
printf(”size of x = %d\n”, sizeof(x));
printf(”size of y = %d\n”, sizeof(y));
printf(”size of ch = %d\n”, sizeof(ch));
printf(”size of double = %d\n”, sizeof(double));
}
Precedence, Associativity of all C operators – Table
| Operator Category | Operators | Precedence | Associativity |
| Parentheses,braces | ( ),[ ] | 1 | L to R |
| Unary Operators | -, ++, – -, !, ~, & | 2 | R to L |
| Multiplicative Operators | *, /, % | 3 | L to R |
| Additive Operators | +, - | 4 | L to R |
| Shift Operators | <<,>> | 5 | L to R |
| Relational Operators | <,<=,>,>= | 6 | L to R |
| Equality Operators | ==, != | 7 | L to R |
| Bitwise Operators | &,^,| | 8 | L to R |
| Logical Operators | &&,|| | 9 | L to R |
| Conditional Operators | ?,: | 10 | R to L |
| Assignment Operators | =,+=,-=,*=,/=,%=,&=,^=,|=,<<=,>>= | 11 | R to L |
| Comma operator | , | 12 | L to R |
- Shorthand assignment operators – a compact way of writing assignment statements in an expression.
Program 11
/*Program to illustrate the comapct representation of arithmetic operators*/
#include<stdio.h>
void main()
{
int x=3,y=4,z=1;
x+=y;
y-=x;
z*=x;
printf(”%d\n”,x);
printf(”%d\n”,y);
printf(”%d\n”,z);
}
- In some applications, we may often want to change the data type of the variables. This process is known as data type conversion.
- Also called type casting.
- Syntax – (data type)variable
- Preporcessor directives – C preprocessor is a collection of special statements called the preprocessor directives.
- The preprocessor directives are executed before the C program is compiled.
- Usually written at the beginning of the pogram. However, it is not mandatory.
- There is no semicolon at the end of the preprocessor directives.
Program 12
/*Program to illustrate the use of sqrt(),sin(),cos() and tan()*/
#include<stdio.h>
#include<math.h>
void main()
{
float x,y,sq,sinx,cosx,tanx;
x=4.0;
y=30.0;
sq=sqrt(x);
y=30*(3.142/180.0); /*convert to radians*/
sinx=sin(y);
cosx=cos(y);
tanx=tan(y);
printf(”Square root of x = %f\n”,sq);
printf(”Sine value of y = %f\n”,sinx);
printf(”Cosine value of y = %f\n”,cosx);
printf(”Tangent value of y = %f\n”,tanx);
}
Program 13
/*Program to illustrate the use of abs(),log10(),pow() & exp()*/
#include<stdio.h>
#include<math.h>
void main()
{
float x,y,xpy,lg,xp;
int z,az;
x=2.0;
y=3.0;
z=-4;
az=abs(z);
lg=log10(x);
xp=exp(y);
xpy=pow(x,y);
printf(”Absolute value of z = %d\n”,az);
printf(”Logarithm of x = %d\n”,lg);
printf(”e to the power y = %f\n”,xp);
printf(”x to the power y = %f\n”,xpy);
}
Related posts:








Leave your response!