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:
Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them.
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
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 :
isalnum() : This function checks whether or not a character is alphanumeric
isdigit(): This function checks whether or not character is decimal digit.
tolower(): This function checks whether or not character is uppercase and then convert it to lowercase.
toupper(): Convert lowercase to uppercase.
isblank(): check whether or not character is blank.
iscntrl(): check whether or not character is control character
isdigit(): check whether or not character is decimal
isgraph(): check whether or not character has graphical representation
isprint(): check whether or not character is printable.
ispunct(): check whether or not character is a punctuation character.
isxdigit(): check whether or not character is white-space
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:
EDOM: This macro represents domain error for a function call, in which an input argument outside the function's domain is passed
ERANGE : This macro represents range error, in which either the input argument or result of the function is outside a function's range.
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:
feclearexcept() : clear floating-point exception
feraiseexcept(): Raise floating-point exception
fegetexceptflag(): Get floating-point exception flags
fesetexceptflag() : Set floating-point exception flags
fegetround(): Get rounding direction mode
fesetround() : Set rounding direction mode
fegetenv(): Get floating-point environment
fesetenv(): Set floating-point environment
feholdexcept(): Hold floating-point exceptions
feupdateenv(): Update floating-point environment
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
CHAR_BIT - Number of bits in a char object
SCHAR_MIN - Minimum value for an object of type signed char
SCHAR_MAX - Maximum value for an object of type signed char
UCHAR_MAX - Maximum value for an object of type unsigned char
CHAR_MIN - Minimum value for an object of type char
CHAR_MAX - Maximum value for an object of type char
MB_LEN_MAX - Maximum number of bytes in a multibyte character, for any locale
SHRT_MIN - Minimum value for an object of type short int
SHRT_MAX - Maximum value for an object of type short int
USHRT_MAX- Maximum value for an object of type unsigned short int
INT_MIN - Minimum value for an object of type int
INT_MAX - Maximum value for an object of type int
UINT_MAX - Maximum value for an object of type unsigned int
LONG_MIN - Minimum value for an object of type long int
LONG_MAX - Maximum value for an object of type long int
ULONG_MAX - Maximum value for an object of type unsigned long int
LLONG_MIN - Minimum value for an object of type long long int
LLONG_MAX - Maximum value for an object of type long long int
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:
setlocale - Set or retrieve locale
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
cos - Compute cosine
sin - Compute sine
tan - Compute tangent
acos - Compute arc cosine
asin - Compute arc sine
atan - Compute arc tangent
atan2 - Compute arc tangent with two parameters
Hyperbolic functions
cosh - Compute hyperbolic cosine
sinh - Compute hyperbolic sine
tanh - Compute hyperbolic tangent
acosh - Compute area hyperbolic cosine
asinh - Compute area hyperbolic sine
atanh - Compute area hyperbolic tangent
Exponential and logarithmic functions
exp - Compute exponential function
frexp - Get significand and exponent
dexp - Generate value from significand and exponent
log - Compute natural logarithm
log10 - Compute common logarithm
modf - Break into fractional and integral parts
exp2 - Compute binary exponential function
expm1 - Compute exponential minus one
ilogb - Integer binary logarithm
log1p - Compute logarithm plus one
log2 - Compute binary logarithm
logb - Compute floating-point base logarithm
scalbn - Scale significand using floating-point base exponent
scalbln - Scale significand using floating-point base exponent (long)
Power functions
pow - Raise to power
sqrt - Compute square root
cbrt - Compute cubic root
hypot - Compute hypotenuse
Error and gamma functions
erf - Compute error function
erfc - Compute complementary error function
tgamma - Compute gamma function
lgamma - Compute log-gamma function
Rounding and remainder functions
ceil - Round up value
floor - Round down value
fmod - Compute remainder of division
trunc - Truncate value
round - Round to nearest
lround - Round to nearest and cast to long integer
llround - Round to nearest and cast to long long integer
rint - Round to integral value
lrint - Round and cast to long integer
llrint - Round and cast to long long integer
nearbyint - Round to nearby integral value
remainder - Compute remainder (IEC 60559)
remquo - Compute remainder and quotient
Floating-point manipulation functions
copysign - Copy sign
nan - Generate quiet NaN
nextafter - Next representable value
nexttoward - Next representable value toward precise value
Minimum, maximum, difference functions
fdim - Positive difference
fmax - Maximum value
fmin - Minimum value
Other functions
fabs - Compute absolute value
abs - Compute absolute value
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:
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:
signal - Set function to handle signal
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 :
ptrdiff_t - This is the signed integral type and is the result of subtracting two pointers.
size_t - This is the unsigned integral type and is the result of the sizeof keyword.
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:
remove - Remove file
rename - Rename file
tmpfile - Open a temporary file
tmpnam - Generate temporary filename
File access:
fclose - Close file
fflush - Flush stream
fopen - Open file
freopen - Reopen stream with different file or mode
setbuf - Set stream buffer
setvbuf - Change stream buffering
Formatted input/output:
fprintf - Write formatted data to stream
fscanf - Read formatted data from stream
printf - Print formatted data to stdout
scanf - Read formatted data from stdin
snprintf - Write formatted output to sized buffer
sprintf - Write formatted data to string
sscanf - Read formatted data from string
vfprintf - Write formatted data from variable argument list to stream
vfscanf - Read formatted data from stream into variable argument list
vprintf - Print formatted data from variable argument list to stdout
vscanf - Read formatted data into variable argument list
vsnprintf - Write formatted data from variable argument list to sized buffer
vsprintf - Write formatted data from variable argument list to string
vsscanf - Read formatted data from string into variable argument list
Character input/output:
fgetc - Get character from stream
fgets - Get string from stream
fputc - Write character to stream
fputs - Write string to stream
getc - Get character from stream
getchar - Get character from stdin
gets - Get string from stdin
putc - Write character to stream
putchar - Write character to stdout
puts - Write string to stdout
ungetc - Unget character from stream
Direct input/output:
fread - Read block of data from stream
fwrite - Write block of data to stream
File positioning:
fgetpos - Get current position in stream
fseek - Reposition stream position indicator
fsetpos - Set position indicator of stream
ftell - Get current position in stream
rewind - Set position of stream to the beginning
Error-handling:
clearerr - Clear error indicators
feof - Check end-of-file indicator
ferror - Check error indicator
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
atof - Convert string to double
atoi - Convert string to integer
atol - Convert string to long integer
atoll - Convert string to long long integer
strtod - Convert string to double
strtof - Convert string to float
strtol - Convert string to long integer
strtold - Convert string to long double
strtoll - Convert string to long long integer
strtoul - Convert string to unsigned long integer
strtoull - Convert string to unsigned long long integer
Pseudo-random sequence generation
rand - Generate random number
srand - Initialize random number generator
Dynamic memory management
calloc - Allocate and zero-initialize array
free - Deallocate memory block
malloc - Allocate memory block
realloc - Reallocate memory block
Environment
abort - Abort current process
atexit - Set function to be executed on exit
at_quick_exit - Set function to be executed on quick exit
exit - Terminate calling process
getenv - Get environment string
quick_exit - Terminate calling process quick
system - Execute system command
_Exit - Terminate calling process
Searching and sorting
bsearch - Binary search in array
qsort - Sort elements of array
Integer arithmetics
abs - Absolute value
div - Integral division
labs - Absolute value
ldiv - Integral division
llabs - Absolute value
lldiv - Integral division
Multibyte characters
mblen - Get length of multibyte character
mbtowc - Convert multibyte sequence to wide character
wctomb - Convert wide character to multibyte sequence
Multibyte strings
mbstowcs - Convert multibyte string to wide-character string
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:
memcpy - Copy block of memory
memmove - Move block of memory
strcpy - Copy string
strncpy - Copy characters from string
Concatenation:
strcat - Concatenate strings
strncat - Append characters from string
Comparison:
memcmp - Compare two blocks of memory
strcmp - Compare two strings
strcoll - Compare two strings using locale
strncmp - Compare characters of two strings
strxfrm - Transform string using locale
Searching:
memchr - Locate character in block of memory
strchr - Locate first occurrence of character in string
strcspn - Get span until character in string
strpbrk - Locate characters in string
strrchr - Locate last occurrence of character in string
strspn - Get span of character set in string
strstr - Locate substring
strtok - Split string into tokens
Other:
memset - Fill block of memory
strerror - Get pointer to error message string
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
clock - Clock program
difftime - Return difference between two times
mktime - Convert tm structure to time_t
time - Get current time
Conversion
asctime - Convert tm structure to string
ctime - Convert time_t value to string
gmtime - Convert time_t to tm as UTC time
localtime - Convert time_t to tm as local time
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:
c16rtomb - Convert 16-bit character to multibyte sequence
c32rtomb - Convert 32-bit character to multibyte sequence
mbrtoc16 - Convert multibyte sequence to 16-bit character
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
Commenti