0
#include<stdio.h>
void main()
{
    int a=100;
    printf("%d",a);
    int b=200;  //Error
    printf("%d",b);
}

I know that declaring b there causes error, but I want to know why?

I would also like to let you guys know that I've compiled this program using Turbo C++ 4.0 by Editor on Windows And the error that I got is "Declaration Not Allowed Here".

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63

1 Answers1

6

This is very dependent on the compiler, or rather which version of C it implements.

Before the C99 standard, declarations could only be placed before other statements. You could not have declarations in between other statements.

That was changed in the C99 standard, and since then you can have declarations anywhere.

If your compiler gives an error for the code you show, it's probably very old.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for the heads up. When I compiled this on my Ubuntu it ran successfully, All I wanted to know is why is that error is occurring when compiled with those old version compilers and why were those declarations should be given at starting line only according to the old compilers in order to run it good. Im sorry for those who feel my question is silly. – Harish Regada Sep 14 '18 at 17:14
  • 1
    @HarishRegada the declaration `-std=c90` mentioned by kiran Biradar is to force the modern gcc compiler to use the old C standard, in which case the error is produced. It's not for Turbo C which still uses the old standard. I hope the duplicate answer helps you. – Weather Vane Sep 14 '18 at 18:01
  • @WeatherVane Thanks for the help, I've already got a great answer from seeing the original post. Anyway, thanks man – Harish Regada Sep 14 '18 at 18:06
  • 2
    Turbo C is very old. It is sad that anyone learning C in the 21st Century is forced to use Turbo C. – Jonathan Leffler Sep 14 '18 at 19:59