0

I am testing the following code in Code::Blocks v 12.11:

#include <stdio.h>

int main()
{
    display();
    return 0;
}

void display()
{
    printf("\nHi");
}

It compiles successfully and runs well.I am unable to understand why? My queries are as follows:

  1. A function or variable needs to be at least declared before getting used in C/C++. Here,there is no declaration before we called the function display().

  2. By default,all functions have return type as int.So,I am expecting a compilation error here but it went through successfully.The compiler would have assumed display() to be int display() and then we defined it as void display().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
akjlab
  • 170
  • 6
  • you are right a function should be declared before in c otherwise it will through an error. – R R Nov 14 '13 at 05:00
  • 1
    Your second point is wrong. You have defined a return type of void and your function returns void. I don't think there is a default return type for functions - you have to define a return type as part of the function definition. – ddoor Nov 14 '13 at 05:01
  • 3
    @FaddishWorm No, in C90 functions are assumed to return `int` if they have not been declared. Thus `int main(void){ int x = 1 + f(); return x; }` compiles, which it wouldn't if `f()` was assumed to return `void`. – Pascal Cuoq Nov 14 '13 at 13:32
  • possible duplicate of [warning: implicit declaration of function](http://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function) – sashoalm Apr 08 '14 at 06:02

2 Answers2

4

In C++, functions must be declared or defined before being use; that code cannot be C++.

In C89 or pre-standard C, if the compiler encounters an identifier followed by an open parenthesis, it is a function call, and if there is no declaration or definition in effect, then the return type is implicitly int and the number and types of the arguments is unspecified. (Thus, in the example, display() is a function returning an int and taking an indefinite — but not variable — number of arguments.)

In C99 or C11, in any strict compliance mode, you must have a declaration or definition of the function in scope before calling. It still doesn't have to be a prototype (the inferred declaration int display(); is not a prototype, and the definition is also not a prototype — it would need to be int display(void) to provide a prototype!).

Because of the inferred type for display() and the contradictory definition, you should get a compilation error, I believe. It would be at best a sloppy compiler that allowed it through, even in C89 mode. Indeed, I think even a pre-standard compiler should complain about the difference between the assumed and actual return types, but of course there was no standard so you couldn't complain (and, in any case, that standard is 24 years old now — compilers that only support that are verging on the archaic).

Which compiler (and version) are you using on which platform?

GCC 4.8.2 on Mac OS X 10.9, even with things set as permissive as possible, says:

dec.c:9:6: warning: conflicting types for ‘display’ [enabled by default]
 void display()
      ^
dec.c:5:5: note: previous implicit declaration of ‘display’ was here
     display();
     ^
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Most versions of GCC will emit code anyway - note that this is just a warning, which satisfies the old standard that said the compiler "must diagnose". To turn this into an error, use `-pedantic-errors` or, ideally, get into the habit of compiling everything with `-Werror` (to treat all errors as warnings) and keep your code clean(er). – Gabe Nov 14 '13 at 06:26
  • True, it is only a warning. I forget that people might try to run code despite warnings during compilation — I don't, and I use rather stringent options to make sure there aren't any warnings for much more obscure problems than this one. – Jonathan Leffler Nov 14 '13 at 06:47
  • I used Code::Blocks version 12.11 on Windows7.More details are given here: http://www.codeblocks.org/features – akjlab Nov 15 '13 at 08:52
-1

Your code works perfectly well as a .c file and shows error when it is executed as a .cpp file I hope this post explains why.

Why are function declaration mandatory in C++ and not in C?

Community
  • 1
  • 1