Why is `int` datatype not mandatory to be mentioned for function arguments in the function definition?

StackOverflow https://stackoverflow.com/questions/23384834

  •  12-07-2023
  •  | 
  •  

Question

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?

Was it helpful?

Solution

In C89, the default type is assumed to be int. This (is valid in C89), however the default type rule has been abandoned in C99. See the difference:

C89 - Compiles fine

C99 prog.c:3:6: error: type of ‘var’ defaults to ‘int’

Try to compile with the -std=c99 flag.

OTHER TIPS

By default the functions arguements are of type int in C89. So the code executes fine.
You can go through this related question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top