sos help for understanding

View: New views
3 Messages — Rating Filter:   Alert me  

sos help for understanding

by yacson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everybody. Sorry, I'm a new user and i want to understand why this c code work (in linux I'm worling on mandriva 2009 spring)? main() {printf("hello,world");} why does not the compiler complain about the missing stdio.h ? it just gives "warning:incompatible implicit declaration of built-in function printf" and actually prints the string "hello,world".

Re: sos help for understanding

by dan hitt-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, the compiler needs to know how the function printf() is to be
used, and it can only find that information by looking at
/usr/include/stdio.h (or wherever the system puts it).

(A book on c would probably be helpful.)

dan

On Fri, Nov 6, 2009 at 11:10 AM, yacson <yactiem@...> wrote:

>
> Hi everybody.
> Sorry, I'm a  new user and i want to understand why this c code work (in
> linux I'm worling on mandriva 2009 spring)?
> main()
> {printf("hello,world");}
>
> why does not the compiler complain about the missing stdio.h ? it just gives
> "warning:incompatible implicit declaration of built-in function printf" and
> actually prints the string "hello,world".
> --
> View this message in context: http://old.nabble.com/sos-help-for-understanding-tp26230861p26230861.html
> Sent from the gcc - Help mailing list archive at Nabble.com.
>
>

Re: sos help for understanding

by Ian Lance Taylor-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

yacson <yactiem@...> writes:

> Sorry, I'm a  new user and i want to understand why this c code work (in
> linux I'm worling on mandriva 2009 spring)?
> main()
> {printf("hello,world");}
>
> why does not the compiler complain about the missing stdio.h ? it just gives
> "warning:incompatible implicit declaration of built-in function printf" and
> actually prints the string "hello,world".

For historical reasons C permits you call functions which have not
been declared.  If you do that, the function is presumed to have been
declared as "int fn()" (which is not the same as "int fn(void)").
That is what is happening here.

This is not true in C++, by the way, only in C.

Technically the standard prohibits you from using an implicit
declaration for a varargs function like printf, but in practice it
normally works on real systems.  Also the standard prohibits you from
using an implicit declaration for a function which does not actually
return int, but that too will normally work unless the function
returns a struct.

Ian