Thread: Visual studios and C

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Visual studios and C

    I am able to compile c code fine in visual studios but i cannot declare variables anywhere else but immediately after the int main(void) I can't declare variables somewhere in the middle of the code. Why is that?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because C says so. You can declare variables only at the start of a block (right after a curly-brace and before any actual statements).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    k thanks. never happened to me when i'm using cc compiler on linux machine

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The restriction was relaxed in C99. Modern VS should allow it with the right checkbox checked, I would think, except I don't know which checkbox.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > never happened to me when i'm using cc compiler on linux machine
    It would, if you did say
    cc -ansi prog.c

    It yet again demonstrates the importance of actually learning the language, and not just what your current compiler will let you get away with. The latter approach runs into all sorts of trouble when you change compilers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Salem View Post
    > never happened to me when i'm using cc compiler on linux machine
    It would, if you did say
    cc -ansi prog.c

    It yet again demonstrates the importance of actually learning the language, and not just what your current compiler will let you get away with. The latter approach runs into all sorts of trouble when you change compilers.
    At least on my machine, you need to add -pedantic (even with -Wall). But that's MinGW. [/nitpick]

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Visual Studio does not support C99 AFAIK.
    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