C Programmimg Notes
C Programmimg Notes
int i;
int main()
{
if (i);
else
printf("Ëlse");
return 0;
}
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
printf ("%dn", no);
return 0;
}
ANS..RUNTIME ERROR
There is a bug in the above program. It goes inside the do-while loop for c = 0 also. So it fails
during runtime.
#include<stdio.h>
int main()
{
int i = 0;
for (printf("1stn"); i < 2 && printf("2ndn"); ++i && printf("3rdn"))
{
printf("*n");
}
return 0;
}
ANS:
1st
2nd
*
3rd
2nd
*
3rd
It is just one by one execution of statements in for loop. a) The initial statement is executed only
once. b) The second condition is printed before '*' is printed. The second statement also has short
circuiting logical && operator which prints the second part only if 'i' is smaller than 2 b) The
third statement is printed after '*' is printed. This also has short circuiting logical && operator
which prints the second part only if '++i' is not zero.
#include <stdio.h>
int main()
{
int i = 3;
switch(i)
{
printf("Outside ");
case 1: printf("Geeks");
break;
case 2: printf("Quiz");
break;
defau1t: printf("GeeksQuiz");
}
return 0;
statements which do not fall within these labels, Outside is not printed. Please take a closer
label used for goto statements. Hence, nothing is printed in the above program.
#include <stdio.h>
int main()
int x = 3;
if (x == 2); x = 0;
if (x == 3) x++;
else x += 2;
return 0;
ANS X=2
Value of x would be 2. Note the semicolon after first if statement. x becomes 0 after the first
What’s going to happen when we compile and run the following C program?
#include "stdio.h"
int main()
{
int j = 0;
for ( ; j < 10 ; )
{
if (j < 10)
printf("Geeks", j++);
else
continue;
printf(“Quiz”);
}
return 0;
}
Run on IDE
once.
ANS: No compile error and it’ll print GeeksQuiz 10 times.
C Loops & Control Structure C Quiz - 104
Discuss it
Question 27 Explanation:
Here, initialization of j has been done outside for loop. if condition serves as control
statement and prints GeeksQuiz 10 times due to two printfs. Please note
that continue comes in picture when j becomes 10. At that time, second printf gets
skipped and second expression in for is checked and it fails. Due to this, for loop
ends.
Question 28
CORRECT
Which of the following statement is correct for switch controlling expression?
Question 28 Explanation:
As per C standard, “The controlling expression of a switch statement shall have
integer type.” Since char is prompted to integer in switch control expression, it’s
allowed but float isn’t promoted. That’s why B is correct statement.
Question 29
WRONG
Choose the best statement with respect to following three program snippets.
/*Program Snippet 1 with for loop*/
for (i = 0; i < 10; i++)
{
/*statement1*/
continue;
/*statement2*/
}
/*Program Snippet 2 with while loop*/
i = 0;
while (i < 10)
{
/*statement1*/
continue;
/*statement2*/
i++;
}
/*Program Snippet 3 with do-while loop*/
i = 0;
do
{
/*statement1*/
continue;
/*statement2*/
i++;
}while (i < 10);
Run on IDE
All the loops are equivalent i.e. any of the three can be chosen and they all
Question 29 Explanation:
First and foremost, continue can be used in any of the 3 loops in C. In case of “for” loop, when
continue is hit, the next expression to be executed would be i++ followed by controlling
expression (i.e. i < 10). In case of “while” loop, when continue is hit, the next expression to be
executed would be controlling expression (i.e. i < 10). In case of “do-while” loop, when
continue is hit, the next expression to be executed would be controlling expression (i.e. i < 10).
That’s why “while” and “do-while” loops would behave exactly same but not the “for” loop.
Just to re-iterate, i++ would be executed in “for” loop when continue is hit.
Question 30
In the context of "break" and "continue" statements in C, pick the best statement.
In "switch" body, two "case" can't result in same value. Though having only "case" or only
"default" is okay. In fact, "switch" body can be empty also.
NOTE:
In loop jamming, the bodies of the two loops are merged together to form a single
loop provided that they do not make any references to each other.
In loop concatenation, the bodies of the two loops are concatenated together to form
a series of loop, loop concatenation, some times help to reduce complexity.
In loop unrolling, we try to optimize a program's execution speed. It is also known
as space–time tradeoff.
In strength reduction compiler optimize expensive operations with equivalent but
less expensive operations.