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)
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.
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.
#include <stdio. h> – It is used to include the standard input output library functions. The printf() function is defined in stdio. h .
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With