Function without return type specified in C
Asked Answered
W

3

7

I came across this piece of code in C:

#include <stdio.h>
main( )
{
 int i = 5;
 workover(i);
 printf("%d",i);
}
workover(i)
int i;
{
 i = i*i;
 return(i);
}

I want to know how the declaration of the function "workover" is valid? What happens when we don't mention the return type of a function? (can we return anything?).The parameter is also just a variable name, how does this work?

Wiseacre answered 30/5, 2015 at 3:59 Comment(0)
P
19

If you do not specify a return type or parameter type, C will implicitly declare it as int.

This is a "feature" from the earlier versions of C (C89 and C90), but is generally considered bad practice nowadays. Since the C99 standard (1999) does no longer allow this, a compiler targeting C99 or later will likely give you a warning similar to the following:

program.c: At top level:
program.c:8:1: warning: return type defaults to ‘int’
 workover(i)
 ^
Pecos answered 30/5, 2015 at 4:3 Comment(0)
Q
4

The function declaration syntax was used in older versions of C, and is still valid, so the code fragment "workover(i) int i;" is equivalent to "workover(int i)". Although, I think it may still generate warnings or even errors, depending on what compiler options you use.

Quicklime answered 30/5, 2015 at 4:5 Comment(2)
It's not still valid: "C99 is, for the most part, backward compatible with C89, but it is stricter in some ways. In particular, a declaration that lacks a type specifier no longer has int implicitly assumed." That's why a warning may be generated.Pecos
Those two are not exactly equivalent; workover(i) int i; is not a prototype but workover(int i) is. It is similar to the difference between int main() and int main(void)Monophagous
S
1

When I compile your code as $ gcc common.c -o common.exe -Wall (Trying it over Cygwin Terminal as I don't have my linux system with me right now)

I get following warnings:

common.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main( )
^
common.c: In function ‘main’:
common.c:6:2: warning: implicit declaration of function ‘workover’ [-Wimplicit-f                  unction-declaration]
workover(i);
^
common.c: At top level:
common.c:9:1: warning: return type defaults to ‘int’ [-Wreturn-type]
workover(i)
^
common.c: In function ‘main’:
common.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
  1. The first and the third one says, return type defaults to ‘int’ which means that if you don't specify a return type, compiler will implicitly declare it as int.
  2. The second one says, implicit declaration of function ‘workover’ since the compiler doesn't know what workover is.
  3. Third warning is pretty simple to understand and will disappear if you fix the first one.

You should do it this way:

#include <stdio.h>

int workover(int);

int i;

int main(void)
{
    int i = 5;
    workover(i);
    printf("%d",i);     //prints 5
    return 0;
}

int workover(int i)
{
    i = i*i;    //i will have local scope, so after this execution i will be 25;
    return(i);  //returns 25
}
Sneaky answered 30/5, 2015 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.