3

Reading this answer which explains the polyglot program on page not found on Stack Overflow I was surprised to read putchar was used because you don't need any #include to use it. This seems to be the case, although en.cppreference.com reference and www.cplusplus.com reference show putchar as defined in the stdio.h header.

How can a function be used (correctly) without having a declaration in C? Or is putchar something inbuilt in compiler (like sizeof operator)?

Community
  • 1
  • 1
bolov
  • 72,283
  • 15
  • 145
  • 224
  • with appropriate warning level set, you should get warning then using undeclared functions. on gcc use `-Wall`. in Visual Studio switch on Warning LEvel 4 in project Settings. for all other compiler RTFM. – vlad_tepesch May 20 '14 at 08:26

2 Answers2

5

In , you can use any function without a declaration.

The compiler then assumes, that the function has a return type of int. The parameters are passed to the function as given. Since there is no function declaration, the compiler cannot verify, if the parameters are correct.

putchar is not builtin into the compiler. However, since

The function call putchar(c) shall be equivalent to putc(c,stdout).

it might be defined as a macro, e.g.

#define putchar(c) putc(c, stdout)

In this case, you must include stdio.h to have the correct definition for putchar.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • what about the parameters? how does the compiler handle them (on a function call without a declaration)? – bolov May 20 '14 at 08:12
  • thank you. One last question: this holds true for `C++` also? – bolov May 20 '14 at 08:18
  • No, for C++ a declaration is required. If you don't have a function declaration, you will get a compilation error. – Olaf Dietsche May 20 '14 at 08:35
  • 1
    C requires a function prototype to be visible. "Implicit int" was removed from the C language 15 years ago. So this answer is outdated. – Lundin May 20 '14 at 08:39
  • As @Lundin wrote, the function declaration is now required. From C99 Rationale: *"The rule for implicit declaration of functions has been removed in C99. The effect is to guarantee the production of a diagnostic that will catch an additional category of programming errors."* – ouah May 20 '14 at 08:45
  • I just tried with `gcc -std=c99 ...` and there's still only a *warning*, not an error. – Olaf Dietsche May 20 '14 at 09:07
  • @OlafDietsche Compile as `gcc -std=c99 -pedantic-errors`, for best standard compliance. – Lundin May 20 '14 at 11:05
2

Some compilers do weird, non-standard things such as automatically including various common headers. It is possible that the code was compiled on one such compiler.

Otherwise, in the old obsolete C90 standard, you didn't need to have a function prototype visible: if you had not, the compiler would start to assume that the return type was int. Which doesn't make any sense. This nonsense was removed from the C language with the C99 standard.

So the reason the code compiled, was because you used a crappy compiler. There are no guarantees that the code will compile/link or work as predicted.

For example:

int main ()
{
  putchar('a');
}

This compiles with gcc as well as gcc -std=c90. But if you compile it as standard C,

gcc -std=c99 -pedantic-errors

you'll get error: implicit declaration of function 'putchar'.

Lundin
  • 195,001
  • 40
  • 254
  • 396