6

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

int main()
{
    printf("integra");
    return 0;
}
J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
venkat
  • 157
  • 2
  • 5
  • 1
    "Why does this work?" is not a valid question when you've written code with undefined behavior. – R.. GitHub STOP HELPING ICE Jan 11 '11 at 05:58
  • @R..: maybe that's a reason to assume that venkat didn't came up with this question himself, but his teacher did??? – Bart Jan 11 '11 at 15:03
  • Possible duplicate of [Why #include is \*not\* required to use printf()?](http://stackoverflow.com/questions/336814/why-include-stdio-h-is-not-required-to-use-printf) –  Oct 30 '15 at 02:00

6 Answers6

8

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.

aTJ
  • 3,823
  • 3
  • 21
  • 25
7

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.

J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
  • Hi it is also working int main() { if(isalnum(';')) printf("character ; is not alphanumeric"); if(isalnum('A')) printf("character A is alphanumeric "); return 0; } – venkat Jan 11 '11 at 04:30
  • 1
    lookup your man page. `isalpha()` is `int isalpha(int)`. this match what I said. – J-16 SDiZ Jan 11 '11 at 04:34
  • Also, older C standard does not care about argument type (in most case). If you want more details, you have to understand what is the different between pascal/c function convention. Google it. – J-16 SDiZ Jan 11 '11 at 04:39
5

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

Abi
  • 4,718
  • 4
  • 20
  • 29
  • The dynamic linker will resolve the printf symbol **even if you have its declaration included**. (Think about that!) – user562374 Jan 12 '11 at 02:20
4

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
$
1

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

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • It is often legal to use undeclared functions in C (but I think not in this case), but not recommended. – Philipp Jan 11 '11 at 13:57
1

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.

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41