Search code examples
c++cheader

Header files can run in main class. How is that possible?


When I working with C programming it's normal include method is

#include<stdio.h>
int main()
{
    printf("Hello World");

    return 0;
}

Output: Hello World

Now I have tried to put my #include into my main class and its run perfectly without any errors or warnings.

int main()
{
    #include <stdio.h>
    printf("Hello World\n");

    return 0;
}

Output: Hello World

I have tried this method with C++. I couldn't do that kind of activity in there it gives me lot of errors.

Why only C have this technique?
How is that possible?


Solution

  • As seen here ,inside cdefs.h __BEGIN_DECLS is defined as

    #ifdef        __cplusplus
    #define __BEGIN_DECLS        extern "C" {
    #define __END_DECLS        }
    #else
    #define __BEGIN_DECLS
    #define __END_DECLS
    #endif
    

    This __BEGIN_DECLS is used in stdio.h as seen here

    For C++, __BEGIN_DECLS expands to extern "C" and linkage specification should be at global scope not inside main