Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

C PROGRAMMIMG NOTES

int i;
int main()
{
    if (i);
    else
        printf("Ëlse");
    return 0;
}

ANS. ELSE BLOCK IS EXECUTED.


Since i is defined globally, it is initialized with default value 0. The
Else block is executed as the expression within if evaluates to
FALSE. Please note that the empty block is equivalent to a semi-
colon(;). So the statements if (i); and if (i) {} are equivalent.

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;

ANS. NOTHING GETS PRINTED.


In a switch block, the control directly flows within the case labels(or dafault label). So,

statements which do not fall within these labels, Outside is not printed. Please take a closer

look at the default label. Its defau1t, not default which s interpreted by the compiler as a

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;

printf("x = %d", x);

return 0;

ANS X=2

Value of x would be 2. Note the semicolon after first if statement. x becomes 0 after the first

if statement. So control goes to else part of second if else statement.

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

A Compile Error due to use of continue in for loop.


B No compile error but it will run into infinite loop printing Geeks.
No compile error and it’ll print GeeksQuiz 10 times followed by Quiz

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?

A Only int can be used in “switch” control expression.


ANS: Both int and char can be used in “switch” control expression.
All types i.e. int, char and float can be used in “switch” control
C expression.
D “switch” control expression can be empty as well.
C Loops & Control Structure    C Quiz - 104    
Discuss it

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

will perform exactly same.

B continue can't be used with all the three loops in C.


After hitting the continue; statement in all the loops, the next expression to
C be executed would be controlling expression (i.e. i < 10) in all the 3 loops.
ANS: None of the above is correct.
C Loops & Control Structure    C Quiz - 105    
Discuss it

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.

A “break” can be used in “for”, “while” and “do-while” loop body.


B “continue” can be used in “for”, “while” and “do-while” loop body.
“break” and “continue” can be used in “for”, “while”, “do-while” loop
C body and “switch” body.
“break” and “continue” can be used in “for”, “while” and “do-while”
D loop body. But only “break” can be used in “switch” body.
“break” and “continue” can be used in “for”, “while” and “do-while” loop
E
body. Besides, “continue” and “break” can be used in “switch” and “if-
else” body.
As per C standard, “continue” can be used in loop body only. “break” can be used in loop body
and switch body only. That’s why correct answer is D.

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.

You might also like