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.
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.