venkat
venkat

Reputation: 157

without including <stdio.h>

below given program is working without including <stdio.h>? Why does this work?

int main()
{
    printf("integra");
    return 0;
}

Upvotes: 6

Views: 5505

Answers (6)

aTJ
aTJ

Reputation: 3913

Definition of printf() is there in libc.so and the dynamic linker will take care of it even if you don't include the header file. During compile time, printf() will be an undefined symbol and it assumes that it may find the definition later on in libc. The header file will just give the proto-type and suppress the compiler(warnings) stating that the definition of the prototype is present in glibc. So basically, the header files are included just to make sure that the definitions are available in our libraries, to help the developer.

Upvotes: 8

user568493
user568493

Reputation:

As Abi pointed out, your code builds successfully without including stdio.h because the linker is defaulting to the system std library for the undefined symbol (printf). GCC would normally warn you of such cases.

Let test.c be:

1: int main()
2: {
3:    printf("test\n");
4:    return 0;
5: }

Building test.c with GCC 4.2.1 on Mac OSX:

$ gcc test.c
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
$ ./a.out
test

You can disable this default linking by specifying a GCC linker option -nostdlib (or -nodefaultlibs) along with -lgcc (as the GCC manual recommends):

$ gcc -nostdlib -lgcc test.c
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
Undefined symbols:
  "_puts", referenced from:
      _main in cc3bvzuM.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$

Upvotes: 4

Bart van Ingen Schenau
Bart van Ingen Schenau

Reputation: 15768

When you use a function that has not been declared, then the compiler will assume this function returns an int and takes an unspecified, but fixed, number of arguments.

If this assumption matches with the definition of the function, and if the arguments you provided also match (modulo the default argument promotions) the parameters that the function expects to receive, then everything is well.
If the assumption is incorrect (like for printf, which is a variadic function), or when the arguments don't match, the results are undefined. One of the nasty things of undefined behaviour is that it can appear to work as expected.

Upvotes: 1

Abi
Abi

Reputation: 4808

printf() is only defined in libc.so

The dynamic linker will resolve the symbol printf() in libc since you have not included it

libc is default in gcc for every program

Upvotes: 5

J-16 SDiZ
J-16 SDiZ

Reputation: 26910

in older standard, undeclared function assume int argument and return value. Your char* have same size (32-bit) as int, so everything work.

Just don't do it.

Upvotes: 7

ta.speot.is
ta.speot.is

Reputation: 27214

Some (old?) compilers do not require prototypes before calling functions.

Upvotes: 1

Related Questions