M.S Chaudhari M.S Chaudhari - 3 months ago 16
C Question

Issue with auto variable

I know about storage classes in C. I am wondering about the

auto
keyword.

In the following expression,

auto int i = 0;


auto
is the storage class specifier,
int
is the data type.

But if we write:

int auto i = 0;


What happens? Is
auto
behaving as the data type?

Answer

The ordering of storage class specifiers (extern, static, register, auto, typedef) with respect to type specifiers (int, double, char, etc.) does not matter. Both are valid and declare the same object.

It is, however, considered a good practice to conform to a certain order. The list of specifiers which precedes the declarator consists of distinctive groups of keywords which are best used in the following order:

  • Storage class specifiers, if any: static, extern, auto, register, typedef;
  • Type qualifiers, if any: const, volatile, restrict;
  • Type modifiers, if any: signed, unsigned, short, long;
  • The type specifier: int, double, char, etc.

Following that rule would lead to more readable declarations like this:

static const volatile unsigned long int a;

Instead of (an extreme example):

unsigned volatile int static const long a;