3

I was studying ANSI C programming language and it says in introduction:

"Local variables are typically "automatic," or created anew with each invocation."

I'm guessing allocating and deallocating variables work with stack frame logic of Java. So are global variables automatic too? They would get in and out of stack frame much less than local variables since they are in the scope of the whole .c file.

Michael
  • 577
  • 1
  • 11
  • 21
  • global variables have a fixed address, they're not automatic. They're allocated at the start of the program and stay that way during the whole execution. – Jean-François Fabre Nov 04 '16 at 21:17
  • No. File-scope objects have static storage duration. – EOF Nov 04 '16 at 21:17
  • 2
    No: variables defined inside a function without the `static` or `register` (or `extern`) keywords are `auto` variables. They could, in theory, be prefixed with the keyword `auto`. You should be shot if you actually add the keyword (witness C++ has completely taken it over for a wholly different purpose) — and if you come across the keyword in production C, you should take the person who wrote the `auto` out behind the shed and make them repent of their evil ways. – Jonathan Leffler Nov 04 '16 at 21:18
  • 2
    Possible duplicate of [using auto variables for global scope](http://stackoverflow.com/questions/20951440/using-auto-variables-for-global-scope) – Jean-François Fabre Nov 04 '16 at 21:18
  • 2
    @JonathanLeffler I like your radical way. Shoot them with the same bullet as the ones who create C-arrays with `new` in C++ :) – Jean-François Fabre Nov 04 '16 at 21:19
  • So are you saying it's illogical to make global variables auto since they are allocated and deallocated only one time during the file's execution? – Michael Nov 04 '16 at 21:24
  • 1
    No; I'm saying it is impossible to create a global variable with the storage class `auto`. The storage class `auto` denies the possibility of the variable being global; a variable being global denies the possibility of it being `auto`. (It's also impossible to create global variables with a storage class of `register` in standard C; `register` denies global and vice versa.) – Jonathan Leffler Nov 04 '16 at 21:25

1 Answers1

2

No, these concepts don't play each other. The term global variable is an informal notion, that refers to variables with external linkage. By definition, the automatic variables have no linkage, hence it does not make sense to have variable, that is both automatic and global.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • It's worthy to say that another difference between globals and locals are that globals are zero-initialized by default, if I'm not wrong, while locals are default initialized, which, for primitive types, implies undefined value. – ABu Nov 04 '16 at 22:23
  • 1
    @Peregring-lk: That's right about globals. The case for locals is more complicated, because there are multiple storage class specifiers. For instance, local variables with `static` keyword are also zero-initialized. OTOH, the automatic variables (those with implicit `auto` specifier) don't have any implicit initialization - they are indeterminate until value is assigned explicitely. – Grzegorz Szpetkowski Nov 04 '16 at 22:37