1

In the following C code, function printr called only with one parameter, But all compiled with no warning on GCC and VS.
I am confused why this is OK? Thanks!

#include <stdlib.h>
#include <stdio.h>

int printr(i, j){
    printf("The first parameter %d\n", i);
    printf("The second parameter %d\n", j);
    return 0;
}

int main(){
    printr(3);
    return 0;
}
Honghe.Wu
  • 5,899
  • 6
  • 36
  • 47

2 Answers2

3

Gcc does warn you:

$ gcc -Wextra y.c
y.c: In function ‘printr’:
y.c:4: warning: type of ‘i’ defaults to ‘int’
y.c:4: warning: type of ‘j’ defaults to ‘int’

And once you've fixed those it will warn

y.c: In function ‘main’:
y.c:11: error: too few arguments to function ‘printr’
Jens
  • 69,818
  • 15
  • 125
  • 179
2

You defined printr() by using old-fashion function definition syntax, therefore compiler cannot do some syntactic check. You should define it like this:

int printr(int i, int j) {

By the way, with -Wextra, gcc will give warnings about your definition.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47