2

Possible Duplicate:
Why #include <stdio.h> is *not* required to use printf()?

I am facing a problem in the below given code.

int main()
{
printf("\nHello Stack Overflow\n\n") ;
return 0 ;
}

In the above mentioned code i left including "#include ". And if i compile and execute this piece of code, the output is printed as expected. But "#include " being the most important thing in a C program, i have ignored it and still the compilation is done without any errors but with warning.

Why is this happening?

Community
  • 1
  • 1
Rajan Chennai
  • 145
  • 1
  • 7
  • Last two questions on implicit declarations ... http://stackoverflow.com/questions/13602546/why-its-possible-to-invoke-a-function-declared-later-in-c – hmjd Nov 28 '12 at 10:18
  • try `gcc -Wall test.c ` You will see the warning – Omkant Nov 28 '12 at 10:23

3 Answers3

2

In C, functions that are not declared are implicitly considered to return an int and to take int arguments.

This is bad practice and will bite you. For example if you want to print data not the same size of an int, like double or float.

kmkaplan
  • 18,655
  • 4
  • 51
  • 65
  • You clear my confusion. I wrote a function that return void* before, but I forget to declare it. It worked well on 32 bit machine. But on 64 bit it always return 32 bit. Thanks – louxiu Nov 28 '12 at 10:26
0

From man gcc

   -nostdlib
       Do not use the standard system startup files or libraries when linking.  No startup files and only the libraries you specify will be
       passed to the linker.  The compiler may generate calls to "memcmp", "memset", "memcpy" and "memmove".  These entries are usually
       resolved by entries in libc.  These entry points should be supplied through some other mechanism when this option is specified.
tuxuday
  • 2,977
  • 17
  • 18
  • This may be the OP's question but I do not think so. If one understands that including the header does not provide the function's implementation, only its prototype, then conversely, it does not have to be surprising that omitting the necessary header does not remove the function's implementation, only its prototype. I think the OP's question is why he is not getting a warning. – Pascal Cuoq Nov 28 '12 at 10:41
  • OP mentioned *compilation is done without any errors but with warning.* Also read doc on *-nostdlib* – tuxuday Nov 28 '12 at 10:45
0

printf symbol is not known at compile time, but libc in linked implicite into binary... and that is how it works.

jacekmigacz
  • 789
  • 12
  • 22