Output of the C program

What is the output of the following code?
Code
  1.  

  2. #include<stdio.h>

  3. void main()

  4. {

  5.    int s=0;

  6.    while(s++<10)

  7.    {

  8.       if(s<4 && s<9)

  9.          continue;

  10.       printf("

  11. %d      ",s);

  12.    }

  13. }

  14.  
Copyright GeekInterview.com


1) 1 2 3 4 5 6 7 8 9

2) 1 2 3 10

3) 4 5 6 7 8 9 10

4) 4 5 6 7 8 9

Showing Answers 1 - 63 of 63 Answers

Rizwan basha

  • Sep 14th, 2011
 

2) 1 2 3 10

  Was this answer useful?  Yes

Roshan

  • Sep 15th, 2011
 

0 1 2 3

  Was this answer useful?  Yes

Clever.

Answer is 3) 4 5 6 7 8 9 10

Reason:

The result of the expression s++ is the value of s *before* the increment, so the expression (s++ < 10) operates on the values 0 through 9.

In the body of the loop, s has been incremented, so the expression (s < 4 && s < 9) operates on the values 1 through 10. When s is between 1 and 3, the continue statement is executed and the loop repeats from the beginning, skipping the printf. So only the values 4 through 10 are written to standard output.

Nits: unless your compiler documentation *explicitly* lists void main() as a legal signature, use int main(void) instead.

aswinipani

  • Sep 25th, 2011
 

ans is (3)- 4 5 6 7 8 9 10

  Was this answer useful?  Yes

Abhinav kumar

  • Sep 28th, 2011
 

The result of the expression s++ is the value of s *before* the increment, so the expression (s++ < 10) operates on the values 0 through 9.


So Ans is 45678910

  Was this answer useful?  Yes

abhir

  • Sep 29th, 2011
 

ans is 456789

  Was this answer useful?  Yes

lacasoft

  • Sep 30th, 2011
 

Just to make things clear:

Code
  1.  

  2. #include void main()

  3. {

  4. int s=0;

  5. while(s++<10)

  6. {

  7. if(s<4 && s<9) continue;

  8. printf(" %d     ",s);

  9. }

  10. }

  11.  



This will not be compiled. #include wants some file name...

gowda

  • Oct 18th, 2011
 

The ans is 3) 4 5 6 7 8 9

  Was this answer useful?  Yes

Patil Navanath

  • Jan 31st, 2012
 

1 2 3 10

  Was this answer useful?  Yes

udaykiran

  • Feb 8th, 2012
 

Answer is 4.

  Was this answer useful?  Yes

Amit Jain

  • Mar 25th, 2012
 

Answer is 3

  Was this answer useful?  Yes

ANKUR

  • Jun 7th, 2012
 

45678910

  Was this answer useful?  Yes

Ravindra Kumar saxena

  • Jul 25th, 2012
 

45678910

  Was this answer useful?  Yes

Nupur Boral

  • Aug 16th, 2012
 

Ans:3 s++ means-first s=0 then s=s+1.so checking the initial value of s & then increment the value of s.In this way continuing this process & print the value of s.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions