3
votes

I have the following code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
     printf("Checking\n");
     exit(0);
}

 

Before source code, I have two functions one is printf() that is present in stdio.h header file. Second one is exit() function that is present in stdlib.h header file.

Now I want the output for this program is:

printf() function is present in stdio.h

exit() function is present in stdlib.h

Is there any way to find out this?

1
If you mean a magic function that do the job: NO there isn't.LPs
You simply learn what header files are needed for some of these functions. And if you can't learn there's always references, and as a last resort you turn to your favorite search engine.Some programmer dude
You you want some info by gcc you can use -aux-info option to print to file those infos. E.g. gcc test.c -aux-info=infos.txt -o testLPs
There is no magic in header files. You can make your own containing just the prototypes for printf and exit, include that, and your program wil compile as usual.Jongware
Same question here. Looks like some homework assignement.Jabberwocky

1 Answers

1
votes

There's really no portable way to do this since the header text doesn't even need to exist in a file at all! It's quite acceptable under the standard for the #include <system header file> to simply modify the environment without reference to a specific actual header file so, in that case, you don't have easy access to the information from a program.

In terms of finding out if the header text is available, this could range from a simple text search (likely to have false positives since there may be a comment about printf in the math.h header file) to a full blown C-aware parser (likely to be complicated).

Alternatively, you could just (manually) reference the actual standard since those details are available there, a la:

7.21.4.1 The remove function
    Synopsis
        #include <stdio.h>
        int remove(const char *filename);

This little snippet from C11 means that the remove function can be found in stdio.h.

And, if you still want a way to do this within a program, simply gather all that knowledge from the standard into a file or data structure (like an associative array) and write code to look up a given identifier, along the lines of:

#include <string.h>
#include <assert.h>
const char *lookFor (const char *ident) {
    // These should have ALL identifiers and their
    //   equivalent headers.

    static const char const *identifier[] = { "remove",  "strcpy",   ... };
    static const char const *header[] =     { "stdio.h", "string.h", ... };

    assert (sizeof(identifier) == sizeof(header));
    for (size_t i = 0; i < sizeof(header) / sizeof(*header); i++)
        if (strcmp (ident, identifier[i]) == 0)
            return header[i];

    return NULL;
}