C Operators and Expressions Explained
C Operators and Expressions Explained
CHAPTER - 2
C Operators and
Expressions
Expression:
Ex: A + B
C – Operators
C language provides different types of operators. They are
Unary Operators
An operator that acts upon only one operand is known as unary operator. The unary operators are
Unary minus:
Any positive operand can be associated with unary minus operand then its value gets
changed to negative. It is indicates with – operator along with operand.
Syntax: - (operand)
b = -3
in the above example initially the value of a is 3 but when this a has associated with unary minus
its value gets changed to negative.
Increment Operator:
This is represented by “++” (double plus) symbol. This symbol can be placed either before or
This increment is two types. They are pre increment and post increment.
Pre increment:
When the increment operator can be placed before the integer variable
that is known aspre increment operation.
In this the first the value of operand or integer variable must be incremented.
Ex: a= ++b
b = b + 1;
a = b;
Post increment:
When the increment operator can be placed after the integer variable that is known as
post increment operation.
In this the first the value of operand or integer variable must be stored or used.
Ex: a= b + +
a = b;
b = b + 1;
void main()
{
int m = 6 , n = 8;
printf(“ m is %d\n”, + + m);
printf(“n is %d\n” , n + + );
}
Decrement Operator:
This is represented by “- -” (double minus) symbol. This symbol can be placed either before or
This decrement is two types. They are pre decrement and post decrement.
Pre decrement:
When the decrement operator can be placed before the integer variable that is known as pre
decrement operation.
In this the first the value of operand or integer variable must be decremented.
And then use or store that value.
Ex: a= - - b
b = b - 1;
a = b;
ex: a= 6 then - - a is 5
Post decrement:
When the decrement operator can be placed after the integer variable that is known as
post decrement operation.
Ex: a= b - -
This is equivalent to 2 assignment statements. They are
a = b;
b = b - 1;
ex: a= 6 then a - - is 6 only.
/* program to demonstrate decrement operators */
void main()
{
int m = 6 , n = 8;
printf(“ m is %d\n”, - - m);
printf(“n is %d\n” , n - - );
}
Binary Operators
An operator that acts upon minimum two operands is known as binary operator. The binary
operators are
Assignment operators
Arithmetic Operators:
These rea the operators used to perform the basic arithmetic operations such as addition ,
subtraction, multiplication and division. The modulus operator is added to the list of C arithmetic
operators. It is used to find the remainder value.
void main()
{
int a , b ,sum sub, mul, div ;
printf(“\n enter 2 numbers”);
scanf(“%d%d”, &a, &b);
sum = a + b ;
sub = a – b;
mul = a * b;
div = a / b;
printf(“\n sum is %d\n”, sum);
printf(“\n sub is %d\n”, sub);
printf(“\n product is %d\n”, mul);
printf(“\n quotient is %d\n”, div);
getch();
}
Relational operators:
They define the relationship that exists between 2 constants or variables. They result in either a
true or false value. The value of true is nonzero that is 1 and that of false is zero.
Logical operators:
These are used to combine two or more expressions. A logical relationship between the
two expressions. A logical relationship between the expressions is checked with logical
operators. The logical operators are
T T T
T F F
F T F
F F F
T T T
T F T
F T T
F F F
T F
F T
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
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/b
%= a %= b a = a%b
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
The bitwise AND operator is represented by a single &. This operator operates on two
integer operands. When two operands are one (1) then the result of this operation will be 1.
Otherwise zero (0).
0 0 0
0 1 0
1 0 0
1 1 1
X&Y= 00000001
Bitwise OR ( | ):
If any one of the operands will be one then the result of this operation will be one
otherwise zero.
0 0 0
0 1 1
1 0 1
1 1 1
X | Y= 00000111
The bitwise XOR operator is represented by a ^ (carot) symbol. This operator operates on
two integer operands. When two operands are zero (0) and one then the result of this operation
will be 0. otherwise one (1).
0 0 0
0 1 1
1 0 1
1 1 0
X ^ Y= 00000110
This operator is also called as 1’s compliment operator. This operator changes 1 to 0 and
0 to 1. This is represented by ~ (tilde character).
X = 5 00000101
11111010
Left shift operator is used to move bits from left side to right side. It is represented by << (double
less than).
Ex: a << 5
X = 10 00001010
Right shift operator is used to move bits from right side to left side. It is represented by >>
(double greater than).
Department of BCA Page 10
Programming in C Govt First Grade College,Tumkur
Ex: a >> 5
X = 10 00001010
void main()
printf(“12|8 is %d\n”,12|8);
Ternary Operator
This is known as Conditional operator. This is used to test the relationship between two
variables. This operator contains a condition followed by two statements or two values.
If the condition is true then the first statement 1 is executed otherwise second statement
will be executed. The condition operator is represented by “ ?: “. It requires two or more
arguments.
z(x>y) ? x : y
the condition x>y is true then x value is printed otherwise y will be printed.
Special Operators
Assignment Operator:
Ex: a = 5;
= = is the relational operator, which is used to compare two quantities means constants, variables
etc.
Comma Operator:
The comma operator can be used to link the related expressions. It is also known as
delimiter or separator. Using comma operator two or more expressions may be combined into a
single expression.
Ex: temp = a;
a= b;
temp = a; a = b; b = temp ;
Size of ( ) Operator:
Size of ( ) operator returns the number of bytes used for the operands, constants, data
types etc.
Where the operand can be data type, constants or variables using this operator we can get
the size of any data type.
int i;
Then d = a + b * c
1
2
In the above example there are 2 operators. They are + and *. In those * has highest precedence
than + operator. For that
d = 20 + 10 * 5
Department of BCA Page 13
Programming in C Govt First Grade College,Tumkur
= 20 + 50
d = 70
if same precedence operators are appeared on a single expression in such situations associatively
from left to right the left most operator will be first executed.
Arithmetic Expressions
A group of constants, variables and operators arranged as per the syntax is known as arthemetic
expression.
Integer Arthemetic:
Ex: 2 + 3 = 5
Floating Arthemetic:
In this some values are integers and some values are real values.
In mixed mode arthemetic expression contains operands of different data types certain
type of conversions are automatically done by the computer itself this is known as implicit type
conversion.
double
float
lowest int
Promotion:
When the variable or constant of lower data type is converted into higher data type is
known as promotion.
Ex: int i = 5;
float f;
f = i;
in the above example I is lowest data type that is integer and f is the highest data type that is
float.
Demotion:
When the variable or constant of highest data type is converted into lowest data type is
known as demotion.
int i;
i = f;
in the above example f is highest data type that is float and i is the lowest data type that is
integer.
Ex: x = (float) 6 + 5 ;
Here 5 and 6 both are integer numbers but the answer will be converted to float data type by
using float casting operator.
void main( )
getch();
Control Structures
Sequence
Whether that condition was true then execute the statements in that if block other wise we come
out from that condition.
if (condition)
statement;
Ex:
#include<stdio.h>
main()
{
int i = 3,j = 5;
if(i < j)
If - else Statement
It is two way branching statement. This is also depends on the condition.
Whether that condition was true then execute the statements in that if block other wise else block
statements are executed.
Ex:
#include<stdio.h>
void main()
{
int a=10, b=20;
clrscr();
if(a>b)
printf(“a is large %d”, a);
else
printf(“ b is large %d”,b);
getch();
Nested If statement
The if statement may itself contain another if statement is known as nested if statement.
Syntax:
if(condition1)
{
If(condition2)
statement1;
else
statement2;
}
else
{
If(condition3)
Statement3;
}
Ex:
main()
{
int a,b,c;
printf(“\n enter 3 numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“ a is large %d”,a);
else
printf(“ c is large %d”,c);
}
else
{
if (c>b)
printf(“ a is large %d”,c);
else
printf(“ c is large %d”,b);
}
}
Ex:
#include <stdio.h>
main()
{
int i = 2;
if(i == 0)
{
printf(" i == 0");
}
else if(i == 1)
{
printf(" i == 1. \n");
}
else if(i == 2)
{
printf(" i == 2. \n");
}
}
Switch Statement
Switch statement provides multi-way branching. It is an extension of if-else statement. Using if-
else a maximum 2 branches are allowed. If there is need for multiple branches then we use
switch is the best option.
To take one of a number of possible actions.
switch is preferred over multiple if...else statements.
Syntax:
switch (expression)
{
case label 1 : statements;
break;
case label 2 : statements;
break;
:
:
case label n : statements;
break;
default : default statements;
} statements;
}
The expression present within the parenthesis of switch statement must be of type int or
char.
Based on the expression. The control is transferred to a particular case label are executed.
The case label must be of type int or char.
The case default is executed only when none of the cases are true.
The default block is optional.
Break is a keyword is used to terminate the switch construct. The control is transferred to
the first statement after the switch construct.
The labels with several cases need not be in order.
More than one statement with a case need not be enclosed in braces.
It is not compulsory to have the statements for a particular case.
There should not be a semicolon at the end of the closing braces after expression.
Nested switch construct will be allowed.
Ex:
main()
{
case 1 : printf(“number
is 1”);break;
case 2: printf(“ number
is 2”);break;
case 3 : printf(“number
is 3”);break;
default: printf(“invalid number”);
}
}
break statement
goto statement.
Break Statement: The break statement ends execution of the nearest enclosing loop or
conditional statement in which it appears. (or)
Syntax:
while(condition)
{
if(condition)
{
statement 1;
:
statement n
break;
}
}
statement n+1;
Ex: main()
{
int i=1;
while(i<=5)
{
if(i==3)
break;
printf(“%d”,i)
i++;
}
}
In the above program output will be 1 and 2.
When I becomes 3 then break statement will be executed then the control transferred to the out
of the loop.
The continue: when this keyword is encountered, it transfers the control back to the loop
condition by skipping the rest of statements of the body of the loop.
This keyword works only with loops. This cannot be used in switch construct.
The transfer of control of continue statement is as shown below.
Syntax: while(condition)
{
if(condition)
{
statement 1;
:
statement n
continue;
}
}
Ex: main()
{
Because when the value of I becomes 2, the condition (i==2) is satisfied and continue statement
will be executed. At the situation 2 will not be printed it will move to the next iteration.
goto statement:
this is an unconditional branching statement. In order to identify where we are in
program, we must and should needed one label. A label is a name that immediately followed by
semi colon.
goto even;
exit() statement:
exit() function is a standard library function that comes ready made with the C compiler. Its
purpose is to terminate the execution of the program.
Break statement terminates the execution of loop in which it is written, where as exit()
function terminates the execution of the program itself.
Looping Statement
Using decision making control structures statements are executed only once during execution of
the program. In some situations, it may be necessary to execute a list of statements more than
once. In such cases, the looping constructs are used.
“ looping construct is a construct that executes the statements repeatedly for a certain number
of times as long as the condition is true.
C has provided three types of iteration statements:
1. While loop.
2. do...while loop
3. For loop
While Loop
While loop is also known as “pre tested loop” or “entry controlled loop”.
Why means in this we are first check out the condition. Whether that condition was true then the
control transferred to the inside the loop. Otherwise exit from the loop.
In While looping statement allows a number of lines represent until the condition is satisfied
Ex:
Syntax: #include<stdio.h>
main()
while (condition) {
int i = 0;
{
printf(" the value of i is %d\n", i);
i = i + 1;
}
}
Flowchart:
Do While Loop
In while loop, the condition is tested in the beginning. If the condition becomes false for first
time, then the body of while loop will not executed even once. In some programming situations,
we may require to execute the body of a loop at least once even though condition is false for first
time. In such situations do-while loop is used.
Do-while loop is also known as “post tested loop” or “exit controlled loop”.
Because here we are checked the condition after one iteration. Whether that condition was true
then control transferred to next iteration otherwise comes out from the loop.
Syntax: Ex:
do #include<stdio.h>
{ void main()
} while(Expr); i=1;
do
i = i+ 1;
} while(i<=n)
Flowchart:
For Loop
For loop is also known as “pre tested loop” or “fixed execution loop”. Thie loop is the
modified form of a while loop.
The for loop is especially used to execute the statements for a certain number of times.
This loop simplifies the program as well as can reduce the number of statements.
Nested Loop in C
As the name already suggests, a loop inside a loop is called Nested Loop. There can be any number of loops
inside a loop. We know there are generally many looping conditions like for, while, and do-while. We can
loop different kinds of loops within each other to form nested loops. C language supports this functionality of
Nested Loops. below is the syntax of Nested Loop in C.
Syntax:
Outside_loop
{
//Outside Loop Statements
Inside_loop
{
//Inside loop Statements
}
}
Flowchart:
Example
Nested loop in ‘for’ condition. This we can generally use for creating or printing a multi-dimensional array.
Code:
#include<stdio.h>
int main()
{
int i,j,x,y;
int a[10][10];
printf("Enter value for x(rows)- max of 10:");
scanf("%d", &x);
printf("Enter value for y(columns) - max of 10:");
scanf("%d",&y);
printf("Let's create a 2-Darray:");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Now printing the array:");
printf("\n");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
printf("\t");
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the value for x(rows) – max of 10 : 3
Enter the value for y(columns) – max of 10 : 2
Lets’s create a 2-D array: 1
2
3
4
5
6
Now printing the array:
1 2
3 4
5 6
Questions:
1. What is an Expression?
2. Write a short note on operators.
3. What is difference between = and = = ?
4. What is the difference between - - count and count - - ?
5. Explain any 3 unary operators.
6. Explain implicit and explicit type conversions.
7. What is the conditional operator explain?
8. Explain different operators in C with examples.
9. Explain logical and relational operators with an example.
10. Write a C program to find size of integer, character and real variables.
11. What is an operator ?
12. Explain type casting with an example.
13. Explain size of ( ) operator.
15. What is Assignment operator explain?
16. What is meant by looping statement? Explain a pre-tested and a post-
tested loop with asuitable program.
17. What is the purpose of if-else construct?
18. What is the purpose of break statement in a loop?
19. Which of the looping statement is always executes at least ones?
20. Explain any three forms of If statements?
21. Give the syntax of for statement
22. Explain the nested IF statement.
23. Write a program to find the sum of n natural numbers using while
statement.
24. Explain for loop statement.
25. Give the syntax of switch statement. and explain
26. Explain any 3 character handling functions.
27. Differentiate between while and do-while loops.
28. Explain various condition statements.
29. What is the use of control statements?
30. Explain general syntax of for loop with an example program.
31. Explain exit, break and goto statements.
32. Explain else-if ladder with example program.
33. How to terminate infinite loop.
34. Mention any 2 differences between while and do-while.
35. Write the general syntax of the following
a) nested-if b) switch c) for loop