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
Output: 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
Output: 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 firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Comment in header file name?
- clocale header file in C++
- How to write your own header file in C?
- Difference between Header file and Library
- <complex.h> header file in C with Examples
- 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 print odd line contents of a File followed by even line content
- C program to copy contents of one file to another file
- Exception header in C++ with examples
- random header | Set 2 (Distributions)
- random header in C++ | Set 3 (Distributions)
- random header in C++ | Set 1(Generators)
- accumulate() and partial_sum() in C++ STL : numeric header
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)
- CSV file management using C++
- fsetpos() (Set File Position) in C