Home > Enterprise >  In C, how does "auto" keyword is used before and after C11?
In C, how does "auto" keyword is used before and after C11?

Time:12-15

i have read, about automatic variables and local variables; i know i can use auto for declare a local variable but i know i just can declare/define a local variable without the auto keyword.

I have read that before C11 the auto keyword is useless and after C11 the auto keyword has now type inference.

Im using C99 and i have notice this version of the standard has no type inference property because if i have this line of code:

...
auto x;
printf("%d",sizeof(x));
...

The output is always (4), even if a initialize "x" with 1.5 for example.

My question is, does auto keyword has a relevant use before C11?

And, after C11, what are the benefits of the "type inference" property of the auto keyword?, where is relevant or important to use auto after C11?

Thanks in advance!!

CodePudding user response:

The auto keyword in C does not mean the same as in C . There is no type inference in C. In C the auto keyword means the compiler decides where the variable should be stored. It's usually ignored by modern compilers.

What you're doing is declaring a variable without a type, so most compilers will default it to int. When you do auto x = 1.5, it's a variable of type (int) that is initialized with the value 1.5 (double), which is truncated to 1.

CodePudding user response:

i have read, about automatic variables and local variables; i know i can use auto for declare a local variable but i know i just can declare/define a local variable without the auto keyword.

Yes.

I have read that before C11 the auto keyword is useless and after C11 the auto keyword has now type inference.

Either your source was wrong or you misunderstood it. There is no type inference in C. You seem to be describing the current meaning of auto in C , but that's a different language.

Im using C99

Use a more recent version of the language if at all you can. The current one is C17, and C23 is anticipated soon. Even compilers that are lagging well behind ought to support at least C11 at this point.

and i have notice this version of the standard has no type inference property because if i have this line of code:

auto x;
printf("%d",sizeof(x));
...

The output is always (4), even if a initialize "x" with 1.5 for example.

That code was valid in C89, but it is invalid in C99 and every version of the language since. It violates a language constraint requiring every declaration to include a type specifier, so your compiler should be emitting a diagnostic about it.

My question is, does auto keyword has a relevant use before C11?

If your compiler nevertheless accepts the program (with or without having emitted the required diagnostic) then it most likely interprets the declaration according to the rules of C89, which specify implicit typing (as int) for variables declared without a type specified. In every version of the C language, the auto means automatic storage duration, which is allowed only for block-scoped variables and is the default for those. auto has nothing to do with data type.

And, after C11, what are the benefits of the "type inference" property of the auto keyword?, where is relevant or important to use auto after C11?

There is no type inference in C11, neither associated with auto nor otherwise. auto has the same meaning in every version of C, and the specifics make that keyword mostly useless: if a valid declaration includes the auto keyword, then removing auto from it does not change its meaning. It does potentially serve to emphasize that declarations indeed have automatic storage duration instead of static storage duration, or that they indeed have no linkage instead of external linkage (and static storage duration), but that's rarely very useful.

  • Related