GeekInterview.com
Series: Subject: Topic:
Question: 47 of 587

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
Asked by: Interview Candidate | Asked on: Aug 30th, 2011
Showing Answers 1 - 21 of 21 Answers
angel421

Answered On : Sep 9th, 2011

View all answers by angel421

3) 45678910

Yes  1 User has rated as useful.
  
Login to rate this answer.
somgupta

Answered On : Sep 10th, 2011

View all answers by somgupta

Ans is 3---> 4 5 6 7 8 9 10

  
Login to rate this answer.
sultaan

Answered On : Sep 13th, 2011

View all answers by sultaan

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

  
Login to rate this answer.
Rizwan basha

Answered On : Sep 14th, 2011

2) 1 2 3 10

  
Login to rate this answer.
Roshan

Answered On : Sep 15th, 2011

0 1 2 3

  
Login to rate this answer.

456789

  
Login to rate this answer.
rajakrs36

Answered On : Sep 15th, 2011

View all answers by rajakrs36

1 2 3 10

  
Login to rate this answer.
jbode

Answered On : Sep 16th, 2011

View all answers by jbode

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.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
aswinipani

Answered On : Sep 25th, 2011

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

  
Login to rate this answer.
Abhinav kumar

Answered On : 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

  
Login to rate this answer.
abhir

Answered On : Sep 29th, 2011

ans is 456789

  
Login to rate this answer.
lacasoft

Answered On : Sep 30th, 2011

View all answers by lacasoft

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

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Swapnil Jain

Answered On : Oct 6th, 2011

View all answers by Swapnil Jain

4 5 6 7 8 9

  
Login to rate this answer.
gowda

Answered On : Oct 18th, 2011

The ans is 3) 4 5 6 7 8 9

  
Login to rate this answer.
Patil Navanath

Answered On : Jan 31st, 2012

1 2 3 10

  
Login to rate this answer.
udaykiran

Answered On : Feb 8th, 2012

Answer is 4.

  
Login to rate this answer.
Amit Jain

Answered On : Mar 25th, 2012

Answer is 3

  
Login to rate this answer.
rams7

Answered On : May 21st, 2012

View all answers by rams7

4 5 6 7 8 9 10

  
Login to rate this answer.
ANKUR

Answered On : Jun 7th, 2012

45678910

  
Login to rate this answer.
Ravindra Kumar saxena

Answered On : Jul 25th, 2012

45678910

  
Login to rate this answer.
Nupur Boral

Answered On : 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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.