Thread: "while" question

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    "while" question

    Code:
    # include <stdio.h>
    
    main()
    
    {
    	int c, n1;
            n1 = 0;
            while (( c = getchar()) != EOF)
                  if ( c == '\n')
                           ++n1;
            printf("%d\n", n1);
    }
    ** it returns**
    % gcc while.c -o while
    % while
    %while: too few arguments.

    however when i do this:

    % gcc while.c
    % a.out

    the programs runs why?

  2. #2
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    I bet you're using a Unix shell of some sort, in which 'while' is a shell command.

    If you want to run your 'while', try ./while
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    in an ironic twist, there is something wrong with your number of arguments as well: they're undefined. Main definition should be:
    Code:
    int main(void)

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There was a thread here a while ago where someone (MacGyver, I think) showed that int main() is actually better than int main(void).

    In any case, the implicit int rule is deprecated in newer versions of C, and so the OP should use int. Returning zero is probably a good idea too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Don't name your programs after other commonly used words (like "while"), and do NOT put "." in your path. To run a program in the current directory you should always run it as "./program"

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you do put "." into your path, it might make it easier to run programs -- you don't have to type "./" all of the time. But, of course, it causes problems when programs are named after shell commands. It also creates a security risk: if someone put a program called ls in a directory which did something completely different from what ls is supposed to do, you'd run that ls instead of /bin/ls if you just typed "ls".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by dwks View Post
    There was a thread here a while ago where someone (MacGyver, I think) showed that int main() is actually better than int main(void).
    Yikes, no!

    From the FAQ:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    The only versions of main() that has the blessing of the C standard for both C89/C90 and C99 is this:

    Code:
    int main(void);
    int main(int argc, char *argv[]);
    Unfortunately, I do not believe the C++ standard allows void to be a type of C++ that main() can accept, because C and C++ have totally different meaning with regard empty arguments in a function. C takes an empty argument function to mean that the function has an unknown number of arguments and C++ takes it to mean that the function has no arguments.

    Hence the only true versions of main() in C++ is:

    Code:
    int main();
    int main(int argc, char *argv[]);
    So hence, the only version of main() which I think fits for both C89/C90, C99, and C++ is this:

    Code:
    int main(int argc, char *argv[]);
    Now someone contradicted me the last time I stated this, but I don't remember the particulars. If someone has something definite on any of this, that would be great.

    Since compilers are usually pretty loose with regard to main()'s return type and parameters, some will, no doubt, feel perfectly fine with arbitrary declarations of main().

    Quote Originally Posted by dwks View Post
    In any case, the implicit int rule is deprecated in newer versions of C, and so the OP should use int. Returning zero is probably a good idea too.
    Agreed.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    There was a thread here a while ago where someone (MacGyver, I think) showed that int main() is actually better than int main(void).
    It was me. int main() has a slight advantage in environments where the "typical" C calling convention of caller-cleans is not used. It's a fairly obscure argument. If you asked me for my personal opinion I'd say I don't think it matters too much. Getting the return type correct is more immediately important.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    It also creates a security risk: if someone put a program called ls in a directory which did something completely different from what ls is supposed to do, you'd run that ls instead of /bin/ls if you just typed "ls".
    That's a strong enough reason to never use it, IMHO. Since "." is not a fixed location, it's like having a wildcard in your path, which obviously can lead to all sorts of awful things.

  10. #10
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by dwks View Post
    There was a thread here a while ago where someone (MacGyver, I think) showed that int main() is actually better than int main(void).
    Using obsolescent practices is not better, hackish at best, but never better.
    Quote Originally Posted by ISO 9899 6.11.6
    The use of function declarators with empty parentheses (not prototype-format parameter
    type declarators) is an obsolescent feature.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    It was me. int main() has a slight advantage in environments where the "typical" C calling convention of caller-cleans is not used. It's a fairly obscure argument. If you asked me for my personal opinion I'd say I don't think it matters too much. Getting the return type correct is more immediately important.
    What system would not have main() declared as using the stdcall convention, or not provide some means to allow main() to work just as gracefully using some other convention?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Here's the "int main()" vs. "int main(void)" thread. I'll have to bookmark this since it keeps coming up.

    http://cboard.cprogramming.com/showthread.php?t=88714

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    What system would not have main() declared as using the stdcall convention, or not provide some means to allow main() to work just as gracefully using some other convention?
    Actually, it's __stdcall that causes the problem. In that convention the callee cleans the stack, which means that the called function has to know certainly what arguments ARE on the stack, so it can clean them. If you declare main(void), this implies there will be no arguments on the stack, even though the runtime DOES push argc and argv there. So as main returns it corrupts the stack.

    Again, you don't normally declare main() as __stdcall so I said this argument is obscure. The standard in fact does state that "int main(void)" is okay, so...

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Indeed, perhaps I should have though a little more when I wrote my last post. IMO, main() should always be stdcall or some equivalent convention where the caller is doing the cleaning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM