Module 2_PPT_P1
Module 2_PPT_P1
Module 2
Integers are whole numbers that can have both zero, positive and negative values
but no decimal values. For example, 0, -5, 10
We can use int for declaring an integer variable.
int id=8; //valid
int id=1.8; //invalid
int a=10;
float b=12.3;
char c=‘d’;
a=b; // value 12 will be stored inside variable ‘a’. Data loss is there.
b=a; // value 10.0 will be stored inside variable ‘b’
}
2) Explicit Type Conversion (type casting)
(Higher to Lower Datatype Conversion)
The syntax in C:
(type) expression
#include<stdio.h>
int main()
{
double fD = 24.1;
float sF=22.2;
int i=(int)fD;
float f = (float)fD;
int i2=(int)sF;
printf("explicit conversion of double into integer is=> %d\n",i);
printf("explicit conversion of double into float is=> %f\n",f);
printf("explicit conversion of float into int is=> %i\n",i2);
printf("%lf\n",fD);
return 0;
}
// C program to demonstrate explicit type casting (higher to lower datatype)
// C program to demonstrate explicit type casting (higher to lower datatype)
#include <stdio.h>
int main() {
float c = 5.55;
int s = (int)c+1;
printf("%d",s);
return 0;
}
Output
6
// C program to demonstrate explicit type casting (higher to lower datatype)
Operators and Expressions
p q p && q p || q p != q
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise Operators
A= A<<1
A=8+16+32+64=120
A=60
A= A>>1
A=2+4+8+16=30
a<<=2 //a=a<<2
a>>=2 //a=a>>2
Increment and Decrement operators
Let a=5
a++;
a--;
++a;
--a;
When i++ is used as prefix(like: ++var), ++var will increment the value
of var and then return it but, if ++ is used as postfix(like: var++),
operator will return the value of operand first and then only increment
it. This can be demonstrated by an example:
#include <stdio.h>
int main()
{
int c=2; Output
printf(“%d\n”,c++); 2
Printf(“%d”,++c);
return 0; 4
}
Conditional Operators (? :) / Ternary operator
Conditional operators are used in decision making in C programming , i.e,
executes different statements according to test condition whether it is either true or
false.
Sizeof is a much used operator in the C or C++. It is a compile time unary operator
which can be used to compute the size of its operand. The result of sizeof is of
unsigned integral type which is usually denoted by size_t.
// The sizeof(variable)operator computes the size of a variable. And, to print the
result returned by sizeof, we use %lu format specifier. l= long integer, u= unsigned
sqrt(s*(s-a)*(s-b)*(s-c))
(-b+sqrt(b*b-4*a*c))/(2*a)
Conversion of Mathematical Expression into C Equivalent Expression
a.) 𝑥
+ 𝑦 (x/(b+c))+(y/(b-c))
𝑏+ 𝑏−
b.) 𝑎 + 𝑏(𝑎𝑑+𝑒)
− 𝑐 a+((b*(a*d+e))/(b-a))-c/d
𝑏 −𝑎 𝑑
Operators Precedence & Associativity in C
Here, operators with the highest precedence appear at the top of the
table, those with the lowest appear at the bottom. Within an
expression, higher precedence operators will be evaluated first.
Operators examples
Relational Expression Description
This condition is used to check whether the x is
x%2 = = 0
an even number or not. The relational
x=7, result=false=0 expression results in value 1(true) if x is an
even number otherwise results in value
0(false).