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>
extern int 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>
int main(){
    int i,j;
    i=j=2,3;
    while(--i&&j++)
         printf("%d %d",i,j);
    return 0;
}




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.




Hide





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



#include<stdio.h>
int main(){
    static int i;
    for(++i;++i;++i) {
         printf("%d ",i);
         if(i==4) break;
    }
    return 0;
}



Explanation


Output: 24
Explanation:
Default value of static int variable in c is zero. So, initial value of variable i = 0
First iteration:
For loop starts value: ++i i.e. i = 0 + 1 = 1
For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2
Loop incrimination: ++I i.e. i = 2 + 1 =3
Second iteration:
For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4.
Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop.




Hide



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

#include<stdio.h>
int main(){
    int i=1;
    for(i=0;i=-1;i=1) {
         printf("%d ",i);
         if(i!=1) break;
    }
    return 0;
}



Explanation


Output: -1
Explanation:
Initial value of variable i is 1.
First iteration:
For loop initial value: i = 0
For loop condition: i = -1 . Since -1 is non- zero number. So loop condition true. Hence printf function will print value of variable i i.e. -1
Since variable i is not equal to 1. So, if condition is true. Due to break keyword program control will come out of the for loop.




Hide



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

#include<stdio.h>
int main(){
    for(;;) {
         printf("%d ",10);
    }
    return 0;
}



Explanation


Output: Infinite loop
Explanation:
In for loop each part is optional.




Hide



(8)What will be output of following c code?
        
#include<stdio.h>
int r();
int main(){
    for(r();r();r()) {
         printf("%d ",r());
    }
    return 0;
}
int r(){
    int static num=7;
    return num--;
}



Explanation


Output: 5 2
Explanation:
First iteration:
Loop initial value: r() = 7
Loop condition: r() = 6
Since condition is true so printf function will print r() i.e. 5
Loop incrimination: r() = 4
Second iteration:
Loop condition: r() = 3
Since condition is true so printf function will print r() i.e. 2
Loop incrimination: r() = 1
Third iteration:
Loop condition: r() = 0
Since condition is false so program control will come out of the for loop.




Hide



(9)What will be output of following c code?
        
#include<stdio.h>
#define p(a,b) a##b
#define call(x) #x
int main(){
    do{
         int i=15,j=3;
         printf("%d",p(i-+,+j));
    }
    while(*(call(625)+3));
    return 0;
}



Explanation


Output: 11
Explanation:
First iteration:
p(i-+,+j)
=i-++j   // a##b
=i - ++j
=15 – 4
= 11
While condition is : *(call(625)+ 3)
= *(“625” + 3)
Note: # preprocessor operator convert the operand into the string.
=*(It will return the memory address of character ‘\0’)
= ‘\0’
= 0  //ASCII value of character null character
Since loop condition is false so program control will come out of the for loop.




Hide



(10)

#include<stdio.h>
int main(){
    int i;
    for(i=0;i<=5;i++);
    printf("%d",i)
    return 0;
}



Explanation


Output: 6
Explanation:
It possible for loop without any body.




Hide



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

#include<stdio.h>
int i=40;
extern int i;
int main(){
    do{
         printf("%d",i++);
    }
    while(5,4,3,2,1,0);
    return 0;
}



Explanation


Output: 40
Explanation:
Initial value of variable i is 40
First iteration:
printf function will print i++ i.e. 40
do - while condition is : (5,4,3,2,1,0)
Here comma is behaving as operator and it will return 0. So while condition is false hence program control will come out of the for loop.




Hide



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

#include<stdio.h>
char _x_(int,...);
int main(){
    char (*p)(int,...)=&_x_;
    for(;(*p)(0,1,2,3,4); )
         printf("%d",!+2);
    return 0;
}
char _x_(int a,...){
    static i=-1;
    return i+++a;
}



Explanation


Output: 0
Explanation:
In c three continuous dot represents variable number of arguments.
p is the pointer to the function _x_
First iteration of for loop:
Initial value: Nothing // In c it is optional
Loop condition: (*p)(0,1,2,3,4)
= *(&_x_)(0,1,2,3,4)  // p = &_x_
= _x_(0,1,2,3,4)    //* and & always cancel to each other
= return i+++a
= return i+ ++a
= return -1 + 1
= 0
Since condition is false. But printf function will print 0. It is bug of c language.




Hide



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

#include<stdio.h>
int main(){
    int i;
    for(i=10;i<=15;i++){
         while(i){
             do{
                 printf("%d ",1);
                 if(i>>1)
                      continue;
             }while(0);
             break;
         }
    }
    return 0;
}



Explanation


Output: 1 1 1 1 1 1
For loop will execute six times.
Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false.




Hide



(14)How many times this loop will execute?

#include<stdio.h>
int main(){
    char c=125;
    do
         printf("%d ",c);
    while(c++);
    return 0;
}



Explanation


Output: Finite times
Explanation:
If we will increment the char variable c it will increment as:
126,127,-128,-127,126 . . . .   , 3, 2, 1, 0
When variable c = 0 then loop will terminate.    




Hide



(15)What will be output of following c code?
        
#include<stdio.h>
int main(){
    int x=123;
    int i={
         printf("c" "++")
    };
    for(x=0;x<=i;x++){
         printf("%x ",x);
    }
    return 0;
}



Explanation


Output: c++0 1 2 3
Explanation:
First printf function will print: c++ and return 3 to variable i.
For loop will execute three time and printf function will print 0, 1, 2 respectively.




Hide




Looping tutorial in c

33 comments:

  1. Hi, first of all, great page !!!

    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 ;)

    ReplyDelete
    Replies
    1. good Mr. Bartek

      Delete
    2. sanath kumar5/24/14, 12:03 AM

      for Q10 wont the output be syntax error as there is no semicolon to terminate printf statement

      Delete
  2. very good examples and explanations are very clear.
    excellent efforts :)

    ReplyDelete
  3. Hey I really apprecicate ur site....Awesome explanations....This is the first page i hv read and really understood....Kudos to u guys !!
    :)

    ReplyDelete
  4. hey i have one doubt in Q:15
    how does %x will work i tried with %i also it was working could you please elaborate about this.
    thanks in Advance !!
    have a Great Day!!

    ReplyDelete
    Replies
    1. in printf statement
      %x : prints number in hexadecimal format
      %i : prints number in integer format
      %d : prints number in decimal format

      Delete
  5. Yes this all are very important question, and also explanation is very clear and in very nice manner.

    Thanx A Lot !!!!

    ReplyDelete
  6. In question 9

    can you explain macro a##b

    ReplyDelete
  7. can u please explain how the return stmt returns 3 value to i variable in 15th question

    ReplyDelete
    Replies
    1. Return type of prinf function is integer which is total numbers of characters its prints. In this example printf("c" "++") it printing three characters 'c','+' and '+' so it will return 3.

      Delete
  8. hi ritesh u r doing great job..

    this is helping to lot of guys like us

    ReplyDelete
  9. can you explain macro a##b and #x

    ReplyDelete
  10. 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.

    ReplyDelete
  11. 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?

    ReplyDelete
  12. #include
    int main()
    {
    int semf=6000,prof;//semester fees , projected fees
    int i,y=0;

    for(i=1;i<=10;i++)
    {
    semf=(semf+6000*2/100);
    prof=semf;
    printf(" Tution amount for %d semester %d\n",y=y+1,prof);

    }

    getch();
    return 0;
    }

    ReplyDelete
  13. Hi,
    can any one explain how does the following while loop work?
    while(5,4,3,2,1,0)

    ReplyDelete
  14. can any 1 give me solution for 1+1/3+1/5+....+1/n
    plzzzzzzz guys

    ReplyDelete
  15. printf("%d",!+2); pls explain it and what is comma(,) operator in c and how it works in while loop

    ReplyDelete
  16. Is it possible to create a
    C program to find the largest and smallest of n numbes inputed by user using while loop?

    ReplyDelete
  17. the way of explanation and way of writing is so ............great..............

    ReplyDelete
  18. what will be output of following prograam??

    for (m=0; m<3; ++m)
    t=(m%2)? m:m+2;
    printf("%d",t)

    ReplyDelete
  19. anjaneyareddy2/5/14, 8:43 PM

    tanx frnds
    and Excellent

    ReplyDelete
  20. what is the difference of using %d and %i?
    I think both produce same result.
    Please clear my doubt about this.

    ReplyDelete