0% found this document useful (0 votes)
1 views

C Programming _ Extra Notes (Part-03)

The document provides class notes on the printf() function, undefined behaviors, control flow statements, and examples of using if-else statements and switch-case statements in C programming. It explains the return value of printf(), side effects, sequence points, and the ternary operator, along with various programming assignments and examples. Additionally, it covers the structure and syntax of control statements, including loops and decision-making constructs.

Uploaded by

Rathod Jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

C Programming _ Extra Notes (Part-03)

The document provides class notes on the printf() function, undefined behaviors, control flow statements, and examples of using if-else statements and switch-case statements in C programming. It explains the return value of printf(), side effects, sequence points, and the ternary operator, along with various programming assignments and examples. Additionally, it covers the structure and syntax of control statements, including loops and decision-making constructs.

Uploaded by

Rathod Jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

03

3rd Week Class Notes


Printf return value

The printf() function will return the number of characters printed.

Example 1
#include <stdio.h>
int main(){
printf("%d", printf("ABCD"));
return 0;
}
Answer
ABCD4
Example 2
#include <stdio.h>
int main(){
printf("\n%d", printf("ABCD"));
return 0;
}
Answer
ABCD
4
Example 3
#include <stdio.h>
int main(){
int a=1000;
printf("%d",printf("\n%d",a));
return 0;
}
Output of the program is _____

Answer
10005

Certain Undefined behaviours

Side effect
X = Y has no side effect
X = Y++;
This expression is having some side effect

Side effects? What are side effects?


Evaluation of an expression produces something and if in addition there is a change in the state
of the execution environment it is said that the expression (its evaluation) has some side effect(s).

Sequence Point:
At certain specified points in the execution sequence called sequence points, all side effects of
previous evaluations shall be complete and no side effects of subsequent evaluations shall have
taken place.

Undefined behaviours

Between two sequence points a variable must not be modified more than once. In an expression
statement, the next sequence point is usually at the terminating semicolon, and the previous
sequence point is at the end of the previous statement. An expression may also contain
intermediate sequence points.
From the above sentence the following expressions invoke Undefined Behaviour.
i++ * ++i; // i is modified more than once
i = ++i // same as above
++i = 2; // same as above
i = ++i +1 // same as above

Ternary Operator:

?
v variable = Expression1 ? Expression2 : Expression3;
variable = Expression1 ? Expression2 : Expression3;

Example 1

#include <stdio.h>

int main()
{
int a;
a = 10>7?10:20;
printf("%d", a);

return 0;
}

Answer

10

Example 2

#include <stdio.h>
int main()
{
int a;
a = 10?10:20;
printf("%d", a);

return 0;
}

Answer

10

Example 3

#include<stdio.h>
int main(){
int x=3, y=4, z=4;
printf("%d", (z>=y>=x?100:200));
return 0 ;
}
(a) 100 (b) 200 (c) 0 (d) 1

Answer
B

Control Flow Statement

Sequential flow of execution:


Sequential control is the "default" control in the sense that every statement automatically points
to the next statement in the flowchart diagram. You do not need to do any extra work to make
sequential control happen.
Types of Control statement

• Selection or Branching or Decision or Conditional


• Iterative statement
• Jump statements

Selection or Branching or Decision


o if
o if-else
o if-else-if
o nested if
o switch

if-else statements:

Decision making structures require that the programmer specify one or more conditions
to be evaluated or tested by the program, along with a statement or statements to be
executed if the condition is determined to be true, and optionally, other statements to be
executed if the condition is determined to be false. Following is the general form of a
typical decision-making structure found in most of the programming languages:
C programming language assumes any non-zero and non-null values
as true, and if it is either zero or null, then it is assumed
as false value. C programming language provides following types
of decision making statements.

Syntax

The syntax of an if statement in C programming language is:


if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true
*/
}

