Thread: a syntax error question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    a syntax error question..

    i got this code
    and i checked it many times and i cant see
    why the compiler tells me
    ??

    (57) : error C2143: syntax error : missing ';' before 'type'
    (58) : error C2065: 'i' : undeclared identifier

    Code:
    int main() { 
      int limit; 
      limit=54; 
      
      int i;                                         //line 57
      for(i=1;i<=limit;++i) {            //line58
        print_if_sum_of_abundants(i); 
      }
      return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The error is somewhere else in the code (or you have a broken compiler - but I don't think that's the case - I'm pretty sure it's bad code above the main function. I just compiled a copy-n-paste of your code in gcc-mingw.

    --
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The code looks okay, and this is confirmed when I try to compile. However, I note that the variable i is declared in the middle of a block. This is not allowed in C prior to the 1999 edition of the C standard. However, it is easily fixed with:
    Code:
    int main() { 
      int limit = 54; 
      
      int i;                                         //line 57
      for(i=1;i<=limit;++i) {            //line58
        print_if_sum_of_abundants(i); 
      }
      return 0;
    }
    Now i is declared at the start of the block along with limit.

    EDIT:
    Wait a minute... I'm wrong: the MinGW port of gcc 3.4.5 appears to consider the assignment of 54 to i as an initialisation (and loosely speaking, it is indeed an initialisation), so perhaps I am not clear of the difference between the rules of C and C++. Furthermore, your use of // comments indicates that your code must be C99.
    Last edited by laserlight; 12-08-2008 at 09:25 AM.
    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

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    That's because you're using C89, which requires you to declare all variables first, before you use them.
    Hence doing this, will work:
    Code:
    int limit = 54;
    int i;
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    That's because you're using C89, which requires you to declare all variables first, before you use them.
    Hence doing this, will work:
    Code:
    int limit = 54;
    int i;
    QuantumPete
    Good point!

    --
    Nats
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    Good point!
    Apparently not. Refer to my edit of post #3.
    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
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it works thanks

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    it works thanks
    As in when you change the definition of limit to be a normal initialisation the error disappears? And at the same time you can use // comments? What compiler are you using and are you compiling in C89 or C99 mode?

    EDIT:
    Oh, I discovered that in my haste I forgot the -pedantic option to gcc, hence gcc did not complain about the declaration in the middle of the block despite -std=c89. Nonetheless, a compiler compiling the code as C89 should have rejected the // comments as syntactically invalid, so it is still strange.
    Last edited by laserlight; 12-08-2008 at 09:34 AM.
    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

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont know how to check
    if its C89 or C99

    i am using codeblocks 8.02 and vs2005
    to write my program

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by laserlight View Post
    Nonetheless, a compiler compiling the code as C89 should have rejected the // comments as syntactically invalid, so it is still strange.
    I think the OP just added the // when forming the question, he probably doesn't have a comment for every line number in his code...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM