Programming in C Unit 2
Programming in C Unit 2
UNIT - II
C Operators & Expressions
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Increment & Decrement Operators
• Bitwise Operators
• Conditional Operator
• Special Operators
• Operator Precedence and Associatively
• Evaluation of Arithmetic Expressions
• Type Conversion
Control Structures
• Decision Making Statements
• Looping Statements
Unary plus(+), Unary minus(-), pointer
operator(*), address operator(&),
increment/decrement operator(++/--)
•Arithmetic Operators
•Relational Operators
•Logical Operators
•Bitwise Operators
•Shorthand Operators
Conditional operator(?:)
• sizeof()
• Comma
Example for Arithmetic operators
Return(0);
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
Int a=10, b=3, res;
clrscr() ; if(ch=='/')
printf("Enter an operator '+', '-', '*', '/', '%'\n") ; {
scanf("%c", &ch) ;
printf("\nEntered Character is%c", ch) ;
printf("\nDivision") ;
if(ch=='+') res=a/b;
{ printf("\nDivision of two numbers=℅d",
printf("\nAddition") ;
res) ;
res=a+b;
printf("\nSum of two numbers= %d", res) ; }
} if(ch=='%')
if(ch=='-') {
{
printf("\nSubtraction") ; printf("\nModular division ") ;
res=a-b; res=a%b;
printf("\nSubtraction of two numbers=%d", res) ; printf("\nMod of two numbers=%d",
}
res) ;
if(ch=='*')
{ }
printf("\nMultiplication") ; getch() ;
res=a*b; }
printf("\nMultiplication of two numbers%d", res) ;
}
Example for relational operator Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
#include <stdio.h> Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to
void main() b
{ int a = 21; int b = 10; int c ;
if( a == b )
{ printf("Line 1 - a is equal to b\n" ); }
else { printf("Line 1 - a is not equal to b\n" ); }
if ( a < b ) { printf("Line 2 - a is less than b\n" ); }
else {
printf("Line 2 - a is not less than b\n" ); }
if ( a > b )
{ printf("Line 3 - a is greater than b\n" ); }
else { printf("Line 3 - a is not greater than b\n" );
} /* Lets change value of a and b */ a = 5; b = 20;
if ( a <= b ) { printf("Line 4 - a is either less than or equal to b\
n" ); } if ( b >= a ) { printf("Line 5 - b is either greater than or equal to a\
Logical AND Operator ( && ) , Logical OR Operator ( || )
Logical NOT Operator ( ! )
// C program for Logical // C program for Logical
// AND Operator // OR Operator
#include <stdio.h> #include <stdio.h>
Output Both values are greater Output Any one of the given value is
than 0 greater than 0
// C program for Logical
// NOT Operator
#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (!(a > 0 && b > 0)) {
// condition returned true but
// logical NOT operator changed it to false
printf("Both values are greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a
variable and right side operand of the assignment operator is a value. The value on the right side must be of the same
data-type of the variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
•“=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on
the left.
For example:a = 10; b = 20; ch = ‘y’;
•“+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on
left to the value on the right and then assigns the result to the variable on the left.
Example:
•(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.
•“-=”This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable
on left from the value on the right and then assigns the result to the variable on the left.
•Example:
•(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2.
•“*=”This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current
value of the variable on left to the value on the right and then assigns the result to the variable on the
left.
•“/=”This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value
of the variable on left by the value on the right and then assigns the result to the variable on the left.
// AS PREFIX ++m
// AS POSTFIX m++
where m is variable.
// C Program to illustrate the increment of both type
#include <stdio.h>
void main()
{
int a = 5;
int b = 5;
// PREFIX
int prefix = ++a; Output:
printf("Prefix Increment: %d\n", prefix); Prefix Increment: 6
Postfix Increment: 5
// POSTFIX
int postfix = b++;
printf("Postfix Increment: %d", postfix);
}
Decrement Operator in C
The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, the value is first
decremented and then used inside the expression. Whereas in the Post-Decrement, the value is first used inside the expression and then
decremented.
Syntax
Just like the increment operator, the decrement operator can also be used in two ways:
// AS PREFIX --m // AS POSTFIX m--
m--;
m--;
Differences between Increment And Decrement Operators
The following table list the difference between the increment and decrement operators:
Increment Operator adds 1 to the operand. Decrement Operator subtracts 1 from the operand.
The Postfix increment operator means the expression is evaluated first using The Postfix decrement operator means the expression is evaluated first using
the original value of the variable and then the variable is the original value of the variable and then the variable is
incremented(increased).The decremented(decreased).
Prefix increment operator means the variable is incremented first and then the Prefix decrement operator means the variable is decremented first and then
expression is evaluated using the new value of the variable. the expression is evaluated using the new value of the variable.
Generally, we use this in decision-making and looping. This is also used in decision-making and looping.
// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
}
Unary minus
Operator Precedence and Associativity Table
The following tables list the C operator precedence from highest to lowest and the associativity for each of the operators:
Operator
Precedence Description Associativity
* Dereference Operator
& Addressof Operator
sizeof Determine size in bytes
3 *,/,% Multiplication, division, modulus Left-to-Right
4 +/- Addition, subtraction Left-to-Right
Bitwise shift left, Bitwise shift
5 << , >> Left-to-Right
right
Relational less than, less than
< , <=
or equal to
6 Left-to-Right
Relational greater than,
> , >=
greater than or equal to
Relational is equal to, is not
7 == , != Left-to-Right
equal to
8 & Bitwise AND Left-to-Right
9 ^ Bitwise exclusive OR Left-to-Right
10 | Bitwise inclusive OR Left-to-Right
11 && Logical AND Left-to-Right
12 || Logical OR Left-to-Right
13 ?: Ternary conditional Right-to-Left
= Assignment
Addition, subtraction
+= , -=
assignment
Multiplication, division
*= , /=
assignment
14 Modulus, bitwise AND Right-to-Left
%= , &=
assignment
Bitwise exclusive, inclusive OR
^= , |=
assignment
Bitwise shift left, right
<<=, >>=
assignment
15 , comma (expression separator) Left-to-Right
a=20, b=10 , c=15 and d=5;
e = (a + b) * c / d;
e = ((a + b) * c) / d;
e = (a + b) * (c / d);
e = a + (b * c) / d;
Control Structures
Control Structures
Control Structures
Sequential control
structure.
Selection control
structure.
Iteration control
structure.
//Addition of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
Int a, b, sum;
clrscr();
printf(“Enter first number\n”);
scanf(“%d”, &a);
printf(“Enter second number\n”);
scanf(“%d”, &b);
Sum=a+b;
printf(“Sum of two numbers = %d”, sum);
getch();
}
Decision making Control Statements
Decision making Control Statements
Decision making Control Statements
Decision making Control Statements
Decision making Control Statements
Decision making Control Statements
Conditional Operator(? :)
• 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;
Loop Control Structures
do while example
•#include<stdio.h>
•int main(){
•int i=1;
•do{
•printf("%d \n",i);
•i++;
•}while(i<=10);
•return 0;
Jumps in Loops
• The break or goto statement can be used to terminate the execution
of a loop (jumping out of a loop) and the continue statement can be
used to skip part of a loop.
i=1;
for (; ;) for (i=1; i<=5; i++)
{ {
printf(“%d”, i); if (i==2)
i++;
if (i==6)
continue;
break; printf(“%d”, i);
} }
Field width Specification for Printing Integers
• Syntax
• %WC – right justified %-WC – left justified
Where ‘W’ indicates total number of columns required to print the value.It is
also called the total width of the output.
‘C’ is the format specifiers for the integer. %d, %u, %lu etc.
Example:
2 5 4
a=254 printf(“%5d”,a) Right justified
3 6 7
a= 367 printf(%-5d”,a) Left justified
Note:
If number of columns(W)are not sufficient to print a value, then additional
columns are automatically created.
If less columns are occupied with data, blanks will be shown in place holders.
Blanks will appear on right if the text is Left justified. Blanks will appear on Left
if the text is right justified..
Qualifiers or modifiers are Keywords, Used to alter the basic data types based on
the size and sign. The qualifiers that can be applied to the basic data types are
Signed, unsigned, short,long
<modifier><basic data type><var1>
Short int a,b;
Unsigned int a=400000000;
Qualifier for char data type
Char ch;
Signed char ch;
Every character is associated with ASCII value.
The Break statement is used to exit from the loop constructs. The continue statement is not used to exit from the loop constructs.
The break statement is usually used with the switch statement, and it can also The continue statement is not used with the switch statement, but it can be
use it within the while loop, do-while loop, or the for-loop. used within the while loop, do-while loop, or for-loop.
When a break statement is encountered then the control is exited from the When the continue statement is encountered then the control automatically
loop construct immediately. passed from the beginning of the loop statement.
Syntax: Syntax:
break; continue;
Break statements uses switch and label statements. It does not use switch and label statements.
Leftover iterations are not executed after the break statement. Leftover iterations can be executed even if the continue keyword appears in a
loop.
break exit()