int main(){
s1;
s2;
s3;
s4;

int main(){
s1;
if(condition){
s2;
s3;
}
s4;

}
Statement S1 and S2 will execute if the condition is TRUE

int main(){
s1;
if(condition/expression){
s2;
s3;
}
s4;

Statement S1 and S2 will execute if the condition is TRUE

Flow of execution will be defined by

Example 1
#include <stdio.h>
int main (){
int a = 10
if( a < 20) {
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}

Example 2
#include <stdio.h>
int main (){
int a = 10
if(a+2) {
printf("Namaskar" );
}
printf("Dosto");
return 0;
}

What is output
HelloWorld

Example 3
#include <stdio.h>
int main (){
int a = 10
if(a-10) {
printf("Hello" );
}
printf("World");
return 0;
}

What is the output of the program ____


World

Example 3
#include <stdio.h>
int main (){
int a = 10
if(-a) {
printf("Hello" );
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld

Example 4
#include <stdio.h>
int main (){

if(printf(“”) {
printf("Hello" );
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld

Example 5

#include <stdio.h>
int main (){

if(printf("")){
printf("Hello" );
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld

Example 6

#include <stdio.h>
int main (){

if(!6){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
World
Example 7

#include <stdio.h>
int main (){

if(3.14){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld

Example 8

#include <stdio.h>
int main (){

i=0;
if(i++){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
World

Example 9
#include <stdio.h>
int main (){

i=0;
if(i++){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
World

Example 10
#include <stdio.h>
int main (){

i=0;
if(i++);{
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld

Example 11
#include <stdio.h>
int main (){

i=0;
if(i++){
printf("Hello");
}
printf("World");
return 0;
}

If-else statement

int main(){
s1;
if(condition/expression){
s2;
s3;
}
else {
s4;
s5;

}
S6;

Only else is not allowed

If bracket is not present.

Example 10
#include <stdio.h>
int main (){

i=1;
if(i+2-3){
printf("Hello friends");
}
else
printf("mai bhi hu");
return 0;
}

Example 11
#include <stdio.h>
int main (){

int i=1;
if(i++){
printf("Hello friends");
}
else;
printf("\tmai bhi hu");
return 0;
}

Programming assignment
• Program to Check Even or Odd
• #include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

• Program to Check Odd or Even Using the Ternary Operator

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

(num%2==0)?printf("%d is even.", num):printf("%d is odd.",


num);
return 0;
}

• Find largest of two numbers

#include<stdio.h>
int main ()
{
int num1, num2;
num1=12,num2=13;
if (num1 == num2)
printf("both are equal");
else if (num1 > num2)
printf("%d is greater", num1);
else
printf("%d is greater", num2);
return 0;
}

Checking multiple condition

Question 1
Decide the grade based on the marks
If marks <50 then Grade is F
if marks >=50 <60 then Grade is D
if marks >=60 <70 then Grade is C
if marks >=70 <80 then Grade is B
if marks >=80 <90 then Grade is A
if marks >=90 then Grade is A+

program

#include<stdio.h>
int main()
{
int marks;
printf("Enter your marks ");
scanf("%d",&marks);
if(marks<0 || marks>100)
{
printf("Wrong Entry");
}
else if(marks<50)
{
printf("Grade F");
}
else if(marks>=50 && marks<60)
{
printf("Grade D");
}
else if(marks>=60 && marks<70)
{
printf("Grade C");
}
else if(marks>=70 && marks<80)
{
printf("Grade B");
}
else if(marks>=80 && marks<90)
{
printf("Grade A");
}
else
{
printf("Grade A+");
}
return 0;
}

Question 2
Largest of 3 Integer a,b,c using if and else

#include <stdio.h>

int main()
{
int a, b, c;

printf("enter the numbers a, b and c: ");


scanf("%d %d %d", &a, &b, &c);

// finding max using compound expressions


if (a >= b && a >= c)
printf("%d is the largest number.", a);

else if (b >= a && b >= c)


printf("%d is the largest number.", b);

else
printf("%d is the largest number.", c);

return 0;
}

Largest of 3 Integer a,b,c using ternary Operator

#include <stdio.h>

int main()
{
// variable declaration
int n1 = 5, n2 = 10, n3 = 15, max;

// Largest among n1, n2 and n3


max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);

// Print the largest number


printf("Largest number among %d, %d and %d is %d.", n1, n2, n3,
max);

return 0;
}

#include <stdio.h>
int main (){

int a=0;
if(a++);
{
a = a+10;
a = a-5 ;

}
printf("%d", a);
return 0;
}

Swich Statement

Switch statements:
A switch statement allows a to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each case.

The switch-case statement is a multi-way decision statement. Unlike the multiple decision
statement that can be created using if-else, the switch statement evaluates the conditional
expression and tests it against numerous constant values. The branch corresponding to the value
that the expression matches is taken during execution.

break Terminates the loop or switch statement and transfers execution to


statement the statement immediately following the loop or switch.

Syntax
The syntax for a switch statement in C++ is as follows:
switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional

// you can have any number of case statements.


default : //Optional
statement(s);
}
Note: Break and default are optional

Break statements
Switch case statements are used to execute only specific case statements based on the switch
expression. If we do not use break statement at the end of each case, program will execute all
consecutive case statements until it finds next break statement or till the end of switch case
block.

Few Points to note


Breakup is optional

#include <stdio.h>
int main(){
int a = 5;
switch(a){
case 4: printf("%d",5);
case 5: printf("%d",5);
case 6: printf("%d",6);
}
}

Expression evaluates to integer

#include <stdio.h>
int main(){
switch(13/4){
case 3: printf("%d",4); break ;
case 4: printf("%d",2); break ;
case 5: printf("%d",5); break ;
}
}

}
#include <stdio.h>
int main(){

switch(13/4.0){
case 4: printf("%d",4); break ;
case 3: printf("%d",2); break ;
case 5: printf("%d",5); break ;
}
}

#include <stdio.h>
}
int main(){
switch(printf(“ABCD”){
case 4: printf("%d",4); break ;
case 3: printf("%d",2); break ;
case 5: printf("%d",5); break ;
}
}

Output
ABCD4
#include <stdio.h>
int main(){
switch('c'){
case 'a': printf("%d",4); break ;
case 'b': printf("%d",2); break ;
case 'c': printf("%d",5); break ;
}
}

Output:____________________
Position of default does not matter

#include <stdio.h>
int main(){
switch('d'){
case 'a': printf("%d",4); break ;
case 'b': printf("%d",2); break ;
default: printf("None");
case 'c': printf("%d",5); break ;
}
}

Output ______________________
Duplicate case labels are not allowed

#include <stdio.h>
int main(){
switch(4){
case 2: printf("%d",4); break ;
case 1+1: printf("%d",2); break ;
default: printf("None");
}
}

Output:____________

Case labels can’t be variable

#include <stdio.h>
int main(){
int a=2;
switch(4){
case 2: printf("%d",4); break ;
case a: printf("%d",2); break ;
default: printf("None");
}
}
FOR LOOP
for (expe1;expr2;expr3)
expr1 is initialization
expr2 is condition checking
expr3 is increment or decrement
Note :

It’s not mandatory to have all expr1, epr2, expr3

expr1: is the initialization , first statement in for loop is executed


expr2 is condition checking: every time condition is TRUE, the block of the code will execute.
Expr3 is increment or decrement: after each statement of the block is executed then last statement is
Expr
Example
Print (“ I am good Student 5 times)
for (i=1;i<=5; i++){
printf(“I am a good Student”);
}
First statement is i=1;
Iteration no 1: condition check 1 <= 5; statement is TRUE
It will execute print and statement: I am a good student
i incremented to 2

Iteration no 2: condition check 2 <= 5; statement is TRUE


It will execute print and statement: I am a good student
i incremented to 3

Iteration no 3: condition check 3 <= 5; statement is TRUE


It will execute print and statement: I am a good student
i incremented to 4

Iteration no 4: condition check 4 <= 5; statement is TRUE


It will execute print and statement: I am a good student
i incremented to 5

Iteration no 5: condition check 5 <= 5; statement is TRUE


It will execute print and statement: I am a good student
i incremented to 6

Iteration no 6: condition check 6 <= 5; statement is False, block will not execute.

Decrement loop
for (i=5;i>=5; i--){
printf(“I am a good Student”);
}
Condition < and >
for (i=1;i<5; i++){
printf(“I am a good Student”);
}
Question number of times loop executed.
for (i=1;i>5; i++){
printf(“I am a good Student”);
}
Answer: 0 times
Question number of times loop executed.
for (i=1;i<=10; i++){
printf(“I am a good Student”);
}

Answer 10 times

for (i=10;i<=100; i++){


printf(“I am a good Student”);
}
Number of times loop executed is 91
100 – 10 +1 = 91

Number of times loop executed


for (i=23;i<93; i++){
printf(“I am a good Student”);
}
Number of times loop executed is 91
92 – 23 +1 = 60 times

Number of times loop executed


for (i=20;i<=90; i++){
printf(“I am a good Student”);
}
Number of times loop executed is 91
90 – 20 +1 = 71 times

Number of times loop executed


Consider the following program
#include<stdio.h>
int main (){
char ch ;
for(ch=1;ch;ch++)
printf("%c\n",ch);
return 0;
}
What is TRUE about this program when we run the program
(A) Compiler Error
(B) Warning
(C) Loop will run for 1 time
(D) Loop will run for 255 times

for (i=23;i<93; i++){


printf(“I am a good Student”);
}
Number of times loop executed is 91
92 – 23 +1 = 60 times

Skipping initialization
int i = 1;
for(;i<=10;i++){
printf(“I am a good Student”);
}

Number of times loop executed is :

Skip iteration:
int i = 1;
for(;i<=10;){
printf(“I am a good Student”);
}

Number of times loop executed is :


Infinite
Skip iteration but included in block:
int i = 1;
for(;i<=10;){
printf(“I am a good Student”);
i++;
}

Skip the condition part

int i = 1;
for(;; i++){
printf(“I am a good Student”);

}
If condition is empty then it’s always evaluated true.
True means infinite loop

Skipping all the condition


int i = 1;
for(;;){
printf(“I am a good Student”);

}
This leads to infinite loop

Number of times loop executed is :


Infinite loop

Infinite loop condition


for (i=1;i>=1;){
printf(“I am a good Student”);
}

Answer is infinite

Number of times loop executed


for (i=1; i; i++){
printf(“I am a good Student”);
}
Number of times loop execute is infinite
Infinite

Number of times loop executed


for (i=1; 1; i++){
printf(“I am a good Student”);
}
Number of times loop execute is infinite
Infinite

Number of times loop executed


for (i=1; 0; i++){
printf(“I am a good Student”);
}
Number of times loop execute is infinite
0 times
Question: Number of times loop executed
int i= -1;
for (; i++;){
printf(“I am a good Student”);
}
Answer : 1 times

Question: Number of times loop executed


int i= 1;
for (;++i < 3;){
printf(“I am a good Student”);
}
Answer : 2 times
#include <stdio.h>
int main(){
int i=-1;
for(++i;i++;i++)
printf("Name");
return 0;
}
Answer
Other than linear increment

Consider the following loop


int i;
for (i=1;i <= 12;i=i+2){
printf(“I am a good Student”);
}

int i;
for (i=1;i <= n;i=i+2){
printf(“I am a good Student”);
}

n
The number of times the loop executed in n is even
2
n
The number of times the loop executed in n is even  
2
Multiplication factor in increment

int i;
for (i=2;i <= n;i=i*2){
printf(“I am a good Student”);
}
The number of times the loop executed is log n

Question
The number of times loop executed is
#include<stdio.h>
int main(){
int i,count=0;
for (i=1; i<=100; ){
printf("\nI am a good Student");
i = i+3;

}
return 0 ;
}
(A) 33
(B) 34
(C) 100
(D) 10
Answer
B
Sum of n natural Number
What is the output of the program
#include<stdio.h>
int main(){
int i,sum=0;
for (i=1; i<=100;i++){
sum= sum+i;
}
printf("%d",sum);
return 0 ;
}
(A) 100
(B) 5050
(C) 10100
(D) 10000

Wrong progra
What is the output of the program
#include<stdio.h>
int main(){
int i,j,sum=0;
for (i=1; i<=10;i++){
sum= sum+j*j;
j=j+1;
}
printf("%d",sum);
return 0 ;
}
(A) 385
(B) 55
(C) 0100
(D) 10000

Answer
A
Nested loop/ if statement

// C program to illustrate nested-if statement

#include <stdio.h>
int main() {
int i = 10;
printf("Enter the number");
scanf("%d",&i);

if (i > 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement Will only be executed if statement
above is true
if (i < 13)
printf("i is smaller than 13 too\n");
else
printf("i is greater than 13");
}
return 0;
}

Nested for loop

#include <stdio.h>
int main() {
int i,j;
for(i=1;i<5;i++){
for(j=1;j<5;j++){
printf("*");
}
printf("\n");
}
return 0;
}
Output
*****
*****
*****
*****

Mathematical aspect:

n + n + n + n + n + ... + n = n2

Second example
#include <stdio.h>
int main() {
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
Output
*
**
***
****
*****

n(n + 1)
Mathematical Aspect
2

Third Pattern
#include <stdio.h>
int main() {
int i,j;
for(i=1;i<=5;i++){
for(j=i;j<=5;j++){
printf("*");
}
printf("\n");
}
return 0;
}

Output
*****
****
***
**
*

Mathematical Aspect
n(n + 1)
2

Question:

Print the pyramid

Some programming
1, Find exponentiation xn
2. find factorial of a number
3. find number is prime or not
4. print 10 Fibonacci sequence

Answer 1
#include <stdio.h>
int main() {
int x = 2,n=7;
int i;
int power =1;
for(i=1;i<=n;i++){
power = power*x;
}

printf("%d",power);
return 0;
}
Answer 2
#include <stdio.h>
int main() {
int n=5;
int i;
int factorial=1;
for(i=2;i<=n;i++){
factorial = factorial * i;
}
printf("%d",factorial);
return 0;
}
Answer 3
#include <stdio.h>
int main() {
int n=101;
int i;
int flag=0;
for(i=2;i<n;i++){
if(n%i==0){
flag=1;
break;
}
}
if (flag ==0)
printf("Number is prime");
else
printf("Number is not prime");
return 0;
}

Answer 4
Do it by your self
while loop

The while loop loops through a block of code as long as a specified condition
is true:
while (condition) {
// code block to be executed
}
Example

int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}

for loop equivalent to while loop

int i = 0; for(i=0;i<5;i++){

while (i < 5) { printf("%d\n", i);


printf("%d\n", i);
i++; }
}

Question:

GATE 2014

10. Consider the function func shown below:


int func(int num) {

int count = 0;

while (num) {

count++;

num>>= 1;

return (count);

The value returned by func(65)is __________.

Solution:

Ans – 9
Question consider the following program
#include <stdio.h>
int main() {
int i=10;
for (; i<=100; i++);{
printf("\nI am a good Student");
}
printf("%d",i);
return 0 ;

}
The value of I is x and number of print statement is y then value of x+y is

Answer

102

Consider the following program


#include <stdio.h>

int main() {

int i=2+4%6+9/10;

while (i<10){

printf("I am good student");

i++;

return 0 ;

The number of times printf statement executed is _____

Answer

4
GATE 2024 set-1

Consider the following C program:


#include <stdio.h>
int main(){
int a = 6;
int b = 0;
while(a < 10) {
a = a / 12 + 1;
a += b;
}
printf(”%d”, a);
return 0;
}
Which one of the following statements is CORRECT?
(A) The program prints 9 as output
(B) The program prints 10 as output
(C) The program gets stuck in an infinite loop
(D) The program prints 6 as output

Break statement in loop


The break statement in C is used for breaking out of the loop. We can use it with any type of loop to
bring the program control out of the loop. In C, we can use the break statement in the following ways:

• Simple Loops
• Nested Loops
• Infinite Loops
• Switch case

Example
#include <stdio.h>
int main(){
printf("break in for loop\n");
for (int i = 1; i < 5; i++) {
if (i == 5) {
break;
}
else {
printf("%d ", i);
}
}
printf("\nbreak in while loop\n");
int i = 1;
while (i < 20) {
if (i == 5)
break;
else
printf("%d ", i);
i++;
}
return 0;
}

Output
break in for loop
1 2 3 4
break in while loop
1 2 3 4

Example 2
Consider the following program
#include <stdio.h>
for(int i=1;i<=3;i++){
for(int j=1;j<=20;j++){
printf("I am good student\n");
if(i==2) break;
}
}
return 0;
}

The number of times printf executed is ____


Answer: 41
Continue
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Example 1
Consider the following program

#include<stdio.h>

int main () {

for (int i = 1; i <= 10; i++) {

if (i == 5||i==6) {

continue;

printf("%d\n", i);

return 0;

}
Answer : 8
Do while loop
C.8
The do…while in C is a loop statement used to repeat some part of the code till the given condition is
fulfilled. It is a form of an exit-controlled or post-tested loop where the test condition is checked after
executing the body of the loop. Due to this, the statements in the do…while loop will always be executed
at least once no matter what the condition is.
d. 11
Syntax of do…while Loop in C
do {
// body of do-while loop
} while (condition);

Example 1
#include <stdio.h>
int main(){
int i = 0;
do {
printf("I am good Student\n");
i++;
} while (i < 3);
return 0;
}

Number of times printf statement executed is


Explanation:
printf("I am good Student\n");
test the condition : i<3
iteration 2
printf("I am good Student\n");
test the condition : 2<3
iteration 3
printf("I am good Student\n");
test the condition : 3<3

3 times printf will be executed

You might also like