C Programming Notes Unit 3
C Programming Notes Unit 3
C -Programming Part-I
Topics
Operators :- Assignment operator, Bitwise operator,
sizeof() , Comma operator
Conditional Statement – If,If-else ,switch statement ,conditional operator
Iteration(Loop) Statement – while , do-while, for loop
Assignment Operator
Assignment operators are used for 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
C provides compound assignment operators to assign a value to variable in order to
assign a new value to a variable after performing a specified operation.
Example- variable name=expression (or) value (or) variable
Operator Example Meaning
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y X=x%y
working of Assignment operators
#include <stdio.h>
int main()
{
int a = 10;
printf("Value of a is %d\n", a);
a += 10;
printf("Value of a is %d\n", a);
a -= 10;
printf("Value of a is %d\n", a);
a *= 10;
printf("Value of a is %d\n", a);
a /= 10;
printf("Value of a is %d\n", a);
return 0;
}
output
Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10
Bitwise operators
Bitwise operators in C-
• The bitwise operators are the operators used to perform the operations on the data at the
bit-level.
• When we perform the bitwise operations, then it is also known as bit-level programming.
It consists of two digits, either 0 or 1.
#include <stdio.h>
int main()
{
int num1 = 10; // Binary: 1010
int num2 = 3; // Binary: 0011
int result_and = num1 & num2; // Result: 0010 (decimal: 2)
printf("Bitwise AND Result: %d\n", result_and);
return 0;
}
Output
Bitwise AND Result: 2
2.Bitwise OR ( | ) Operator-
The bitwise OR operator (|) in C performs a bitwise OR operation on each pair of corresponding
bits. The result is 1 if at least one of the bits is 1.
#include <stdio.h>
int main() {
int num1 = 10; // Binary: 1010
int num2 = 3; // Binary: 0011
int result_or = num1 | num2; // Result: 1011 (decimal: 11)
printf("Bitwise OR Result: %d\n", result_or);
return 0;
}
Output
Bitwise OR Result: 11
Bit wise XOR operator, left shift
3. Bitwise XOR (^)
The bitwise XOR operator (^) in C performs a bitwise XOR operation on each pair of
corresponding bits. The result is 1 if the bits are different; otherwise, it's 0.
#include <stdio.h>
int main() {
int num1 = 10; // Binary: 1010
int num2 = 3; // Binary: 0011
int result_xor = num1 ^ num2; // Result: 1001 (decimal: 9)
printf("Bitwise XOR Result: %d\n", result_xor);
return 0;
}
Output
Bitwise XOR Result: 9
#include <stdio.h>
int main() {
int num = 5; // Binary: 0101
int shift = 2;
int result_left_shift = num << shift; // Result: 10100 (decimal: 20)
printf("Left Shift Result: %d\n", result_left_shift);
return 0;
}
Output
20
Hint:- 5 x 22 =20
#include <stdio.h>
int main()
{
printf("Size of char: %d bytes\n", sizeof(char))
printf("Size of int: %i bytes\n", sizeof(int));
printf("Size of float: %i bytes\n", sizeof(float));
printf("Size of double: %i bytes\n", sizeof(double));
return 0;
}
Output:-
Size of char: 1 bytes
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
2. When the operand is an expression: When sizeof() is used with the expression, it returns
the size of the expression.
#include <stdio.h>
int main()
{
int a = 0;
double d = 10.21;
printf("%lu", sizeof(a + d));
return 0;
}
Output
8
3.Type of operator
sizeof() is a compile-time operator. compile time refers to the time at which the source
code is converted to a binary code. It doesn’t execute (run) the code inside ().
#include <stdio.h>
int main()
{
int y;
int x = 11; // value of x doesn't change
y = sizeof(x++); // prints 4 and 11
printf("%i %i", y, x);
return 0;
}
Output
4 11
Comma as an operator
A comma operator in C is a binary operator. It evaluates the first operand & discards
the result, evaluates the second operand & returns the value as a result
It has the lowest precedence among all C++ Operators
It is left-associative
Used to evaluate multiple expression.
Example-1
int a=5,6,7; //error not allowed at declaration time.
Example-2
int a=(5,6,7);
//a=7 allowed at declaration time within parenthesis(left to right, rightmost value assigned).
Example-3
int a;
a=(5,6,7); //a=7 allowed and assign rightmost value to a.
Example-4
int a;
a=5,6,7; //a=5 allowed and without parenthesis assign leftmost value to a.
if statement syntax-
if(condition)
{
// Statements to execute if condition is true
}
Ex:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Please Enter a Number");
scanf("%d",&n);
if(n>=15)
{
printf("Greater or equal to 15");
}
getch();
}
if---else statement-
syntax-
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Please Enter your age");
scanf("%d",&age);
if(age>=18)
{
printf("You can vote");
}
else
{
printf("You can’t Vote");
}
getch();
}
Lecture 20
if else-if ladder
if---else if ladder-
syntax:
if (condition)
{
statement;
}
else if (condition)
{
statement;
}.
……..
else
{
statement;
}
Example: program for leap year
#include<stdio.h>
#include<conio.h>
void main()
{
int year=1600;
if(year%400==0)
{
printf("%d is leap year",year);
}
else if(year%4==0 && year%100!=0)
{
printf("%d is leap year",year);
}
else
{
printf("%d is not a leap year",year);
}
getch();
}
Points to Remember
It isn't necessary to use break after each block, but if you do not use it, all the consecutive block
of codes will get executed after the matching block.
int i = 1;
2. switch(i)
3. {
4. case 1:
5. printf("A"); // No break
6. case 2:
7. printf("B"); // No break
8. case 3:
9. printf("C");
10. break;
11. }
Output : A B C
The output was supposed to be only A because only the first case matches, but as there is no
break statement after the block, the next blocks are executed, until the cursor encounters a break.
default case can be placed anywhere in the switch case. Even if we don't include the default case
switch statement works.
It first evaluate the condition, if it is true (non-zero) then the “exp1” is evaluated, if the
condition is false (zero) then the “exp2” is evaluated.
#include <stdio.h>
int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);
days = (February == '1') ? 29 : 28;
printf("Number of days in February = %d",days);
return 0;
}
Output
If this year is leap year, enter 1. If not enter any integer: 1
Number of days in February = 29
Types of Loops
Entry Controlled loops: In Entry controlled loops the test condition is checked before
entering the main body of the loop. For Loop and While Loop is Entry-controlled loops
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end
of the loop body. The loop body will execute at least once, irrespective of whether the
condition is true or false. do-while Loop is Exit Controlled loop.
While Loop
The while loop is an entry controlled loop statement, i.e means the condition is
evaluated first and it is true, then the body of the loop is executed.
After executing the body of the loop, the condition is once again evaluated and if it is
true, the body is executed once again, the process of repeated execution of the loop
continues until the condition finally becomes false and
variable initialization ;
while (condition)
{
statements ;
variable increment or decrement
}
while loop can be addressed as an entry control loop. It is completed in 3 steps.
Variable initialization.( e.g int x=0; )
return 0;
}
Output
The sum of number from 1 to 10 is 55
#include<stdio.h>
int main()
{
int num;
int i=1;
For Loop:
This is an entry controlled looping statement.
One of the most important features of this loop is that the three actions can be taken at a time
like variable initialization, condition checking and increment/decrement.
The for loop can be more concise and flexible than that of while and do-while loops.
statements
}
Various forms of FOR LOOP
I am using variable num in all the below examples –
1) Here instead of num++, I‟m using num=num+1 which is nothing but same as num++.
for (num=10; num<20; num=num+1)
2) Initialization part can be skipped from loop as shown below, the counter variable is declared
before the loop itself.
int num=10;
for (;num<20;num++)
Must Note: Although we can skip init part but semicolon (;) before condition is must, without
which you will get compilation error.
Like initialization, you can also skip the increment part as we did below. In this case semicolon
(;) is must, after condition logic. The increment part is being done in for loop body itself.
for (num=10; num<20; )
{
//Code
num++;
}
4) Below case is also possible, increment in body and init during declaration of counter variable.
int num=10;
for (;num<20;)
{
//Statements
num++;
}
5) Counter can be decremented also, In the below example the variable gets decremented each
time the loop runs until the condition num>10 becomes false.
for(num=20; num>10; num--)
Program to calculate the sum of first n natural numbers
Example:
#include<stdio.h>
#include<conio.h>
void main( )
{
int x;
for(x=1; x<=10; x++)
{
printf("%d\t",x);
}
getch();
}
Output
1 2 3 4 5 6 7 8 9 10
Once the statement(s) is executed then The increment can be done before or after the
increment is done. execution of the statement(s).
It is normally used when the number of It is normally used when the number of
iterations is known. iterations is unknown.
For loop is entry controlled loop. While loop is also entry controlled loop.
The for loop is used when the number of The while loop is used when the number of
iterations is known. iterations is unknown.
Do-While loop :-
It is a Exit-Controlled Loop
The do-while loop is similar to a while loop but the only difference lies in the do-while
loop test condition which is tested at the end of the body
In the do-while loop, the loop body will execute at least once irrespective of the test
condition.
Synatax:-
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
#include<stdio.h>
int main()
{
int a=1;
do
{
printf(“%d\n”,a);
a++;
}
while(i<=10);
return 0;
}
Output
12345678910
------------------------------------------------------------------------------------------------------------