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?
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?
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).
k thanks. never happened to me when i'm using cc compiler on linux machine
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.
> 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.