top of page

Header Files in C/C++




A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.


Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.


They are most often used to include external variable declarations, macro definitions, type definitions, and function declarations. Groups of logically related functions are commonly declared together in a header file, such as the C library input and output functions listed in the stdio.h header file.


There are of 2 types of header file:

  1. Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them.

  2. User-defined header files: These files are defined by the user and can be imported using #include.


Header Files in C/C++

Below are some inbuilt header files in C/C++:


1. <assert.h>

The assert.h header file provide macro called assert which is used to verify assumptions make by the program and print diagnostic message. If the assumption you make about the code is correct it continues without disruption. f the assumption isn't correct it prints a diagnostic message.


Function list

  1. void assert(int expression) - This is actually a macro and not a function, which can be used to add diagnostics in your C program.


2. <complex.h>

The Complex.h header file is used for complex number operations and manipulations. cmplx() function creates complex number objects by taking real part and imaginary parts as parameters. This function returns the object of complex numbers.


3. <ctype.h>

The ctype.h header file of the C Standard Library declares several functions that are useful for testing and mapping characters. All the functions accepts int as a parameter, whose value must be EOF or representable as an unsigned char. All the functions return non-zero (true) if the argument c satisfies the condition described, and zero(false) if not.


Below are some of the functions of this library :

  1. isalnum() : This function checks whether or not a character is alphanumeric

  2. isdigit(): This function checks whether or not character is decimal digit.

  3. tolower(): This function checks whether or not character is uppercase and then convert it to lowercase.

  4. toupper(): Convert lowercase to uppercase.

  5. isblank(): check whether or not character is blank.

  6. iscntrl(): check whether or not character is control character

  7. isdigit(): check whether or not character is decimal

  8. isgraph(): check whether or not character has graphical representation

  9. isprint(): check whether or not character is printable.

  10. ispunct(): check whether or not character is a punctuation character.

  11. isxdigit(): check whether or not character is white-space

  12. isxdigit():check whether or not character is hexadecimal digit.


4. <errno.h>

This header file is used to test the value stored in errno by certain library functions. At program startup, the value stored is zero. Library functions store only values greater than zero. Any library function can alter the value stored, but only those cases where a library function is explicitly required to store a value are documented here. To test whether a library function stores a value in errno, the program should store the value zero there immediately before it calls the library function.


Below are some of the errno.h functions of this library:

  1. EDOM: This macro represents domain error for a function call, in which an input argument outside the function's domain is passed

  2. ERANGE : This macro represents range error, in which either the input argument or result of the function is outside a function's range.

  3. EACCES : This macro represents an error when a file cannot be opened for reading.


5. <fenv.h>

This header file is used to define two types, several macros, and several functions that test and control floating-point status, if the implementation permits. The functionality matches IEC 60559, but it can be applied to similar floating-point hardware. Floating-point status can be represented in an object of type fexcept_t. It forms part of the floating-point control, which determines the behavior of floating-point arithmetic. A copy of the floating-point control can be represented in an object of type fenv_t.


Another part of the floating-point control is the rounding mode, representable as a value of type int, which determines how floating-point values convert to integers. The rounding modes are:

  • downward, toward the nearest more negative integer

  • to nearest, toward the nearest integer with the closer value, or toward the nearest even integer if two integers are equally near

  • toward zero, toward the nearest integer closer to zero (also called truncation)

  • upward, toward the nearest more positive integer


Below are some of the function of this library:

  1. feclearexcept() : clear floating-point exception

  2. feraiseexcept(): Raise floating-point exception

  3. fegetexceptflag(): Get floating-point exception flags

  4. fesetexceptflag() : Set floating-point exception flags

  5. fegetround(): Get rounding direction mode

  6. fesetround() : Set rounding direction mode

  7. fegetenv(): Get floating-point environment

  8. fesetenv(): Set floating-point environment

  9. feholdexcept(): Hold floating-point exceptions

  10. feupdateenv(): Update floating-point environment

  11. fetestexcept(): Test for floating-point exceptions


6. <float.h>

