MODULE-2
Syllabus:
Operators in C, Type conversion and typecasting.
Decision control and Looping statements: Introduction to decision control, Conditional
branching statements, iterative statements, nested loops, break and continue statements, goto
statement.
Operators in C:
Operators are used to perform operations on variables and values.
C has a wide range of operators to perform various operations.
1) Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ Division
% remainder after division (modulo division)
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
2) Increment and 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>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Increment operators are of two types:
a) Pre increment:
Use ++ operator as a prefix like: ++a, the value of a is incremented by 1; then it returns
the value.
b) Post increment:
Use ++ operator as a postfix like: a++, the original value of a is returned first; then a is
incremented by 1.
Decrement operators are of two types:
a) Pre decrement:
Use -- operator as a prefix like: --a, the value of a is decremented by 1; then it returns
the value.
b) Post decrement:
Use -- operator as a postfix like: a--, the original value of a is returned first; then a is
decremented by 1.
3) Relational Operators
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
< Less than 5 < 3 is evaluated to 0
Operator Meaning of Operator Example
!= 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
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
4) Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in
C programming.
Operator Meaning Example
Logical AND. True only if all If c = 5 and d = 2 then, expression
&&
operands are true ((c==5) && (d>5)) equals to 0.
Logical OR. True only if If c = 5 and d = 2 then, expression
||
either one operand is true ((c==5) || (d>5)) equals to 1.
Logical NOT. True only if the If c = 5 then, expression !(c==5)
!
operand is 0 equals to 0.
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
5) Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Bitwise AND Operator &
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit
of an operand is 0, the result of corresponding bit is evaluated to 0.
In C Programming, the bitwise AND operator is denoted by &.
Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
00011001
________
00001000 = 8 (In decimal)
Bitwise OR Operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
Bitwise XOR (exclusive OR) Operator ^
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)
Bitwise Complement Operator ~
Bitwise complement operator is a unary operator (works on only one operand). It changes 1
to 0 and 0 to 1. It is denoted by ~.
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
6) 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
7) Special Operators
a) Comma Operator
Comma operators are used to link related expressions together. When we want to declare
multiple numbers of variables in any program and provide different arguments in any
function, we use the comma in the form of a separator in the program.
For instance,
int x, y, z;
In the statement mentioned above, the comma acts as a separator and informs the compiler
that the x, y, and z variables are three different types of variables.
b) The sizeof operator
The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).
#include <stdio.h>
int main()
{
int a;
printf("Size of int=%lu bytes\n",sizeof(a));
return 0;
}
8) Conditional Operators (Ternary Operators)(?)
The conditional operator is also known as a ternary operator. The conditional statements
are the decision-making statements which depends upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
The behaviour of the conditional operator is similar to the 'if-else' statement as 'if-else'
statement is also a decision-making statement.
Syntax:
Expression1? expression2: expression3;
#include <stdio.h>
int main()
{
int a=5,b;
b=((a==5)?(3):(2));
printf("The value of 'b' variable is : %d",b);
return 0;
}
The associativity of operators:
The operators of the same precedence are evaluated either from left to right or from
right to left depending on the level. This is known as the associativity property of an
operator.
The table below shows the associativity of the operators:
Operator Description of Operator Associativity
. Direct member selection Left to right
-> Indirect member selection Left to right
[] Array element reference Left to right
() Functional call Left to right
~ Bitwise(1’s) complement Right to left
! Logical negation Right to left
– Unary minus Right to left
+ Unary plus Right to left
— Decrement Right to left
++ Increment Right to left
* Pointer reference Right to left
& Dereference (Address) Right to left
(type) Typecast (conversion) Right to left
sizeof Returns the size of an object Right to left
% Remainder Left to right
/ Divide Left to right
* Multiply Left to right
– Binary minus (subtraction) Left to right
+ Binary plus (Addition) Left to right
>> Right shift Left to right
<< Left shift Left to right
> Greater than Left to right
< Less than Left to right
>= Greater than or equal Left to right
<= Less than or equal Left to right
== Equal to Left to right
!= Not equal to Left to right
^ Bitwise exclusive OR Left to right
& Bitwise AND Left to right
|| Logical OR Left to right
| Bitwise OR Left to right
?: Conditional Operator Right to left
&& Logical AND Left to right
, Separator of expressions Left to right
= Simple assignment Right to left
/= Assign quotient Right to left
*= Assign product Right to left
%= Assign remainder Right to left
-= Assign difference Right to left
+= Assign sum Right to left
|= Assign bitwise OR Right to left
^= Assign bitwise XOR Right to left
&= Assign bitwise AND Right to left
>>= Assign right shift Right to left
<<= Assign left shift Right to left
Evaluation of expressions involving all the above type of operators:
The following expression includes operators from six different precedence groups.
Consider variables x, y , z as integer variables.
Z+=(x0>0 && x<=10) ? ++x : x/y;
The statement begins by evaluating the complex
expression(x>0 && x<=10)
If this expression is true, the expression ++x is evaluated. Otherwise, the a/bis evaluated.
Finally, the assignment operation (+=) is carried out, causing the value of c to be increased by
the value of the conditional expression.
If for example x, y, and z have the values 1,2,3 respectively, then the value of the conditional
expression will be 2(because the expression ++a will be evaluated), and the value of z will
increase to 5(z=3+2). On the other hand, if x,y and z have the values 50,10,20 respectively,
then the value of the conditional expression will be 5(because the expression x/y will be
evaluated) and the value of z will increase to 25(z=20+5).
Decision control and Looping statements:
Program to print table for the given number using do while loop
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i++;
}while(i<=10);
return 0;
}
C programs Sample Examples:
Write a C program to find Fibonacci Series without recursion
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Write a c program to check Armstrong Number.
Armstrong number is a number that is equal to the sum of cubes of its digits.
For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Output:
enter the number=153
armstrong number
enter the number=5
not armstrong number
Write a c program to reverse a given number.
#include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}
Output:
Enter a number: 123
Reversed Number: 321
NOTE: For more C programs kindly refer this website.
https://www.javatpoint.com/c-programs
https://www.geeksforgeeks.org/c-programming-examples/
https://beginnersbook.com/2015/02/simple-c-programs/
https://www.studytonight.com/c/programs/
https://codescracker.com/c/program/c-programming-
examples.htm