#include<stdio.
h> 0 and 1
int main()
{ compiler option 1 byte
//statements
char option ;
printf("%c", option );
return 0;
}
variable : char var_name ;
character : a, b, c, d, 1,2,3,4, $, %, & char option ;
integer : 10, -26, 35, -47 char alphabet;
real number/fractional number : 13.5, 19.47, 18.19 15.19
float -> single precesion -> 4 bytes
double -> double acurate -> 8 bytes
%.2f
[email protected]
Size and sign modifier : change some of the properties of the variables
Size modifiers
short int -> 2 bytes -> %hd
int -> 4bytes -> %d
long int -> 4/8 bytes depending om compiler implementation -> %ld
long long int -> 8/16bytes depending om compiler implementation -> %lld
long double -> 12/16 bytes -> %Lf
1234567890
sign modifiers : short , int, long int, long long
int -> 4 bytes 32 bits
signed -> either +ve or -ve ; 1 bit is reserved signess, 31 bits to store the data
unsigned -> only +ve number; use all 32 bits to store the data %u
%hu %lu %llu
Conditional constructs
single iteration multi iteration(loops)
if and its family while
switch do while
for
if condition : to execute some set of statements based on the condition
if(condition)
{
//statements
}
if else : to execute one set of statement if the condition is true and another set of
statements if the condition is false
if(condition) Find the greatest of two numbers
{ int num1 , num2
//code1
} if(num1 > num2)
else {
{ printf("num1 is greater\n");
//code 2 }
} else
{
printf("num2 is greater \n");
}
Check if the numebr is +ve or -ve
if(num > 0)
{
printf("Number is +ve\n");
}
else
{
printf("Number is -ve\n");
}
if-else if - else :
check if number is +ve or -ve or zero
if(num > 0)
{ just say pass -> if
printf("Number is +ve\n");
say pass or fail -> il else
}
else if(num < 0) grades A, B, C -> if else if else
{
printf("Number is -ve\n");
}
else
{
printf("Number is 0\n");
}
Largest of 3 numbers : num1, num2, num3
num1
if - elseif - else True num1 > num3
if(num1 > num2 && num1 > num3)
{ num3
printf("num1 is greater\n"); num1 > num2
} num2
else if(num2 > num1 && num2 > num3)
num2 > num3
{ False
printf("num2 is greater\n");
if(num1 > num2) num3
}
else {
{ if(num1 > num3)
printf("num3 is greater\n"); {
}
printf("num1 is greater\n");
}
else
{
printf("num3 is greater\n");
}
}
else
{
if(num2 > num3)
{
printf("num2 is greater\n");
}
else
{
printf("num3 is greater\n");
}
}
switch case : Replacement to the simple if elseif else block (but can work only with int)
check if the number is 10 or 20 or 30 or 40 or none
if(num == 10)
{ switch(num)
printf("Number is 10\n"); {
} case 10 : printf("Number is 10\n");
else if(num == 20) break;
{ case 20 : printf("Number is 20\n");
printf("Number is 20\n");
break;
}
else if(num == 30)
case 30 : printf("Number is 30\n");
{ break;
printf("Number is 30\n"); case 40 : printf("Number is 40\n");
} break;
else if(num == 40) default :printf("Neither 10 nor 20 nor 30 nor 40\n");
{
}
printf("Number is 40\n");
}
else
{
printf("Neither 10 nor 20 nor 30 nor 40\n");
}
Loops : to execute the statements for more than once continuously
while(condition) -> entry controlled loop
printf("Hello "); { The statements may not get executed
printf("Hello "); //statements even once
printf("Hello "); }
printf("Hello ");
printf("Hello "); do -> exit controlled loop
printf("Hello "); { The statements gets executed atleast once
printf("Hello "); //statements
printf("Hello "); }while(condition) ;
printf("Hello ");
printf("Hello ");
for(inti ; condition ; post eval expre)
{
//statemets
}
break : used in loop or switch case -> used to step out
continue : used only inside loop -> skip and move to the next iteration
https://moodle.emertxe-group.com/login/index.php
Operators :special symbols used to perform some operations
Unary Arithmetic : + , - , *, /, %
Binary Logical : &&, ||, !
ternary Relational : > < >= <= == !=
Bitwise : & | ^ ~ >> <<
sizeof();
x = 10 + 3 * 5 / 9 % 7
x = 10 + 1
x = 11
x = 25 - 12 + ( 3 % 4) * 5 * 2
x = 25 - 12 + 30
13 + 30
43
% => modulus -> reminder
0 -> quotient (division)
4 3
0
3 -> reminder (modulus %)
Logical operators : are used to check mutiple conditions
Logical AND (&&) Logical OR (||) Logical NOT(!)
Input1 Input2 Output Input1 Input2 Output Input Output
False False False False True
False False False
False True False True False
False True True
True False False True False True
True True True True True True
Input1 && Input2 Input1 || Input2
any non zero in place of input is true,
zero is false
Increment ++ -> used to increase the value of a variable by 1
Pre Post x=5
++x x++
x=6 x=6
immediatly change will happen in the next statement
Short circuit evalution :
In || : If the 1st input is true, irrespective of 2nd input, output is true
In && : If the 1st input is false, irrespective of 2nd input, output is false
0.25 * 2 = 0.5 0.7 * 2 = 1.4
0.7 0.5
0.5 * 2 = 1.0 0.4 * 2 = 0.8
1.7 0.25
0.0 * 2 = 0.0 0.8 * 2 = 1.6
2.7 15.25
0.6 * 2 = 1.2
3.7
0.2 * 2 = 0.4
15.7
.
12.2
.
14.8
.
int num1 = 1, num2 = 1; .
x += 5
float num3 = 1.7, num4 = 1.5;
x=x+5
num1 += num2 += num3 += num4;
num1 += num2 += (num3 =3.2);
decimal
num1 += (num2 = num2 + 3.2);
octal
num1 += (num2 = 4);
hexa decimal
num1 = num1 + 4;
binary
num1 = 5;
printf(“num1 is %d\n”, num1)
Bitwise operators : Bits of a num -> num should always be integer
num1 = 0x56,
Bitwise AND: (&) num2 = 0x74
num1 = 0x75, num2 = 0x98
Input1 Input2 Output 0101 0110
0 0 0 0111 0101
0111 0100
0 1 0 1001 1000 0101 0100 -> 0x54
0001 0000 -> 0x10 = 16
1 0 0
1 1 1
num1 = 0x53 , num2 = 0xA9
Bitwise OR : (|)
Input1 Input2 Output num1 | num2 0101 0011
0 0 0 1010 1001
1111 1011 -> 0xFB
0 1 1
1 0 1
1 1 1
Bitwise XOR : (^) num1 = 0xE9, num2 = 0x72
Input1 Input2 Output num1 ^ num2
0 0 0 1110 1001
0 1 1 0111 0010
1 0 1 1001 1011 -> 0x9B
1 1 0
Complement : (~) num = 0xE2
Input1 Output ~num
0 1 1110 0010
1 0 0001 1101 -> 0x1D
Bitwise left shift : << num = 0x02 MSB = lost, LSB = 0
num << no_shifts num << 2
0 0 0 0 0 0 1 0 -> 2 2 * (2 ^ 0) num >> n--> num * (2 ^ n)
Lost 0 0 0 0 0 1 0 0 ->4 2 * (2 ^ 1)
Lost -> 8 2 * (2^2)
0 0 0 0 1 0 0 0
Right shift -> 2 types LSB is lost
signed right shift unsigned right shift
num = 8 msb is filled
num = 0xA3 MSB is filled with
num >> 2 with 0
num >> 2 prev msb
1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 8
1 1 0 1 0 0 0 1 lost 0 0 0 0 0 1 0 0 lost 4
lost 2
1 1 1 0 1 0 0 0 lost 0 0 0 0 0 0 1 0
num >> n --> num / (2^n)
No class tomorrow -> Next class is on Monday