Re: question about xCode and C
Re: question about xCode and C
- Subject: Re: question about xCode and C
- From: David Leimbach <email@hidden>
- Date: Fri, 28 Jan 2005 18:08:20 -0800
On Fri, 28 Jan 2005 16:33:45 -0800, Michael de Haan <email@hidden> wrote:
> Dear List,
>
> I am relatively new to C...
>
> I am having difficulty understanding the following: ( I am reading
> Brian Overland's "C in plain English")
>
> As an illustration, he shows how to enter something like this, without
> anything else.
>
> void main() {
> printf( " foo bar ! \n");
> }
>
> ... and explains how to C this is like any other function that needs to
> be defined before being used, and hence the need to add " #include
> <stdio.h> " in the first line.
>
> Well, I just typed it without the # include and ......printed just
> fine, although I had expected to receive a warning "undefined etc etc".
> . I did this both in Terminal and Xcode and got the exact same
> warning but both printed ok......
>
> I know there are real C gurus on the list...any explanation as to why
> it seems to work without adding the includes on Xcode or terminal?
This is because the header file doesn't have a *definition* for printf at all.
tt has the declaration or prototype of printf.
In C a prototype is just the return value, name and arguments to the function
int printf(const char *, ...);
The *definition* of printf is in libSystem.dylib on Mac OS X.
In C, if the compiler doesn't see a prototype or declaration of a
function it attempt
to generate one internally with the correct types based on how you use
the function
and will default the return type to "int".
I'd say 99% of the time you don't want this behavior and you'd like to know when
prototypes are missing. There are settings in Xcode to turn warnings
on for missing
prototypes.
It's not only good practice to always have prototypes sometimes it's
just necessary.
I've ported a lot of code from 32 bit systems to 64 bit systems where
"malloc" is assumed to return an
int instead of a pointer because the programmer didn't include
<stdlib.h>. On 64bit systems where sizeof(int) < sizeof(void *) you
get the following:
1) Code that compiles correctly
2) Code that links without error
3) Code that doesn't work :).
So... while your compiler isn't complaining about the missing
prototype it probably
should as a "best practices" sort of policy.
Dave
>
> Tks
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden