C : Return Type Defaults to Int

15,575

Solution 1

I don't know ObjectiveC but in standard C you must declare the return type and the parameters for main. So: int main(void) should work. Most (all) operating systems expect a return of int from main.

The return value from main is the exit code for the program to indicate whether it ended normally or otherwise.

Solution 2

Warning are your friends.

It's telling you the function returns int but you are not returning anything so there is a potential problem.

Solution 3

In older C, any function could be defined without an explicit return type, and by default, it was assumed to return int. This was also true for function parameters. In more recent versions of the C standard, implicit-int has been removed (paragraph 5 of Foreword).

Also, any function that has a return type other than void (meaning the function does not return a value) must have a return statement, otherwise your program is not conforming. Conformance is important if you want your program to run in a well-defined manner. main is a special exception to the return-statement-rule, whereby it does not need to have a return statement. If no return statement is provided, then the main function implicitly returns 0 (section 5.1.2.2.3).

Your compiler warned you that you have not explicitly specified a return type, and also that the control reaches the end of a non-void function without returning a value. The former can be easily remedied by putting int before main in your code. The latter—even though main is an exception to the rule—can likewise be remedied by putting in a return statement in anyway, just to be consistent with other non-void functions.

Solution 4

To fix this you need int main() and return 0; at the end:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int total = 81;
    float ratio = 1.618;

    char* result;
    asprintf (&result, "total: %i, ratio: %1.3f", total, ratio);

    printf ("%s \n", result);
    free (result);
    return 0; 
}

Solution 5

A C function can be written as

<return type a> <function name>( <type 1> <parameter 1>, .. ) {
   ...

   return <element of type a>;
}

However can be left out, but by the set of rules your compiler plays by it is advised to specify a return type.

Share:
15,575
Justian Meyer
Author by

Justian Meyer

Graduated from Georgia Institute of Technology in Dec 2014 with degree focus on Information/Internetworks and Intelligence. Started my programming journey at 10 years old with DarkBasic and have since gone on to work at two top tech companies in Silicon Valley.

Updated on June 17, 2022

Comments

  • Justian Meyer
    Justian Meyer almost 2 years

    I'm trying to follow "Cocoa and Objective C: Up and Running" by OReilly (a great read by the way), but I'm consistently bugged with these annoying warnings.

    For example: this code segment (p.56-57)...

    #include <stdio.h>
    #include <stdlib.h>
    
    main() {
        int total = 81;
        float ratio = 1.618;
    
        char* result;
        asprintf (&result, "total: %i, ratio: %1.3f", total, ratio);
    
        printf ("%s \n", result);
        free (result);
    }
    

    Throws me this...

    /Users/justianmeyer/Desktop/test.c:5: warning: return type defaults to ‘int’
    /Users/justianmeyer/Desktop/test.c: In function ‘main’:
    /Users/justianmeyer/Desktop/test.c:18: warning: control reaches end of non-void function
    total: 81, ratio: 1.618 
    

    I've been programming for a while now, but am completely new to C/Objective C as of yesterday. I've been reading that undeclared methods are expected to accept/return an int by default, but isn't the main() method supposed to be automatically declared? Why would it need to be defaulted to an int? Furthermore, is main() supposed to return an int? What's the use in that? Where is the return value received?

    Also: Is there any way to create a ".c" file in XCode 4.2 without including it in a project? Right now I'm using TextMate for the examples, but I want to become as familiar with XCode as possible over the course of this book.