简体   繁体   中英

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

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?

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, ...);
...

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.

在此输入图像描述

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

在此输入图像描述

使用它作为参考C库引用 ,让你的代码工作使用它

#include <stdio.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM