-3

what is the output of this code?

#include<stdio.h>
//int a;
int main()
{
int a=2;
printf("%d",f1());
}
void f1()
{
int b;
++b;
}

I haven't specified the prototype for f1(), even though its printing output as 0, could some one explain this? And the same output is printing even though function's return type is int

2 Answers2

2

It is called Implicit Function Declaration and it's obsolete. Because there is no declaration for f1() the compiler implicitly declares it. In the implicit declaration the function returns int, so that's why it's behaving as it is however,

  1. It's undefined behavior because f1() does not return an int.
  2. It's an obsolete "feature" and the compiler should be telling you through a warning.
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I've compiled the code with full optimization. The LM content of the function f1 is: `repz retq` ... :) The variable `b` is not initialized, thus it might be any value, but it's not used! – Sir Jo Black Jun 27 '17 at 10:36
  • 1
    @SergioFormiggini ++b is a "use" of b. It is a read of uninitialized value. There are many issues with this code. – Ajay Brahmakshatriya Jun 27 '17 at 10:39
  • 2
    @MohanKrishnac I believe it will if you use proper warning level and `-Werror`, least in gcc. – Sourav Ghosh Jun 27 '17 at 10:47
  • Ok! Yes is used, but I meant to say that the LM code doesn't use it. If we see the code the instruction `b++` is useless because is only increased inside the C function! – Sir Jo Black Jun 27 '17 at 10:47
  • 2
    in linux use `cc -Wall -Werror filename.c` for warning and error – Vishwajeet Vishu Jun 27 '17 at 10:49
  • I'm a little surprised that the compiler isn't yakking outright, since the implicit declaration and definition are different types. – John Bode Jun 27 '17 at 12:28
1

In the 1989 C standard (and earlier pre-standard version of C), a function was implicitly declared by any attempt to call it. The implicit declaration was of the function accepting a variable list of arguments, and returning int.

So, since your printf() statement calls f1() without a preceding declaration of f1(), the compiler assumes it is a function that returns int (and can accept any number of arguments). The behaviour is then undefined, since the function is subsequently defined to return void (i.e. it is not consistent with the implicit declaration).

Under C99 and later, this will normally result in a compilation error. Most C89 compilers accept implicit declarations silently by default, but can be configured (higher warning levels) to give a warning. Some compilers from some vendors (notably gcc) continue to support the implicit declaration even when compiling for later C standards, and are still configured by default to not give a warning.

Note 1: if your compiler accepts an implicit declaration of f1(), it will probably also compile without the #include <stdio.h> - since printf() will also be implicitly declared.

Note 2: Your definition of f1() also has undefined behaviour, since b is uninitialised and of automatic storage duration. Accessing its value, let alone incrementing it, therefore gives undefined behaviour.

Peter
  • 35,646
  • 4
  • 32
  • 74