Professional Documents
Culture Documents
UNIT-II C LANGUAGE FYBsc it
UNIT-II C LANGUAGE FYBsc it
UNIT-II
Operators:
● 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.
1. Arithmetic operators
2. Assignment operators
3. Increment / Decrement operators
4. Relational operators
5. Logical operators
6. Conditional operator
7. Bitwise operators
8. Special operators
1. Arithmetic Operator:
The operators that are used to perform arithmetic operations such as Addition,
subtraction, multiplication etc., are called arithmetic operators.
The meaning of all the operators along with examples is shown below:
Arithmetic Description Example Result Priority Associativity
operator
2. Assignment Operator:
● An operator which is used to copy the data or result of an expression into a
memory location (which is identified by a variable name), is called an assignment
operator.
● Copying or storing into a memory location is called assigning and hence the name.
The assignment operator is denoted by ‘=’ sign.
● Assignment always happens from right to left.
For Example:
● a = b; /* Store the value of b into a */
● area = L * B; /* Compute the product and store in variable area */
● pi = 3.1416; /* Store the number 3.1416 using the variable pi */
#include<stdio.h>
int main()
{
int a = 20,b;
b=++a;
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}
Output:
a=21
b=21
b. Post-increment Operator:
● If the increment operator ++ is placed after (post) the operand, then the operator
is called post-increment. As the name indicates, post-increment means increment
● after (post) the operand value is used. So, operand value is used first and then the
operand value is incremented by 1.
● Eg: a++, b++ etc.
Output:
a=21
b=20
a. Pre-decrement Operator:
● If the decrement operator - - is placed before (pre) the operand, then the Operator
is called pre-decrement.
● As the name indicates, pre-decrement means decrement before (pre) the operand
value is used. So, the operand value is decremented by 1 first, and then this
decremented value is used.
● Eg: --a, --b etc.
Sahyog College of Management Studies, Thane
Program to show the use of pre-decrement operator.
#include<stdio.h>
int main()
{
int a = 20;
int b;
b=--a
printf("a=%d\n",a); Output:
printf("b=%d\n",b); a=19
return 0; b=19
}
b. Post-increment Operator:
● If the decrement operator -- is placed after (post) the operand, then the operator is
called post-decrement.
● As the name indicates, post-decrement means decrement after (post) the operand
value is used. So, operand value is used first and then the operand value is
decremented by 1.
● Eg: a--, b-- etc.
Logical operators and the meaning associated with them are shown in the following table:
&& Logical And. If all the (10<20) && (23>5)=1 2 Left to right
conditions are true then
(21<43) &&(5==5)=0
result will be true
a. Logical AND: The result of logical ‘AND’ operator denoted by && is true if and only
if both the operands are evaluated to true. If one of the operands is evaluated to
false.
c. Logical Not: The logical ‘NOT’ denoted by ! can be true or false. The result is true if
the operand is false and the result is false if the operand is true.
Operand 1 Result
True(1) False(0)
False(0) True(1)
Example1:
Suppose, A=10, B=20
(A<B) ? printf(“A is the smallest number”) : printf(“B is the smallest number”);
Output:
A is the smallest number
Example2:
Suppose, A=30, B=12
C= (A<B) ? A : B;
printf(“Smallest no is %d”,C);
Output:
Smallest no is 12
The bit-wise operators and the meaning associated with them are shown in the following table:
#include <stdio.h>
int main()
{
int a=12,b=25,c;
Bitwise OR ( | ):
● The bitwise OR operator is represented by a single vertical sign (|). Two integer operands are
written on both sides of the (|) symbol.
● The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1.
00001100
| 00011001
__________
00011101 = 29 (In decimal)
#include <stdio.h>
int main()
{
int a=12,b=25,c;
c=a^b;
printf("12^25=%d",c);
return 0;
}
Output:
12 ^ 25=21
X Y X&Y X|Y X ^Y ~X
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
0 0 0 1 1 0 1 0 = 26
So,
13<<1=26
#include <stdio.h>
int main()
{
int a=13,b;
b=a<<1;
printf("13<<1=%d",b);
return 0;
}
Output:
13<<1=26
Where,
● Operand is an integer expression on which we apply the right-shift operation.
● n is the number of bits to be shifted.(value of n can be any number)
Discarded
#include <stdio.h>
int main()
{
int a=20,b;
b=a>>1;
printf("20>>1=%d",b);
return 0;
}
Output:
20>>1=10
9. Special Operators:
Sizeof operator:
● This operator is used to find the number of bytes occupied by a variable or a datatype in the
computer memory.
The syntax is shown below:
sizeof(variable_name / Data_type);
Example:
char ch;
sizeof(int); //4 byte
sizeof(ch); // 1 byte
Example:
int a;
&a ; //returns address of a
#include <stdio.h>
int main()
{
int a=20;
printf("Address of a in decimal :%d",&a);
printf("\nAddress of a in Hexadecimal :%X",&a);
return 0;
}
Output:
Address of a in decimal :6487580
Address of a in Hexadecimal :62FE1C
Note:
Precedence operators:
● In C language, each operator is associated with a priority value. Based on the priority, the
expressions are evaluated. A part of the expression with priority value 1 is evaluated first; part of
the expression with priority value 2 is evaluated next and so on.
● These priority values that are associated with various operators are called
Precedence of operators.
Associativity Operators:
● If all the operators in an expression have equal priority, then the direction order chosen (left to
right evaluation or right to left evaluation) to evaluate an expression is called associativity of
operators.
Associativity Operators are classified into 2 categories:
1. Left Associative:
● In an expression, if there are two or more operators having the same priority and are evaluated
from left-to-right, then the operators are called Left to Right associative operators.
For example,
8 + 4 + 3 = 15
Sahyog College of Management Studies, Thane
8-4 -3=1
In the above example + and – both are having equal priority so in this case according to associativity
this expression will be evaluated and associativity of + and – and left to right. So above expression
will execute from left to right.
2. Right Associative:
● In an expression, if there are two or more operators having the same priority and are evaluated
from right-to-left, then the operators are called Right to Left associative operators.
For Example,
i = j = k = 10;
Above expression will execute from right to left because 3 times = operator is used and associativity
of = is right to left.
Syntax:
if(condition)
{
//code to be executed
}
Program:
// C program to illustrate If statement
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
2. IF – else Statement
The if statement evaluates the code if the condition is true but what if the condition is
not true, here comes the else statement. It tells the code what to do when the if
condition is false.
Syntax:
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}
Program:
check whether a number is even or odd using if-else statement in C language
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
output
enter a number:4
4 is even number
enter a number:5
5 is odd number
Sahyog College of Management Studies, Thane
3. If – else – if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements. The execution
starts from top and checked for each if condition. The statement of if block will be executed
which evaluates to be true. If none of the if condition evaluates to be true then the last else
block is evaluated.
Syntax:
if(condition1)
{
// code to be executed if condition1 is true
}
else if(condition2)
{
// code to be executed if condition2 is true
}
else if(condition3)
{
// code to be executed if condition3 is true
}
...
else
{
// code to be executed if all the conditions are false
}
Output
4. Nested – If Statement
if statement inside an if statement is known as nested if. if statement in this case is the target of
another if or else statement. When more than one condition needs to be true and one of the
condition is the sub-condition of parent condition, nested if can be used.
Syntax:
if (condition1)
{
// code to be executed
// if condition2 is true
if (condition2)
{
// code to be executed
// if condition2 is true
}
}
5. Switch Statement
Switch statement is an alternative to long if-else-if ladders. The expression is checked for
different cases and the one match is executed. break statement is used to move out of the
switch. If the break is not used, the control will flow to all cases below it until break is found or
switch comes to an end. There is default case (optional) at the end of switch, if none of the case
matches then default case is executed.
Syntax:
switch (expression)
{
case value1: // statement; break;
case value2: // statement ; break;
.
.
.
case valueN: // statement;break;
default: // default statement sequence
}
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
Looping Statement:
Loops in programming are used to repeat a block of code until the specified condition is met. A loop
statement allows programmers to execute a statement or group of statements multiple times without
repetition of code.
Types of C Loops
1. for
2. while
3. do-while
Output: 1 2 3 4 5 6 7 8 9 10
C Program: Print table for the given number using C for loop
#include<stdio.h>
int main(){
int i=1,number,m;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
m=number * i;
printf("%d * %d = %d \n", number , i m);
}
return 0;
}
#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d ",i);
i++;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Example:
while(1)
{
Printf(“hello”);
}
do
{
//code to be executed
}
while(condition);
Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
printf("value of a: %d\n", a);
a = a + 1;
}
while( a < 20 );
return 0;
}
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
a) break
b) continue
c) goto
a) break:
A break statement is used to terminate the execution of the rest of the block where it is present
and takes the control out of the block.
It is mostly used in loops and switch-case to bypass the rest of the statement and take the
control to the end of the loop.
When a break statement is encountered inside the switch case statement, the execution control
moves out of the switch statement directly.
When the break statement is encountered inside the looping statement, the execution control
moves out of the looping statements.
Another point to be taken into consideration is that break statement when used in nested loops
only terminates the inner loop where it is used and not any of the outer loops.
Output:
value of a: 10
value of a: 11
value of a: 12
b) continue:
The continue statement skips the current iteration of the loop and continues with the next
iteration.
continue statements are only used in loops.
Output:
01235678
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
if(i==2) if(i==3)
break; continue;
printf(“%d”,i); printf(“%d”,i);
return 0; return 0;
} }
Output: Output:
1 2 1 2 4 5
c) goto:
The goto statement allows us to transfer control of the program to the specified label.
The label is an identifier. When the goto statement is encountered, the control of the program
jumps to label: and starts executing the code.
#include <stdio.h>
int main ()
{
int n=1;
start:
printf("Hello ");
n++;
if(n<=5)
{
goto start;
}
return 0;
}
Output:
Hello Hello Hello Hello Hello
Output:
1 2 3 4 5 6 7 8 9 10
NESTED LOOPS:
Loop inside another loop is called nesting of loop.
Nested loops are basically used when we want to generate output in the form row and column i.e.
in tabular form.
1. nested for loop
2. nested while loop
3. nested do while loop
for(j=1;j<=4;j++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}
Output:
1111
2222
3333
4444
5555
Note: outer loop is always used for rows and inner loop is used for columns.
Program:
#include<stdio.h>
int main()
{
int i=1,j;
Output:
123
123
123
12 3
} while(condition);
Program:
#include<stdio.h>
int main()
{
int i=1,j;
do
{
Sahyog College of Management Studies, Thane
j=1;
do
{
printf("%2d ",i*j);
j++;
}while(j<=10);
i++;
printf("\n");
}while(i<=4);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
Note: There is no rule that a loop must be nested inside its own type. In fact, there can be
any type of loop nested inside any type and to any level. For e.g. in for loop we can write
while loop.
************