开发者

why "int main(anything_you_type)" doesnt produce any error?

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 t开发者_开发问答hink 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?


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


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[])


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