Visual studios and C

This is a discussion on Visual studios and C within the C Programming forums, part of the General Programming Boards category; I am able to compile c code fine in visual studios but i cannot declare variables anywhere else but immediately ...

  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,185
    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,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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

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