C: How can I know what header do I need for the functions I am using?

2.3k Views Asked by At

Example program in C (without headers):

int main()
{
printf("\nHello World\n");
}

How can I know what include header (example: #include <stdio.h>) should I prepend?

3

There are 3 best solutions below

3
On

Use this as reference C library reference, to your code to work use this

#include <stdio.h>
0
On

Considering that you might not be able to search online (which would be the obvious choice most of the time I guess) and that you are on a linux machine you could also search for it in the man pages.
To search inside the man pages you can use man -k {search term}

For example printf

$ man -k printf 
asprintf (3)         - print to allocated string
dprintf (3)          - formatted output conversion
fprintf (3)          - formatted output conversion
fwprintf (3)         - formatted wide-character output conversion
printf (1)           - format and print data
printf (3)           - formatted output conversion
snprintf (3)         - formatted output conversion
sprintf (3)          - formatted output conversion
swprintf (3)         - formatted wide-character output conversion
vasprintf (3)        - print to allocated string
vdprintf (3)         - formatted output conversion
vfprintf (3)         - formatted output conversion
vfwprintf (3)        - formatted wide-character output conversion
vprintf (3)          - formatted output conversion
vsnprintf (3)        - formatted output conversion
vsprintf (3)         - formatted output conversion
vswprintf (3)        - formatted wide-character output conversion
vwprintf (3)         - formatted wide-character output conversion
wprintf (3)          - formatted wide-character output conversion
XtAsprintf (3)       - memory management functions

$ man 3 printf
PRINTF(3)                                                                                  Linux Programmer's Manual                                                                                 PRINTF(3)

NAME
       printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf, vsprintf, vsnprintf - formatted output conversion

SYNOPSIS
       #include <stdio.h>

       int printf(const char *format, ...);
       int fprintf(FILE *stream, const char *format, ...);
...
0
On

As has been mentioned in comments, you can use the search feature on https://en.cppreference.com/w/c/header.

Just make sure you select the C version of the function.

enter image description here

And the header you need to include is listed at the top of the page.

enter image description here