Thread: what is the output

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    8

    what is the output

    what is the output and how is it?

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    You're the "programmer", you tell me.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    For the file "output" which I've sent the o/p is 4.
    but how is it 4 I dont know.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    for(int i=0;i<=3;i++);
    printf("%d",i);
    getch();
    }
    This would output the numbers 0 to 3.

    You have some problems
    • use of non-standard header, conio.h
    • Use of void main
    • Use of non-standard function, clrscr()

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    can u plz explain me what do u mean by non-standard header, conio.h?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    if we put a semi colon ( at the end of any loop it would go into infinite loop but y it is giving the o/p as the values from 0 to3?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    conio is not part of the c standard and isn't included in many compilers. So your code won't compile on many compilers. This is only a problem if you want to make your code portable or make it available to others or are trying to learn standard, portable c.

  8. #8
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by singer_shant
    if we put a semi colon ( at the end of any loop it would go into infinite loop but y it is giving the o/p as the values from 0 to3?
    Not with a for loop. The for loop will run to its conclusion because it contains the condition which will cause it to cease looping.

    Your statement:
    Code:
    for(int i=0;i<=3;i++);
    is really:
    Code:
    for (int i = 0; i <= 3; i++)
       ;
    which means loop as long as i is less than 3, doing nothing in the loop.

    At the end of the loop, i has already been set to 4, it falls out of the loop because the condition is no longer true, and 4 is output to stdout.

    hope this helps.....

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    thank you fgw_three. I did get u.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    but if the question is like this :


    for(;;


    then what is the output

  11. #11
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by singer_shant
    but if the question is like this :

    Code:
                    for(;;);

    then what is the output
    That is an infinite loop, it won't exit.

    If you need to do something with ";)" in a post, be sure to check "Disable smilies in text" before you post.

  12. #12
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by cwr
    That is an infinite loop, it won't exit.

    If you need to do something with "" in a post, be sure to check "Disable smilies in text" before you post.
    Yep -- that's one way that you can infinite loop with a for.

    I tend to avoid that one, though. While there ARE times that you want to loop infinitely and exit the program from within the loop, I usually use something along the lines of
    Code:
    while (1) {
      /*do something, exit from within here */
    }
    instead of
    Code:
    for (;;) {
      /* do something, exit from wthin here */
    }
    Just habit I guess....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM