Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
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.


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