The Wayback Machine - https://web.archive.org/web/20240204103721/https://www.geeksforgeeks.org/print-hello-world-c-without-using-header-file/
Open In App
Related Articles

Print “Hello World” in C/C++ without using any header file

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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;
    }

    
    

    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
    extern "C"
    {
    int printf(const char *format, ...);
    }
      
    int main()
    {
      printf( "Hello World" );
      return 0;
    }

    
    

    Output: Hello World

See this to know more about all phases of compilation of C program.

This blog is contributed by Shubham Bansal.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 13 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads