Declaring an array in the scope of the file size depending on the scope

I learned from my high school

  • Whenever we declare a variable, space will not be reserved for the variable.
  • Whenever we declare a variable, the compiler will not look for other data, such as the definition of a variable.

The same applies to all data types except the array, since it is static in nature, and the size must be specified at the beginning. But I came across two scenarios with an error in one and no errors in the other.

When I declare an array outside main () without size, the code runs smoothly, but as it changes and places it inside main, I get an error, the size of which must be determined.

In both cases, I declare an array that is not used anywhere. Is there a difference in declaring a variable inside main () or outside it? I'm a little confused, and using this eludes me.

example code when declaring outside the main

#include <stdio.h> int a, b, c, d, e; int bal[]; int main(void) { printf("successful"); return 0; } 

error: None


example code when declaring inside the main

 #include <stdio.h> int a, b, c, d, e; int main(void) { int bal[]; return 0; } 

Error:

 prog.c: In function 'main': prog.c:5:5: error: array size missing in 'bal' 
+5
source share

All Articles