Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Universal header to include in C language

Tags:

c

I am new to C language, and was wondering if there is a universal header that can be included at top of main function . In Java it is very easy just do ctrl+shift+o in eclipse and it imports packages for you. But in C, I have to google every time and add it. Sometimes, I don't even know what library to include. Thank you very much .

like image 280
David Prun Avatar asked Mar 01 '26 01:03

David Prun


2 Answers

There is no universal header -- since parsing every header takes time, and there are thousands (if not millions) of headers available, there is no way to include them all into every compilation unit. You wouldn't want to, since 99.9% of them wouldn't be used and would only needlessly bloat the end executable with static allocations.

Every standardized function will tell you the headers you need to include at the top of its manpage; for example, from malloc(3):

NAME
   calloc, malloc, free, realloc - Allocate and free dynamic
   memory

SYNOPSIS
   #include <stdlib.h>

   void *calloc(size_t nmemb, size_t size);
   void *malloc(size_t size);
   void free(void *ptr);
   void *realloc(void *ptr, size_t size);

Thus you need to #include <stdlib.h> in your project, and there's the prototypes for you to see.

If you want a quick way to see the manpages, you can configure your IDE to show them to you quickly. The default keybinding for K in vim is to load the manpage for the function under the cursor -- but, since it uses the default manpage search order, it can sometimes find the wrong page. (On printf, for example, it loads printf(1) rather than printf(3). Annoying. The MANSECT environment variable described in man(1) can be used to change this behavior to show you 3 before 1, if you wish.)

like image 103
sarnold Avatar answered Mar 03 '26 17:03

sarnold


There isn't a single header that is all-encompassing.

All else apart, any such header for MS Windows would be wrong for Unix, and any such header for Unix would be wrong for Windows. Even on Unix, should the universal header include all the X11 headers? What about the OpenSSL headers? What about the POSIX threads headers? Other POSIX headers?

You need to learn where to go to find the information for any given function that you need to use. On Unix, the classic resource was the 'man page' (meaning 'manual page', typically formatted with the '-man' troff/nroff macro package). These days, I tend to use the web: for example, I find POSIX man pages at The Open Group.

(Note that a header is separate from a library; there may be many headers used by the functions in a single library. See the Standard C Library as an illustration.)

like image 38
Jonathan Leffler Avatar answered Mar 03 '26 18:03

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!