Thread: Parse Error, expecting `'}''

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    Question Parse Error, expecting `'}''

    Hi there

    the beginning of my program reads:

    #include <stdio.h>
    #include <math.h>
    main()
    {
    int Ca; Ca = 998;
    int Cm; Cm = 1617;

    and the compiler tells me:

    line 6: Parse Error, expecting `'}''
    'int Cm'
    aborting compile

    what is wrong with line 6?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     main()
    {
    int Ca; Ca = 998;
    int Cm; Cm = 1617;
    
    and the compiler tells me:
    Multiple problems:

    1) Main should take this form:

    int main( void )

    Or this form:

    int main( int argc, char *argv[] )

    2) Assign your variables in the declaration, or after all variables have been declared:

    int Ca = 998;
    int Cm = 1617;

    Or the second method:

    int Ca;
    int Cm;
    Ca = 998;
    Cm = 1617;

    3) You have no closing braces
    Code:
    }
    4) Main returns an int, so it should actually end up as:
    Code:
        return 0;
    }
    Have fun.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>1) Main should take this form:
    >>int main( void )
    >>Or this form:
    >>int main( int argc, char *argv[] )

    Not necessarily. main( ) is legal in C, but not in C++.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by XSquared
    Not necessarily. main( ) is legal in C, but not in C++.
    Well technicly, this works also:

    int main( int argc, char**argv )

    However, according to the standard, unless it has changed in C99, the two forms I listed are what the standard defines.
    5.1.2.2.1 Program Startup
    ....
    It shall be defined with a return type of int and with no paramegers:

    int main( void ) { /* ... */ }

    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

    int main( int argc, char *argv[]) { /* ... */ }

    or equivalent) or in some other implementation-defined manner.
    So if you want the standard, you use the equivelant of what I listed; anything else is non-standard, implementation specific.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Whoops. I was wrong.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    28
    >>int main( int argc, char *argv[] )

    ok this is one of the standard, my question is that why do we pass two arguments to the main function, one as an int and one as a pointer to a char array. What are some of their use in the program?

  7. #7
    .........
    Join Date
    Nov 2002
    Posts
    303
    argc == arguement count
    It counts the arguements at the command line.
    Like say to run your program you type
    ./program then argc would be 1 in your program, if you typed say ./program something then argc would be 2

    argv is an array of pointers to the strings you used at the command line. argv stands for arguement vector. So if you typed ./program something argv[0] == "./program" and argv[1] == "something".

    You use them so your program can accept command line arguements. That's how programs like cp, ls, etc take arguements at the command line.

    Almost forgot, this is a good read, it explains it well and you should have a solid understanding of command line arguements if you work through it and understand all the code presented. It's worth it, goodluck.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Last edited by SourceCode; 06-13-2003 at 11:39 PM.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    28
    thanks. I understand it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM