Thread: 5 lines of code and already error?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    5 lines of code and already error?

    Code:
    #include <stdio.h>
    int main();
    
    
      {   printf("Enter desired grade");
         char grade;
         scanf("%c", &grade);
         return 0;
    }



    error expected a identifier or '(' before '{'
    (I have no idea whats even wrong with this)
    Last edited by tmac619619; 10-08-2012 at 08:36 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    scanf("%c", &grade);
    You're missing a comma in there.
    And it's "int main(void)"
    And be sure to "return 0" at the end of the program.

    [EDIT] Oh, and no semi-colon after "main()"!

    Code:
    int main();
    Last edited by Matticus; 10-08-2012 at 08:36 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    I noticed that i solved the problem as soon as i removed the ; from int main();

    Why?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by tmac619619 View Post
    I noticed that i solved the problem as soon as i removed the ; from int main();

    Why?
    The braces are multi-line block everywhere or nearly everywhere you can use {} can instead be a single line block as in just a single ";".
    This resulted in a main function that was very short.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    I'm sorry i'm very newb at this lingo.

    int main ();

    int main()

    whats the difference? I know the ';' effectively "concludes" a statement or command.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by tmac619619
    I noticed that i solved the problem as soon as i removed the ; from int main();

    Why?
    With the semi-colon there, you declared, but did not define (implement) the main function. Later on, you will find that this becomes useful when you have many functions, some of which call others. You can then declare all of them near the top and not have to worry about which order you define them.

    Anyway, the point is that since that is only a declaration of main, you end up with a block of code in the middle of nowhere, instead of being the body of the main function.

    Quote Originally Posted by stahta01
    Code:
    The braces are multi-line block everywhere or nearly everywhere you can use {} can instead be a single line block as in just a single ";".
    This explanation does not apply in this case since substituting the braces with a semi-colon changes the meaning of the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by tmac619619 View Post
    I'm sorry i'm very newb at this lingo.

    int main ();

    int main()

    whats the difference? I know the ';' effectively "concludes" a statement or command.
    You can consider

    Code:
    int main ();
    I am wrong; the semi colon in this case makes it a function prototype.
    Code:
    int main (){;}
    This below is a function with a very small function body.

    Code:
    int main ()
    {
        ;
    }
    You are going to have to wait for a person who can explain better that I for a clearer answer.

    Use laserlight Answer; she is the expert here on C++ and better on C than me.

    Tim S.
    Last edited by stahta01; 10-08-2012 at 08:56 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by stahta01
    You can consider
    Code:
    int main ();
    To be exactly the same as
    Code:
    int main (){;}
    No, the former is a declaration of the main function that declares that the main function returns an int and takes an unknown number of arguments. The latter is a definition of the main function that declares that the main function returns an int and takes no arguments, then implements the main function to do nothing (besides implicitly return 0, in the case of C99 and later).

    The former does not constitute a complete program; the latter is a complete (though not particularly useful) program. They are definitely not equivalent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple lines of code
    By BrianAshmore in forum C Programming
    Replies: 2
    Last Post: 07-17-2012, 07:07 AM
  2. Replies: 8
    Last Post: 06-17-2012, 01:56 PM
  3. Lines of code per day.
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-26-2007, 09:20 AM
  4. Me and my 2.8+ million lines of code
    By Noobie in forum C++ Programming
    Replies: 27
    Last Post: 05-14-2003, 07:58 AM
  5. What do these tow lines of code mean?
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2002, 04:30 PM