Ultra-newbie question

This is a discussion on Ultra-newbie question within the C Programming forums, part of the General Programming Boards category; I'm taking baby steps with C after having been spoiled with Python. Can someone tell me why this gives me ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Ultra-newbie question

    I'm taking baby steps with C after having been spoiled with Python.

    Can someone tell me why this gives me "error C2143: syntax error : missing ';' before 'type'"? The line with the error is the line "int num;".

    Code:
    int main(void)
    {
        printf("Hello");
        int num; 
        return 0;
    }
    It's not supposed to do anything in particular of course - I was just wondering why declaring a variable after the "printf" statement like this is a syntax error. Are you always supposed to declare variables right at the start of a function before anything else? If so, why?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Sharke View Post
    Are you always supposed to declare variables right at the start of a function before anything else?
    For C89, variables must be declared at the start of a block of code (ie. after a {). For C99 and C++, it's not required.

    Quote Originally Posted by Sharke View Post
    If so, why?
    It's a good practice, and keeps your code clean.

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by MacGyver View Post
    For C89, variables must be declared at the start of a block of code (ie. after a {). For C99 and C++, it's not required.



    It's a good practice, and keeps your code clean.
    Understood. I am using Visual C++ for a compiler - does this mean that it's compiling C89? I'm not sure how to set or change this.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes, it's probably trying harder to attain to C89, although unless you turn up the warning levels it will probably let a lot of things slide by. Elysia or someone else more familiar with VC++ would be able to tell you how to change it, depending of course on what version you are using.

    I would recommend keeping your code compliant for both C standards if possible. With that said, if you wish to make your code either C89 or C99 specific, that is fine as long as you pick something as a standard.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    VC++ does not support C99 to my knowledge.
    You'll have to get another compiler or stick to C89.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21