Recently I went through some code similar to this: (The code is proprietary, and hence adding a similar one)
#include<stdio.h>
void test_it(var)
{
printf("%d\n",var);
}
int main()
{
test_it(67);
return 0;
}
The arguments of test_it
do not have datatype mentioned.
I compiled it as gcc test_it.c
... : Surprisingly No Warnings/Error
Again I compiled using: gcc -Wall test_it.c
... : No Warnings/Error yet again
(Getting more aggressive now...)
I compiled it again using: gcc -Wall -Wextra test_it.c
... :
warning: type of 'var' defaults to 'int'
finally I got the warning.
I tried using multiple arguments as:
void test_it(var1, var2)
{
printf("%d\n%d\n",var1, var2);
}
int main()
{
test_it(67,76);
return 0;
}
Same beahavior!!
Also I tried this:
void test_it(var)
{
printf("%d\n",var);
}
main() // Notice that no `int` there
{
test_it(67);
return 0;
}
This code gave warning with -Wall
option only.
So my question is why the int
datatype is not mandatory for function arguments in function definition?
EDIT:
Rewording the question:
Why gcc
doesn't give warning with -Wall
in the case of omitting datatype of function arguments, but gives warning for omitting the function return type? Why does it ignore it in the first case?
By default the functions arguements are of type int
in C89. So the code executes fine.
You can go through this related question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.