INDEX

Looping questions in c and answers

Looping questions and answers with explanation for written test exam
and interview in c programming language

(1)

What will be output of following
c code?

#include<stdio.h>

externint x;

int main(){

do{

do{

printf("%o",x);

}

while(!-2);

}

while(0);

return 0;

}

int x=8;

Explanation

Output: 10

Explanation:

Here variable x is extern type. So
it will search the definition of variable x. which is present at the end of the
code. So value of variable x =8

There are two do-while loops in the
above code. AS we know do-while executes
at least one time even that condition is false.
So program control will reach at
printf statement at it will print octal number 10 which is equal to decimal
number 8.

Note: %o is used to print the
number in octal format.

In inner do- while loop while
condition is ! -2 = 0

In C zero means false. Hence program control will come out of the
inner do-while loop. In outer do-while loop while condition is 0.
That is again false. So program control will also come out of the outer
do-while loop.

Hide

(2)

What will be output of following
c code?

#include<stdio.h>

int main(){

int i=2,j=2;

while(i+1?--i:j++)

printf("%d",i);

return 0;

}

Explanation

Output: 1

Explanation:

Consider the while loop condition: i + 1 ? -- i : ++j

In first iteration:

i + 1 = 3 (True)

So ternary operator will return -–i i.e. 1

In c 1 means true so while
condition is true. Hence printf statement will print 1

In second iteration:

i+ 1 = 2 (True)

So ternary operator will return -–i i.e. 0

In c zero means false so while
condition is false. Hence program control will come out of the while loop.

Hide

(3)

What will be output of following
c code?

#include<stdio.h>

int main(){

int x=011,i;

for(i=0;i<x;i+=3){

printf("Start ");

continue;

printf("End");

}

return 0;

}

Explanation

Output: Start Start Start

Explantion:

011 is octal number. Its equivalent decimal value is 9.

So, x = 9

First iteration:

i = 0

i < x i.e. 0 < 9
i.e. if loop condition is true.

Hence printf statement will print: Start

Due to continue keyword program
control will come at the beginning of the for loop and value of variable i will
be:

i += 3

i = i + 3 = 3

Second iteration:

i = 3

i < x i.e. 3 < 9 i.e. if loop condition is true.

Hence printf statement will print: Start

Due to continue keyword program
control will come at the beginning of the for loop and value of variable i will
be:

i += 3

i = i + 3 = 6

Third iteration:

i = 3

i < x i.e. 6 < 9 i.e. if loop condition is true.

Hence printf statement will print: Start

Due to continue keyword program
control will come at the beginning of the for loop and value of variable i will
be:

i += 3

i = i + 3 = 9

fourth iteration:

i = 6

i < x i.e. 9 < 9 i.e. if loop condition is false.

Hence program control will come out of the for loop.

Hide

(4)What will be output of
following c code?

#include<stdio.h>

intmain(){

inti,j;

i=j=2,3;

while(--i&&j++)

printf("%d %d",i,j);

return0;

}

Explanation

Output: 13

Explanation:

Initial value of variable

i = 2

j = 2

Consider the while condition : --i && j++

In first iteration:

--i && j++

= 1 && 2 //In c
any non-zero number represents true.

= 1 (True)

So
while loop condition is true. Hence printf function will print value of i = 1
and j = 3 (Due to post increment operator)

In second iteration:

--i && j++

= 0 && 3 //In c zero represents false

= 0 //False

So
while loop condition is false. Hence program control will come out of the for
loop.

34 comments:

Now, to the point :). I must argue with You about Q12. The output is correct but the explanation is quite wrong. printf will print 0 (!+2 = 0) becouse there will be one iteration of 'for' loop, consider following fragment:= return i+++a= return i++ + a (NOT i + ++a !!!)= return -1 + 0 (and after that i incremented)= -1 (so the loop condition is TRUE)Now i = 0 and condition in second iteration is FALSE (becouse return 0 + 0) and program terminates.So ,there's no such bug in C language ;)

First: Really it is a great page and it benefit me alot to fresh my memory.For Q.12: There is not any bug.but the compiler evaluated i+++a as following: i++ + a.So, we have (-1)++ + 0 = -1 .... true and printf() statement is executed.

i+++a is evaluated as i++ + a.

That is due to compiler really do not ignore all white spaces.

for the case: i+++a: it handle the variable i then it looks for the longest possible operator.we have 2 options: + , ++ / it cannot consider +++ as an operator as there is not an operator (+++)The longest one is ++.then the remained operator is +. and then handle the next variable.

To make idea clear:consider we have:i = -1;a =0;z = i+++++a;

Now the compiler will parse it: i++ ++ + a. and it will bring a compilation error. (Lvalue required as increment operand).

If we modified the expression and added some white space it will work.z = i++ + ++a;it works and result will be zero.

At one college, the tuition for a full-time student is $6,000 per semester. It has been announced that the tuition will increase by 2% each year for the next 5 years. Design a program with a loop that displays the projected semesters tuition amount for the next five years. how would this be done?