5

I'm implementing some basic data structures in C and I found out that if I omit the return type from a function and call that function the compiler doesn't generate an error. I compiled with cc file.c and didn't use -Wall (so I missed the warning) but in other programming languages this is a serious error and the program won't compile.

Per Graham Borland's request, here's a simple example:

int test() 
{
  printf("Hi!");
}

int main() 
{ 
    test();
}
Daniel
  • 1,075
  • 2
  • 14
  • 26

5 Answers5

7

It's because in C, any variable / function is implicitly int.

This is the same reason that you can use register instead of register int, or unsigned instead of unsigned int, auto instead of auto int, and static instead of static int. I personally always explicitly qualify my variables with int, but whether you do or not is your option.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
7

C is an old language and at the time it was introduced, returning integers was common enough to be the default return type of a function. People later started realizing that with more complicated return types, it was best to specify int to be sure you are not forgetting the return type, but in order to maintain backwards compatibility with old code, C could not remove this default behavior. Instead most compilers issue warnings.

If the function reaches the end without a return statement an undefined value is returned except in the main function, 0 is returned. This has the same reason as above.

/* implicit declaration of printf as: int printf(int); */

/* implicit int type */
main()
{
    printf("hello, world\n");
} /* implicit return 0; */
Matt
  • 21,026
  • 18
  • 63
  • 115
  • 7
    If you go back far enough (mid-80s or ealier), there wasn't a return type of `void`; so a function that didn't really return a value was still deemed to return an `int`. But if you never used the value, it didn't matter if there wasn't a return at the end of the function. Also note that in C89, the return value from main is undefined if you don't explicitly return a value. It was only in C99 that it became defined that falling off the end of `main()` was equivalent to `return 0;`. This was partly to follow the lead of C++98 which also defines that behaviour. – Jonathan Leffler Jun 01 '12 at 23:48
  • @Jonathan `void` was in PWB's C, which predated the 80's. – Jim Balter Jun 02 '12 at 03:44
  • 1
    Maybe; but it wasn't in the C I learned on in 1983-4. It arrived maybe 3 years later. It depends on which compiler you were using. – Jonathan Leffler Jun 02 '12 at 05:42
  • nitpick, it is not the return value that is undefined, it is the behavior of the whole program that is undefined would it ever try to access the return value of the function. So if you'd do this (try to access the value) it could eat your hard disk or empty your bank account. And `main` is different (since C99) since it is an explicit exception that is specified in the standard. – Jens Gustedt Jun 02 '12 at 08:29
  • @JonathanLeffler The example in my answer is the exact test of the first program in the first edition of Kernighan and Ritchie's _The_ _C_ _Programming_ _Language_ (Published in 1978). Note the lack of a return statement and of a `#include ` (the latter is added in the second edition). – Matt Jun 02 '12 at 21:00
  • I'd not looked at my K&R1, but apart from the comments, I can readily believe it is what they used. K&R didn't want to get involved in discussing the finer points of the return value from `main()` in the opening example. Nevertheless, the value returned to the environment (shell calling the program) was not defined prior to compilation with C99 (so even in K&R2, the code is a little sloppy). C coding standards are more stringent now than they were then. – Jonathan Leffler Jun 02 '12 at 21:19
  • @JonathanLeffler Yeah I forgot to say apart from the comments. – Matt Jun 02 '12 at 21:34
  • @JonathanLeffler Also, they never return anything from `main` in any example throughout the whole book. They do mention that "a `return` statement with no expression causes control, but no useful value, to be returned to the caller," which I take to mean the return value is undefined but the behavior is well-defined. – Matt Jun 02 '12 at 21:47
3

The return type of a function is optional and is considered "int" if not specified.

BoltBait
  • 11,361
  • 9
  • 58
  • 87
3

I haven't seen the why addressed well in any of the answers so far. In C, it's perfectly legal for a function with a non-void return type to end without a return statement/value, as long as the caller does not attempt to use the return value. For instance (slightly nontrivial example):

#include <stdio.h>
int foo(int want_result)
{
    puts("hello");
    if (want_result) return 42;
}

int main()
{
    foo(0);
    printf("%d\n", foo(1));
}

This example is somewhat contrived, but it could actually become meaningful if the return type is a huge structure that would take non-trivial time to return.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

The function will not return 0 as the Matt's answer says.

In fact, printf returns a value: the number of characters printed. In your case, printf, which is the last expression in your test function, will return 3. Thus the function test will return 3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samuel Gosselin
  • 591
  • 5
  • 5