Search code examples
ctype-inferenceauto

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


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!!


Solution

  • 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.