3

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?

Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52
  • You might try looking [at this reference](https://en.cppreference.com/w/c/header) – Tom Drake Mar 05 '19 at 23:08
  • https://www.tutorialspoint.com/c_standard_library/ – Amadeu Antunes Mar 05 '19 at 23:09
  • 2
    Google is your friend. "In C, what include file is needed for the function printf" – EvilTeach Mar 05 '19 at 23:11
  • 1
    @EvilTeach but how long until google crawls this page, decides it is a better match for the search phrase and starts to return this page as the top result? – Increasingly Idiotic Mar 05 '19 at 23:18
  • @IncreasinglyIdiotic Look at the results starting from the second one? – eesiraed Mar 06 '19 at 00:38
  • 1
    @FeiXiang just saying that seeing "Just google it" can be frustrating because Google (eventually) ends up prioritizing StackOverflow links most of the time. So somewhere down the line the top link ends up being a StackOverflow post with the exact question I search but the answer tells me to "Just google it". – Increasingly Idiotic Mar 06 '19 at 03:44
  • I have looked for a page that documents all of the headers for all of the functions for all of the platforms I care about. I don't think there is one. I can grep the headers on my system, or use Google. Most of the time, Google gets me to the answer faster. – EvilTeach Mar 06 '19 at 14:06
  • if working on a OS/shell that contains the `man` command, you can (at the command prompt) enter: `man commandNameOfInterest` and the MAN page for that command will be displayed. (note: some commands are spelled the same in both the shell and in C, so may need to include the optional number of the MAN page group where the command is described.) In each MAN page (other than the page for the shell commands) is a list of the header files needed – user3629249 Mar 07 '19 at 19:06

3 Answers3

7

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, ...);
...
bw0248
  • 711
  • 5
  • 19
5

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

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
-1

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

#include <stdio.h>
Jackpot
  • 47
  • 1
  • 5
  • 3
    You linked a website for C++, which doesn't include many of the C99 or any of the C11 standard library headers – UnholySheep Mar 05 '19 at 23:07
  • 1
    it is a C library do look closely – Jackpot Mar 05 '19 at 23:08
  • 1
    No it is not. It only includes a subset of the C standard library, which is not already included in the C++ standard library. https://en.cppreference.com/w/c/header has a better list – UnholySheep Mar 05 '19 at 23:09