9

Here I have written my name in main argument declaration but still this program works and did not give any warning.

#include <stdio.h>
int main(Mr32) 
{
    printf("why this works?");
    return 0;
}

Whenever I write anything in place of mr32 , The code still works. I really don't know why this is happening. As per C programming standard this is wrong, right?

Edit : I have tried -Wall but it does not give any warning.

I think here it should be error, because i am not doing as standard C function definition declaration

In c every function definition must follow this format

return-type function_name ( arg_type arg1, ..., arg_type argN ); 

This should also appy to main() right ..??

Okay -Wextra shows warning that mr32 is by default int.

Then why is the default type of any argument in main() an int?

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 3
    Crank up the warning level of your compiler and **mind the warnings** – pmg Sep 27 '11 at 09:03
  • Besides the missing `#include ` gcc does not complain even with `-Wall`. Only with `-Wextra` it complains about `Mr32` defaulting to `int`. The question is more about default main arguments: which spec defines that the type of main defaults to int? – Matteo Sep 27 '11 at 09:16
  • 1
    @pmg: yes `gcc -Wextra` complains. But the question is: why a warning and not an error? Why does it default to `int`? Where and how is this specified? – Matteo Sep 27 '11 at 09:17
  • @ Matteo thanks bro...you got my question.. – Jeegar Patel Sep 27 '11 at 09:19
  • try `gcc -std=c89 -pedantic -Wall -Wmissing-parameter-type -Wold-style-definition`. The 1st warning is included in `-Wextra`; the 2nd one is isolated. – pmg Sep 27 '11 at 09:19
  • 2
    @Matteo: it's specified in 3.5.4.3 of C89, which permits an `identifier-list` instead of a `parameter-type-list`. I'm not sure if the standard explicitly states in normative text that the parameters are then assumed to be `int`, or whether this just follows from the fact that any variable in C89 is by default an int (that is, `auto a;` defines an automatic variable that's an int), but there's an example in 3.7.1 which does mention that `int` is the default for such function parameters. – Steve Jessop Sep 27 '11 at 09:51

3 Answers3

8

In the K&R C definition a parameter without type defaults to int. Your code then corresponds to

int main( int Mr32 ) {
    printf("why this works?");
    return 0;
}

Take a look at this answer for the details: C function syntax, parameter types declared after parameter list

Update

To summarize: in C89 K&R declarations are still supported

  • undeclared parameter types default to int

    void foo( param )
    

    defaults to

    void foo( int param )
    
  • unspecified return types default to int

    foo()
    

    defaults to

    int foo()
    

Note

Although this is supported I would never use it: code should be readable

Community
  • 1
  • 1
Matteo
  • 14,696
  • 9
  • 68
  • 106
  • it seems compiler handles main() as not as normail function.../! – Jeegar Patel Sep 27 '11 at 09:24
  • in normal function defination you can not write like int f00 (mr32){ } – Jeegar Patel Sep 27 '11 at 11:36
  • @Mr.32 Matteo here is telling you that , *yes* you can. (but it will depend on which C standard your compiler follows). – nos Sep 27 '11 at 11:37
  • @Mr.32: K&R style definition applies for every function. You can write `void foo(varname)`. – Matteo Sep 27 '11 at 11:41
  • @Mr.32 in c89, `int foo(bar,baz)` is the same as `int foo(int bar,int baz);` This means if you do not give a type to the function parameters, the parameters are implicitly treated as int. This is a feature carried over from K&R C, which was the "standard" prior to C89. Same for a function return type, if you omit it, it will default to int. – nos Sep 27 '11 at 11:45
  • +1 Matteo i think i got my answer...you should update your answer with some example..! – Jeegar Patel Sep 27 '11 at 11:48
  • @Matteo cool...now i m going to accept your answer..! people generaly dont read comments so now they will get answer by your updates. thanks – Jeegar Patel Sep 27 '11 at 14:33
  • Since you say C89, is this kind of code made obsolete in C99? – Lundin Nov 30 '11 at 11:51
3

Obviously you are using a rather lax compiler. This is what the standards king Comeau makes of it:

Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing.  All rights reserved.
MODE:strict errors C99 

"ComeauTest.c", line 2: error: standard requires that parameter "Mr32" be given a
          type by a subsequent declaration ("int" assumed)
  int main(Mr32) 
           ^

1 error detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
Hit the Back Button to review your code and compile options. 
Compiled with C++0x extensions enabled.

As to what your compiler is doing that's hard to say since you didn't say what your compiler is.


You say you wish to adhere to C89. In that case a parameter with no type information is assumed to have type int. Your main function is interpreted like this:

int main(int Mr32)

Of course this is still not valid C. Valid main functions in C are:

int main(void)
int main(int argc, char* argv[])
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Since this appears to be code for a hosted program, the code is not valid C, unless the specific compiler has documented the behavior of "Mr32".

To have a main() function that accepts other parameters than (void) or (int argc, char *argv[]) is implementation-defined behavior (C99 5.1.2.2.1). So if there isn't any documentation about what "Mr32", is supposed to do, the compiler does not follow the standard. Or more specifically, there needs to be documentation of what the int main(int) syntax is supposed to do on this compiler.

This is true regardless of K&R style function parameters. I believe the standard is the same for C89, C99 as well as all C++ standards.

According to the foot note 9) in the standard, it is acceptable to have another int not named argc, or a typedef equivalent to an int. But in that case there must be a second parameter of type char** as well, which isn't the case here.

Lundin
  • 195,001
  • 40
  • 254
  • 396