This header file is used to determine various properties of floating-point type representations. The standard header <float.h> is available even in a freestanding implementation. You can test the values of any of the integer macros except FLT_ROUNDS in an if directive. (The macros expand to #if expressions.) All other macros defined in this header expand to floating-point constant expressions


Below are some of the Functions of this library:


FLT_RADIX Base for all floating-point types (float, double and long double).


FLT_MANT_DIG Precision of significand, i.e. the number of digits that conform the

DBL_MANT_DIG significand.

LDBL_MANT_DIG


FLT_DIG Number of decimal digits that can be rounded into a floating-point and

DBL_DIG back without change in the number of decimal digits.

LDBL_DIG


FLT_MIN_EXP Minimum negative integer value for the exponent that generates a

DBL_MIN_EXP normalized floating-point number.

LDBL_MIN_EXP


FLT_MIN_10_EXP Minimum negative integer value for the exponent of a base-10 expression

DBL_MIN_10_EXP that would generate a normalized floating-point number.

LDBL_MIN_10_EXP


FLT_MAX_EXP Maximum integer value for the exponent that generates a normalized

DBL_MAX_EXP floating-point number.

LDBL_MAX_EXP


FLT_MAX_10_EXP Maximum integer value for the exponent of a base-10 expression that

DBL_MAX_10_EXP would generate a normalized floating-point number.

LDBL_MAX_10_EXP


FLT_MAX Maximum finite representable floating-point number.

DBL_MAX

LDBL_MAX


FLT_EPSILON Difference between 1 and the least value greater than 1 that is

DBL_EPSILON representable.

LDBL_EPSILON


FLT_MIN Minimum representable positive floating-point number.

DBL_MIN

LDBL_MIN


FLT_ROUNDS Rounding behavior. Possible values:

-1 undetermined

0 toward zero

1 . to nearest

2. toward positive infinity

3. toward negative infinity

Applies to all floating-point types (float, double and long double).


FLT_EVAL_METHOD Properties of the evaluation format. Possible values:

-1 undetermined

0. evaluate just to the range and precision of the type

1. evaluate float and double as double, and long double as long double.

2. evaluate all as long double Other negative values indicate an

implementation-defined behavior.

Applies to all floating-point types (float, double and long double).


DECIMAL_DIG Number of decimal digits that can be rounded into a floating-point type

and back again to the same decimal digits, without loss in precision.



7. <inttypes.h>

This header file is used to define a type, several functions, and numerous macros for fine control over the conversion of integers. Note that the definitions shown for the macros are merely representative -- they can vary among implementations.


Functions lists:

1. imaxabs - equivalent to abs for intmax_t:

intmax_t imaxabs (intmax_t n);

2. imaxdiv - equivalent to div for intmax_t:

imaxdiv_t imaxdiv (intmax_t numer, intmax_t denom);

3. strtoimax - equivalent to strtol for intmax_t:

intmax_t strtoimax (const char* str, char** endptr, int base);

4. strtoumax - equivalent to strtoul for uintmax_t:

uintmax_t strtoumax (const char* str, char** endptr, int base);

5. wcstoimax - equivalent to wcstol for intmax_t:

intmax_t wcstoimax (const wchar_t* wcs, wchar_t** endptr, int base);

6. wcstoumax - equivalent to wcstoul for uintmax_t:

uintmax_t wcstoumax (const wchar_t* wcs, wchar_t** endpt


8. <iso646.h>

This header file is used to provide readable alternatives to certain operators or punctuators. The standard header <iso646.h> is available even in a freestanding implementation.


Marcos operators

and &&

and_eq &=

bitand &

bitor |

comp l~

not !

not_eq !=

or ||

or_eq |=

xor ^

xor_eq ^=


9. <limits.h>

This header file is used to determine various properties of the integer type representations. The standard header <limits.h> is available even in a freestanding implementation. You can test the values of all these macros in an if directive. (The macros are #if expressions.)


Functions lists

  1. CHAR_BIT - Number of bits in a char object

  2. SCHAR_MIN - Minimum value for an object of type signed char

  3. SCHAR_MAX - Maximum value for an object of type signed char

  4. UCHAR_MAX - Maximum value for an object of type unsigned char

  5. CHAR_MIN - Minimum value for an object of type char

  6. CHAR_MAX - Maximum value for an object of type char

  7. MB_LEN_MAX - Maximum number of bytes in a multibyte character, for any locale

  8. SHRT_MIN - Minimum value for an object of type short int

  9. SHRT_MAX - Maximum value for an object of type short int

  10. USHRT_MAX- Maximum value for an object of type unsigned short int

  11. INT_MIN - Minimum value for an object of type int

  12. INT_MAX - Maximum value for an object of type int

  13. UINT_MAX - Maximum value for an object of type unsigned int

  14. LONG_MIN - Minimum value for an object of type long int

  15. LONG_MAX - Maximum value for an object of type long int

  16. ULONG_MAX - Maximum value for an object of type unsigned long int

  17. LLONG_MIN - Minimum value for an object of type long long int

  18. LLONG_MAX - Maximum value for an object of type long long int

  19. ULLONG_MAX - Maximum value for an object of type unsigned long long int



10. <locale.h>

This header file is used to alter or access properties of the current locale -- a collection of culture-specific information. An implementation can define additional macros in this standard header with names that begin with LC_. You can use any of these macro names as the locale category argument (which selects a cohesive subset of a locale) to setlocale.


Functions lists:

  1. setlocale - Set or retrieve locale

  2. localeconv - Get locale formatting parameters for quantities


11. <math.h>

This header file is used to declare a number of functions that perform common mathematical operations on real floating-point values (of type float, double, or long double).


A domain error occurs when the function is not defined for its input argument value or values. A function can report a domain error by storing the value of EDOM in errno and returning a particular value defined for each implementation. Or it can raise an invalid floating-point exception. The macro math_errhandling specifies whether either or both of these approaches is taken.


Functions lists:

Trigonometric functions

  1. cos - Compute cosine

  2. sin - Compute sine

  3. tan - Compute tangent

  4. acos - Compute arc cosine

  5. asin - Compute arc sine

  6. atan - Compute arc tangent

  7. atan2 - Compute arc tangent with two parameters


Hyperbolic functions

  1. cosh - Compute hyperbolic cosine

  2. sinh - Compute hyperbolic sine

  3. tanh - Compute hyperbolic tangent

  4. acosh - Compute area hyperbolic cosine

  5. asinh - Compute area hyperbolic sine

  6. atanh - Compute area hyperbolic tangent


Exponential and logarithmic functions

  1. exp - Compute exponential function

  2. frexp - Get significand and exponent

  3. dexp - Generate value from significand and exponent

  4. log - Compute natural logarithm

  5. log10 - Compute common logarithm

  6. modf - Break into fractional and integral parts

  7. exp2 - Compute binary exponential function

  8. expm1 - Compute exponential minus one

  9. ilogb - Integer binary logarithm

  10. log1p - Compute logarithm plus one

  11. log2 - Compute binary logarithm

  12. logb - Compute floating-point base logarithm

  13. scalbn - Scale significand using floating-point base exponent

  14. scalbln - Scale significand using floating-point base exponent (long)


Power functions

  1. pow - Raise to power

  2. sqrt - Compute square root

  3. cbrt - Compute cubic root

  4. hypot - Compute hypotenuse

Error and gamma functions

  1. erf - Compute error function

  2. erfc - Compute complementary error function

  3. tgamma - Compute gamma function

  4. lgamma - Compute log-gamma function


Rounding and remainder functions

  1. ceil - Round up value

  2. floor - Round down value

  3. fmod - Compute remainder of division

  4. trunc - Truncate value

  5. round - Round to nearest

  6. lround - Round to nearest and cast to long integer

  7. llround - Round to nearest and cast to long long integer

  8. rint - Round to integral value

  9. lrint - Round and cast to long integer

  10. llrint - Round and cast to long long integer

  11. nearbyint - Round to nearby integral value

  12. remainder - Compute remainder (IEC 60559)

  13. remquo - Compute remainder and quotient


Floating-point manipulation functions

  1. copysign - Copy sign

  2. nan - Generate quiet NaN

  3. nextafter - Next representable value

  4. nexttoward - Next representable value toward precise value


Minimum, maximum, difference functions

  1. fdim - Positive difference

  2. fmax - Maximum value

  3. fmin - Minimum value


Other functions

  1. fabs - Compute absolute value

  2. abs - Compute absolute value

  3. fma - Multiply-add


12. <setjmp.h>

This header file is used to perform control transfers that bypass the normal function call and return protocol.


Function lists:

  1. longjump - long jump


13. <signal.h>

This header file is used to specify how the program handles signals while it executes. A signal can report some exceptional behavior within the program, such as division by zero. Or a signal can report some asynchronous event outside the program, such as someone striking an interactive attention key on a keyboard.


Function lists:

  1. signal - Set function to handle signal

  2. raise - Generates a signal


14. <stdarg.h>

This header file is used to access the unnamed additional arguments (arguments with no corresponding parameter declarations) in a function that accepts a varying number of arguments. To access the additional arguments:

  • The program must first execute the macro va_start within the body of the function to initialize an object with context information.

  • Subsequent execution of the macro va_arg, designating the same context information, yields the values of the additional arguments in order, beginning with the first unnamed argument. You can execute the macro va_arg from any function that can access the context information saved by the macro va_start.

  • If you have executed the macro va_start in a function, you must execute the macro va_end in the same function, designating the same context information, before the function returns.


15. <stdbool.h>

This header file is used to define a type and several macros suitable for writing Boolean tests. The standard header <stdbool.h> is available even in a freestanding implementation.


16. <stddef.h>

This header file is used to define several types and macros that are of general use throughout the program. The standard header <stddef.h> is available even in a freestanding implementation.


Variables are :

  1. ptrdiff_t - This is the signed integral type and is the result of subtracting two pointers.

  2. size_t - This is the unsigned integral type and is the result of the sizeof keyword.

  3. wchar_t - This is an integral type of the size of a wide character constant.


17. <stdint.h>

This header file is used to define various integer types, and related macros, with size constraints. Note that the definitions shown for the types and macros are merely representative -- they can vary among implementations.


18. <stdio.h>

This header file is used so that you can perform input and output operations on streams and files.


Function lists:


Operations on files:

  1. remove - Remove file

  2. rename - Rename file

  3. tmpfile - Open a temporary file

  4. tmpnam - Generate temporary filename


File access:

  1. fclose - Close file

  2. fflush - Flush stream

  3. fopen - Open file

  4. freopen - Reopen stream with different file or mode

  5. setbuf - Set stream buffer

  6. setvbuf - Change stream buffering


Formatted input/output:

  1. fprintf - Write formatted data to stream

  2. fscanf - Read formatted data from stream

  3. printf - Print formatted data to stdout

  4. scanf - Read formatted data from stdin

  5. snprintf - Write formatted output to sized buffer

  6. sprintf - Write formatted data to string

  7. sscanf - Read formatted data from string

  8. vfprintf - Write formatted data from variable argument list to stream

  9. vfscanf - Read formatted data from stream into variable argument list

  10. vprintf - Print formatted data from variable argument list to stdout

  11. vscanf - Read formatted data into variable argument list

  12. vsnprintf - Write formatted data from variable argument list to sized buffer

  13. vsprintf - Write formatted data from variable argument list to string

  14. vsscanf - Read formatted data from string into variable argument list


Character input/output:

  1. fgetc - Get character from stream

  2. fgets - Get string from stream

  3. fputc - Write character to stream

  4. fputs - Write string to stream

  5. getc - Get character from stream

  6. getchar - Get character from stdin

  7. gets - Get string from stdin

  8. putc - Write character to stream

  9. putchar - Write character to stdout

  10. puts - Write string to stdout

  11. ungetc - Unget character from stream


Direct input/output:

  1. fread - Read block of data from stream

  2. fwrite - Write block of data to stream


File positioning:

  1. fgetpos - Get current position in stream

  2. fseek - Reposition stream position indicator

  3. fsetpos - Set position indicator of stream

  4. ftell - Get current position in stream

  5. rewind - Set position of stream to the beginning


Error-handling:

  1. clearerr - Clear error indicators

  2. feof - Check end-of-file indicator

  3. ferror - Check error indicator

  4. perror - Print error message


19. <stdlib.h>

This header file is used to declare an assortment of useful functions and to define the macros and types that help you use them.


String conversion

  1. atof - Convert string to double

  2. atoi - Convert string to integer

  3. atol - Convert string to long integer

  4. atoll - Convert string to long long integer

  5. strtod - Convert string to double

  6. strtof - Convert string to float

  7. strtol - Convert string to long integer

  8. strtold - Convert string to long double

  9. strtoll - Convert string to long long integer

  10. strtoul - Convert string to unsigned long integer

  11. strtoull - Convert string to unsigned long long integer


Pseudo-random sequence generation

  1. rand - Generate random number

  2. srand - Initialize random number generator


Dynamic memory management

  1. calloc - Allocate and zero-initialize array

  2. free - Deallocate memory block

  3. malloc - Allocate memory block

  4. realloc - Reallocate memory block


Environment

  1. abort - Abort current process

  2. atexit - Set function to be executed on exit

  3. at_quick_exit - Set function to be executed on quick exit

  4. exit - Terminate calling process

  5. getenv - Get environment string

  6. quick_exit - Terminate calling process quick

  7. system - Execute system command

  8. _Exit - Terminate calling process


Searching and sorting

  1. bsearch - Binary search in array

  2. qsort - Sort elements of array


Integer arithmetics

  1. abs - Absolute value

  2. div - Integral division

  3. labs - Absolute value

  4. ldiv - Integral division

  5. llabs - Absolute value

  6. lldiv - Integral division


Multibyte characters

  1. mblen - Get length of multibyte character

  2. mbtowc - Convert multibyte sequence to wide character

  3. wctomb - Convert wide character to multibyte sequence


Multibyte strings

  1. mbstowcs - Convert multibyte string to wide-character string

  2. wcstombs - Convert wide-character string to multibyte string


20. <string.h>

This header file is used to declare a number of functions that help you manipulate C strings and other arrays of characters.


Functions lists:


Copying:

  1. memcpy - Copy block of memory

  2. memmove - Move block of memory

  3. strcpy - Copy string

  4. strncpy - Copy characters from string


Concatenation:

  1. strcat - Concatenate strings

  2. strncat - Append characters from string


Comparison:

  1. memcmp - Compare two blocks of memory

  2. strcmp - Compare two strings

  3. strcoll - Compare two strings using locale

  4. strncmp - Compare characters of two strings

  5. strxfrm - Transform string using locale


Searching:

  1. memchr - Locate character in block of memory

  2. strchr - Locate first occurrence of character in string

  3. strcspn - Get span until character in string

  4. strpbrk - Locate characters in string

  5. strrchr - Locate last occurrence of character in string

  6. strspn - Get span of character set in string

  7. strstr - Locate substring

  8. strtok - Split string into tokens


Other:

  1. memset - Fill block of memory

  2. strerror - Get pointer to error message string

  3. strlen - Get string length


21. <tgmath.h>

This header file is used to define several families of generic functions. A generic function has the same name for two or more distinct parameter lists


22. <time.h>

This header file is used to declare several functions that help you manipulate times. The diagram summarizes the functions and the object types that they convert between.


Function lists:


Time manipulation

  1. clock - Clock program

  2. difftime - Return difference between two times

  3. mktime - Convert tm structure to time_t

  4. time - Get current time

Conversion

  1. asctime - Convert tm structure to string

  2. ctime - Convert time_t value to string

  3. gmtime - Convert time_t to tm as UTC time

  4. localtime - Convert time_t to tm as local time

  5. strftime - Format time as string


23. <uchar.h>

This header provides support for 16-bit and 32-bit characters, suitable to be encoded using UTF-16 and UTF-32.


Function lists:

  1. c16rtomb - Convert 16-bit character to multibyte sequence

  2. c32rtomb - Convert 32-bit character to multibyte sequence

  3. mbrtoc16 - Convert multibyte sequence to 16-bit character

  4. mbrtoc32 - Convert multibyte sequence to 32-bit character


24. <wchar.h>

This header file is used,so that you can perform input and output operations on wide streams or manipulate wide strings.


25. <wctype.h>

This header file is used to declare several functions that are useful for classifying and mapping codes from the target wide-character set.




The Tech Platform

0 comments
bottom of page