C Intro_OPerator_Expression
C Intro_OPerator_Expression
C
DR. S JAHANGEER SIDIQ
ASSISTANT PROFESSOR
SCOPE
VIT CHENNAI
Agenda
Quick recap of basic C program
Operators
Expressions
About First Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Welcome to C”);
getch();
}
About First Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Welcome to C”);
getch();
}
Header Files
Created by the company
Contains a huge collection of predefined programs
Helps to quickly writes the program
Header files (contd..)
# pound
Lines starts with Preprocessor directives
Preprocessor is a bult-in software
◦ What is the task?
Header files (contd..)
# pound
Lines starts with Preprocessor directives
Preprocessor is a bult-in software
◦ What is the task?
Basics
Program
Source Expanded Compiler 010100100
Code Preprocessor
Source code 0100010011
010100010
About First Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Welcome to C”);
getch();
}
About Second Program (Contd..)
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf(“Enter your roll”);
scanf(“%d”,&a); /*scanf(“format specifier”,&<var_name>) */
getch();
}
To do
WAP to accept grade, roll and percentage from the user as input and then display there values
back on the screen.
Operators
Symbol through which we can perform various operations on the variables or values
Three most basic types are
◦ Arithmetic operators
◦ Relational operators
◦ Logical operators
Arithmetic Operators
Operators through which we can perform various operations of mathematics on variables.
There are five arithmetic operators are
◦ +
◦ -
◦ *
◦ /
◦ %
Arithmetic Operators (contd..)
Mostly they work according to basic mathematical way but in some special cases they differ from math behavior
Int a=10,b=3;
◦ a+b
◦ a-b
◦ a*b
◦ a/b
◦ a%b Modulo operator works with int only and not with float values
◦ If numerator is smaller than denominator then the remainder will always be the numerator.
◦ 1%2 ---->1
◦ If numerator is +ve the remainder is +ve
◦ 7%5 ==>2
◦ 7%(-5) ===> -2
◦ If numerator is –ve then remainder is –ve
◦ -7%(-5) ---->-2
◦ -7%5 ---->-2
Relational Operator
There are 6 relational operators
◦ >
◦ <
◦ >=
◦ <=
◦ ==
◦ !=
Multiline assignment
◦ Ex- int a=10,b=10,c;
◦ c=a=b;
◦ What about a=b=10=c;
◦ 10=11;
◦ 10=10;
◦ 10==10
To do
Int a=10, b=10,c;
◦ c=a=b;
◦ c=a==b;
◦ c=a==‘b’;
◦ c=a=‘b’
◦ c=‘a’==‘b’
Logical Operators
&& logical AND operator
|| logical OR operator
! logical NOT operator
Logical Operators (Contd..)
Multiple relational operators shall always be used along with logical operators
Behaviour of &&
◦ 1 && 1 =1 or T && T=T
◦ 0 && 1 =0 or F && T=F
◦ 1 && 0 =0 or T && F=F
◦ 0 && 0 =0 or F && F=F
Behaviour of ||
◦ 1 || 1 =1 or T || T=T
◦ 0 || 1 =1 or F || T=T
◦ 1 || 0 =1 or T || F=T
◦ 0 || 0 =0 or F || F=F
To do
Int a=10,b=10,c=15,d;
Evaluate the following in C program
◦ d=a>b && b>c
◦ d=a>b ||b>c
Precedence and Associativity of
Operators
Rule of precedence helps the compiler to decide that which operator has to be solved first if
expression contains multiple operators.
Highest priority is given to parenthesis i.e. ()
◦ /,*,%
◦ +,-
◦ <,>,>=,<=
◦ ==,!=
◦ &&, ||
◦ =
If operators are of same precedence then left to right evaluation will take place. For few it goes for right to
left also
To do
Evaluate the following in your program
◦ a=b+c*d-e;
◦ a=b+c>d*e==f
◦ a=(b+c)*(d-e)
◦ a=b*(c-(d+e))
Program
WAP to accept 3 integer from user; calculate and print their sum and average.
WAP to accept radius of a circle from the user and calculate and print the area and
circumference.
Typecasting
Take 2 integer, divide it and store the result should be printed in float value.
Solution-1
#include<stdio.h>
#include<conio.h>
void main()
{
◦ int a,b;
◦ float c;
◦ clrscr();
◦ printf(“Enter 2 integer:”)
◦ scanf(“%d %d”,&a,&b);
◦ c=a/b;
◦ printf(“div is: %f ”, c);
◦ getch();
}
Another way is to use following line
c=(float)a/b;
Or
c=a/(float)b;
Solution-2
#include<stdio.h>
#include<conio.h>
void main()
{
◦ int a,b;
◦ float c;
◦ clrscr();
◦ printf(“Enter 2 integer:”)
◦ scanf(“%d %d”,&a,&b);
◦ c=(float)a/b; //c=a/(float)b
◦ printf(“div is: %f ”, c);
◦ getch();
}
Will it work
#include<stdio.h>
#include<conio.h>
void main()
{
◦ int a,b;
◦ int c;
◦ clrscr();
◦ printf(“Enter 2 integer:”)
◦ scanf(“%d %d”,&a,&b);
◦ (float)c=(float)a/b; //c=a/(float)b
◦ printf(“div is: %f ”, c);
◦ getch();
}
Typecasting
It is a technique using which a programmer can temporarily change the data type of a variable
to some other type during execution of an expression. But as soon as the expression execution is
over the variable again gets converted to its original type. Also called as Local Conversion.
It never on LHS of the expression
Type conversion
Whenever we assign a value of different data type to a variable of another data type, then the
C/C++ compiler automatically convert the value on RHS of assignment a/c to the variable on LHS
and this conversion done by the compiler is called Type conversion.
Int a;
a=1.6;
Difference between Type casting
and Type conversion
1- done by programmer, called as Explicit conversion 1- done by compiler so called as Implicit Conversion
3- It can be done with or without assignment operator 3- It is done in the context of assignment operator
To do: Analyze the effect of
expression
1- int a;
a=10/4;
2- float a;
a=10/4;
3- int a;
a=10/4.0;
Type Promotion
In C programming whenever we perform arithmetic operations on variable of 2 different data
type, then before performing the operation the compiler upgrades the variable/value of smaller
type to the data type of the higher operand automatically.
If we add int and float then before adding compiler will promote/upgrade int to float and this
promotion is called type promotion
Evaluate the following
10+’Z’+1.5
Range Hierarchy
Signed char
Unsigned char
Signed int
Unsigned int
Long int
Float
Double
Long double
To do and Analyze the Effect
a=b+c*d/e
a----->int
b----->double
c----->char
d----->int
e----->float