Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using printf function without actually importing stdio.h and it worked?! Why is that so? [duplicate]

Tags:

c

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

//#include <stdio.h>
//#include <conio.h>

main(){

printf("Hi");
getch();

}

As I was programming this, it shocked me that it worked without actually importing any c libraries such as stdio that contains the printf function. Why is that so? (Used Dev-C++ 4.9.9.2, saved as .c, not .cpp)

like image 872
Xegara Avatar asked Jun 22 '12 06:06

Xegara


People also ask

Do you need stdio.h for printf?

The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement.

What happens when printf is executed?

Input/Output The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.

What is #include stdio.h in C program?

#include <stdio. h> – It is used to include the standard input output library functions. The printf() function is defined in stdio. h .

What header file is printf in C?

The printf() function in C++ is used to write a formatted string to the standard output ( stdout ). It is defined in the cstdio header file.


1 Answers

C allows you to call functions without first defining the prototypes. (C++ does not do this.) An implicit prototype for printf will be defined as follows:

int printf();

Coincidentally, the calling conventions for this implicit prototype matched the actual calling conventions for printf on your platform.

In general, you cannot count on this working, and there are a large number of cases where it won't work. I recommend enabling compiler warnings to detect implicit prototype declarations so you can fix them (by including the correct header).

Footnote: #include does not import libraries, it merely pastes files into your source code at compile time. The <stdio.h> header contains (directly or indirectly) certain prototypes, but the library has to be linked in separately. Since printf is usually in a library that is linked to programs by default, you usually don't have to do anything to use printf.

like image 67
Dietrich Epp Avatar answered Sep 28 '22 03:09

Dietrich Epp