-4
#include<stdio.h>

void fun(z)
{
    printf("%d",z);
}
int main()
{
    int a=5;
    fun(a);
}

This is giving output as 5. Shouldn't it give an error - undeclared variable z ?

Is this a compiler optimization ?

user6837640
  • 82
  • 1
  • 9
Sheldor
  • 198
  • 1
  • 1
  • 10
  • 2
    Ancient C has implicit `int` type. – EOF Sep 17 '16 at 12:09
  • `z` is not 'not declared'. It is a parameter of the function (and its type is implicitly `int`). – Paul Ogilvie Sep 17 '16 at 12:14
  • Never ever write such code! Get a C book about **modern** C (which started 17 years ago with C99). And `z` apparently **is** declared - the prehistoric way. You compiler should warn. If not: **always** enable at least the recommended warnings and pay heed to them. – too honest for this site Sep 17 '16 at 12:25

1 Answers1

1

This is not a compiler optimization, it is a compliance with an ancient C convention that allowed you to skip variable and parameter types when the desired type is int. This convention pre-dates the ANSI standard, and should be avoided even if your compiler is fine with such code.

You will get a warning if you tell the compiler that you want your code to comply with one of more modern standards, say, C99 or C11. The flag is compiler-dependent. If you are using gcc, add

-std=c99

flag to see the warning.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523