Thread: Parse error before 'int'

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    Parse error before 'int'

    The line that the error refers to is:
    Code:
    int a, b, c, d, temp = 1, count = 0, x, y;
    The line that is actually causing the error is the line just before, which is:
    Code:
    update = 0;
    This call is within a procedure (these two lines are in fact the first two lines of the procedure), and the variable is a global boolean (it has to be global, as other procedures use it). I simply cannot see what is wrong.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    i'd guess that update = 0; isn't immediately preceded by a type name. this works.
    Code:
    int update = 0;
    int a, b, c, d, temp = 1, count = 0, x, y;
    and this doesn't, right?
    Code:
    int update;
    update = 0;
    int a, b, c, d, temp = 1, count = 0, x, y;
    in c you have to have all declarations at the start of a block. you can't mix declarations and statements like in c++.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Code:
    update = 0;
    is code. You can't declare variables after code.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In C (1989/90 standard), you are not allowed to declare new variables "after code", so if you have "update = 0; int ...", you are declaring new variables after the first "code" statement, which is not allowed.

    Move the the variable declaration to the beginning of the block (blocks - anything enclosed with braces).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,237
    Quote Originally Posted by thealmightyone
    The line that is actually causing the error is the line just before, which is:
    What compiler are you using, and are you compiling in C90 or C99 mode? In the 1990 edition of the C standard, declarations must come before "actual" code, so this would be wrong:
    Code:
    update = 0;
    int a, b, c, d, temp = 1, count = 0, x, y;
    If this is not the problem, I suggest that you post the smallest and simplest program that demonstrates the error.

    Quote Originally Posted by thealmightyone
    the variable is a global boolean (it has to be global, as other procedures use it)
    Other functions can use the variable, or its value, if you design your functions to have appropriate parameters. Prefer local variables to global variables as it makes it easier to reason about and maintain your program.
    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. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM