Thread: why can I get this simple code to compile?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    why can I get this simple code to compile?

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("s");
    
        struct nein
        {
            int a;
        };        //<-the compiler thinks the code ends here.
    }
    it gives this error when I compile with gcc, why? It works without the printf statement though.

    bash$ gcc -W -Wall -pedantic -ansii -o typesize typesize.c
    typesize.c: In function `main':
    typesize.c:7: parse error before `struct'
    typesize.c:10: warning: control reaches end of non-void function
    typesize.c: At top level:
    typesize.c:10: warning: ANSI C does not allow extra `;' outside of a function
    typesize.c:11: parse error before `}'

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Actually I don't know... it compiles fine for me.

    Maybe cause you have the struct inside of main.
    Last edited by SlyMaelstrom; 11-10-2005 at 12:57 AM.
    Sent from my iPad®

  3. #3
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > warning: ANSI C does not allow extra `;' outside of a function
    Your posted code does NOT have this.
    Try posting the code you actually compiled, not some randomness.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    I know, that is why I don't get it, it is the code I am trying to compile! the ; refers to the ; for the struct declaration. If you count the lines from the top that ; is in line ten and the line which the compiler refers to. Am I using gcc wrong? Thats the only thing I can think of now.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Try putting the struct declaration at the start of main, instead of after the printf statement.

    When I compiled this code with gcc, I got:
    Code:
    $ gcc -Wall -pedantic -ansi tarotcard.c -o tarotcard
    tarotcard.c: In function ‘main’:
    tarotcard.c:10: warning: ISO C90 forbids mixed declarations and code
    tarotcard.c:10: error: syntax error before ‘/’ token
    tarotcard.c:11: warning: control reaches end of non-void function
    To get rid of those warnings, you would need to remove the '//' style comment, move the struct declaration to the start of main instead of after the printf, and add a return 0; statement to the end of main. Not sure why your gcc is giving different output to mine, but hopefully this helps.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    Well it works when I move it out side the main. but why doesn't it work inside the main? As I mentioned it works when I remove the printf statement...... do you think there is a problem with my gcc?

    And thank everyone for the fast replies, this forum is really good!

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Did you try moving it to the start of main? If you remove the printf it would then be at the start, this is probably why it works when you do that. Also it will work when it's outside of main because that's allowed by C89. What isn't allowed is declaring a struct anywhere in the code - it must be global, i.e. outside all functions, or at the start of a block.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    Ah so thats why. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Errors in my Code. Can anyone help?
    By DGLaurynP in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 09:36 AM
  2. Why the simple code doesn't compile?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2008, 03:28 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Replies: 1
    Last Post: 01-29-2002, 02:49 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM