Print “Hello World” in C/C++ without using any header file
Write a C/C++ program that prints Hello World without including any header file.
Conceptually it’s seems impractical to write a C/C++ program that print Hello World without using a header file of “stdio.h”. Since the declaration of printf() function contains in the “stdio.h” header file.
But we can easily achieve this by taking the advantage of C pre-processor directives. The fact is at the time of compiling a program, the first phase of C preprocessing expands all header files into a single file and after that compiler itself compiles the expanded file. Therefore we just need to extract the declaration of printf() function from header file and use it in our main program like that:-
- C language: Just declare the printf() function taken from “stdio.h” header file.
//Declare the printf() function
int
printf
(
const
char
*format, ...);
int
main()
{
printf
(
"Hello World"
);
return
0;
}
chevron_rightfilter_noneOutput: Hello World
- C++ language: We can’t directly put the declaration of printf() function as in previous case due to the problem of Name mangling in C++. See this to know more about Name mangling. Therefore we just need to declare the printf() inside extern keyword like that:-
//Declare the printf() function inside
//extern "C" for C++ compiler
extern
"C"
{
int
printf
(
const
char
*format, ...);
}
int
main()
{
printf
(
"Hello World"
);
return
0;
}
chevron_rightfilter_noneOutput: Hello World
See this to know more about all phases of compilation of C program.
This blog is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Recommended Posts:
- clocale header file in C++
- How to write your own header file in C?
- Comment in header file name?
- Difference between Header file and Library
- time.h header file in C with Examples
- C Program to print contents of file
- C++ program to print unique words in a file
- C program to copy contents of one file to another file
- random header in C++ | Set 3 (Distributions)
- random header | Set 2 (Distributions)
- random header in C++ | Set 1(Generators)
- numeric header in C++ STL | Set 1 (accumulate() and partial_sum())
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)