Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 810
Jeegar Patel Avatar asked Sep 27 '11 09:09

Jeegar Patel


1 Answers

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

like image 136
Matteo Avatar answered Sep 27 '22 18:09

Matteo