1
#include <stdio.h>

void foo(auto int i); // line 3

int main()
{
    foo(10);
}

void foo(auto int i) // line 13
{
    printf("%d\n", i );
}

What is wrong in this code? This program is not compiling.

Errors I am seeing are as follows:

main.c:3:27: error: storage class specified for parameter 'i'
     void foo(auto int i);

main.c:13:27: error: storage class specified for parameter 'i'
     void foo(auto int i)
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
  • 2
    What is the error message? – Mad Physicist Feb 03 '14 at 16:56
  • 1
    `auto` is an obsolete qualifier for local variables, not function parameters. – Paul R Feb 03 '14 at 16:57
  • @PaulR why obsolete ? I don't think so. – Zaffy Feb 03 '14 at 16:58
  • 3
    If the code is not compiling, you ***must*** describe the compilation error you saw, including what line number it references. ***VTC*** – abelenky Feb 03 '14 at 16:59
  • @Zaffy: If not obsolete, then at least redundant, which amounts to the same thing in practice. – Paul R Feb 03 '14 at 17:00
  • 1
    Not sure why this question received three down votes, especially after the OP added the error messages when asked for. – Shafik Yaghmour Feb 03 '14 at 17:14
  • My guess would be that the down-votes are most likely for (i) no attempt to research `auto` keyword, (ii) no attempt to interpret fairly self-evident error message. A quick Google search would have turned up all the relevant information without the need to post a question. – Paul R Feb 03 '14 at 17:37
  • Better just not to use `auto` unless you're writing C++ i.e. C++11. C++11 most likely chose that keyword because no one is using it anymore for its original meaning. – Brandin Feb 03 '14 at 19:27

2 Answers2

3

auto is a storage class specifier. This is used for local variables(automatic local variables) You can't put it in the declaration of function parameter.

On compiling you should get the error:

[Error] storage class specified for parameter 'i'  

In C, You are not allowed to put a storage class specifier in the parameter declaration (except register)

haccks
  • 104,019
  • 25
  • 176
  • 264
2

The only storage class specifier that is valid in a parameter declaration is register, we can see this from the draft C99 standard section 6.7.5.3 Function declarators (including prototypes) paragraph 2 which says:

The only storage-class specifier that shall occur in a parameter declaration is register.

this is section 6.7.6.3 in C11.

the storage class specifiers from section 6.7.1 Storage-class specifiers are as follows:

typedef
extern
static
auto
register
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740