C Header Files
\n -- \n \n - Home
\n - HTML
\n - JavaScript
\n - CSS
\n - Vue
\n - React
\n - Python3
\n - Java
\n - C
\n - C++
\n - C#
\n - AI
\n - Go
\n - SQL
\n - Linux
\n - VS Code
\n - Bootstrap
\n - Git
\n - Local Bookmarks
\n
\n\n \n\n \n - Vue3 Tutorial
\n - Vue2 Tutorial
\n
\n\n \n - Bootstrap3
\n - Bootstrap4
\n - Bootstrap5
\n
\n\n \n - Machine Learning
\n - PyTorch
\n - TensorFlow
\n - Sklearn
\n - NLP
\n - AI Agent
\n - Ollama
\n - Coding Plan
\n
\n\n C Tutorial
\n \n - C Language Tutorial
\n - C Introduction
\n - C Environment Setup
\n - C VScode
\n - C Program Structure
\n - C Basic Syntax
\n - C Data Types
\n - C Variables
\n - C Constants
\n - C Storage Classes
\n - C Operators
\n - C Decision Making
\n - C Loops
\n - C Functions
\n - C Scope Rules
\n - C Arrays
\n - C enum (Enumeration)
\n - C Pointers
\n - C Function Pointers and Callback Functions
\n - C Strings
\n - C Structures
\n - C Unions
\n - C Bit Fields
\n - C typedef
\n - C Input & Output
\n - C File I/O
\n - C Preprocessors
\n - C Header Files
\n - C Type Casting
\n - C Error Handling
\n - C Recursion
\n - C Variable Arguments
\n - C Memory Management
\n - C Undefined Behavior
\n - C Command Line Arguments
\n - C Safe Functions
\n - C Sorting Algorithms
\n - C Project Structure
\n - C Examples
\n - C Classic 100 Examples
\n - C Quiz
\n
\n\n C Standard Library
\n \n - C Standard Library - Reference Manual
\n - <a href="#" title="C Standard Library - ">C Standard Library -
\n
Deep Dive
\n- \n
- Network Design and Development \n
- Scripting \n
- Web Services \n
- Computer Science \n
- Programming \n
- Software \n
- Programming Languages \n
- Scripting Languages \n
- Development Tools \n
- Web Service \n
C Header Files
\nHeader files are files with the extension **.h**, containing C function declarations and macro definitions, referenced and shared across multiple source files. There are two types of header files: those written by programmers and those that come with the compiler.
\nTo use a header file in a program, you need to use the C preprocessor directive **#include** to include it. We've already seen the **stdio.h** header file earlier, which is a header file that comes with the compiler.
\nIncluding a header file is equivalent to copying the content of the header file. However, we don't directly copy the content of header files into source files because doing so can easily lead to errors, especially when a program is composed of multiple source files.
\nA simple practice in C or C++ programs is to write all constants, macros, system global variables, and function prototypes in header files, and include these header files whenever needed.
\n\nSyntax for Including Header Files
\nYou can include user and system header files using the preprocessor directive **#include**. It has the following two forms:
\n#include <file>\n This form is used to include system header files. It searches for a file named `file` in the standard list of system directories. When compiling source code, you can prepend directories to this list using the -I option.
\n#include "file"\n This form is used to include user header files. It searches for a file named `file` in the directory containing the current file. When compiling source code, you can prepend directories to this list using the -I option.
\n\nOperation of Including Header Files
\nThe **#include** directive instructs the C preprocessor to process the specified file as input. The preprocessor's output includes the already generated output, the output generated by the included file, and the text output after the **#include** directive. For example, if you have a header file header.h as follows:
\nchar *test (void);\n And a main program _program.c_ that uses this header file, as follows:
\nint x;\n#include "header.h"\nint main (void)\n{\n puts (test ());\n}\n The compiler will see the following code information:
\nint x;\nchar *test (void);\nint main (void)\n{\n puts (test ());\n}\n\n Including a Header File Only Once
\nIf a header file is included twice, the compiler will process the content of the header file twice, which will generate errors. To prevent this, the standard practice is to place the entire content of the file inside a conditional compilation statement, as follows:
\n#ifndef HEADER_FILE\n #define HEADER_FILE\n the entire header file file\n#endif\n This structure is commonly known as an **#ifndef** wrapper. When the header file is included again, the condition is false because HEADER_FILE is already defined. At this point, the preprocessor will skip the entire content of the file, and the compiler will ignore it.
\n\nConditional Inclusion
\nSometimes it is necessary to select one from multiple different header files to include in a program. For example, you may need to specify configuration parameters to be used on different operating systems. You can achieve this through a series of conditions, as follows:
\n#if SYSTEM_1\n # include "system_1.h"\n#elif SYSTEM_2\n # include "system_2.h"\n#elif SYSTEM_3\n ...\n#endif\n However, this approach is very inappropriate when there are many header files. The preprocessor uses macros to define the name of the header file. This is called **conditional inclusion**. Instead of using the header file name as a direct argument to **#include**, you can simply use a macro name instead:
\n#define SYSTEM_H "system_1.h"\n...\n#include SYSTEM_H\n SYSTEM_H will be expanded, and the preprocessor will search for system_1.h, just as if **#include** was originally written that way. SYSTEM_H can be defined by your Makefile via the -D option.
\n\n\n\n
Standard Library Header Files
\nC Standard Library Header Files are a set of header files defined by the ANSI C (also known as C89/C90) and ISO C (C99 and C11) standards. These header files provide a large number of functions, macros, and type definitions for handling common programming tasks such as input/output, string manipulation, mathematical calculations, memory management, etc.
\nHere are some common C standard library header files and a brief description of their functions:
\n\n| Header File | \nFunction Description | \n
|---|---|
| <stdio.h> | \nStandard Input/Output library, includes functions like `printf`, `scanf`, etc. | \n
| <stdlib.h> | \nStandard library functions, includes memory allocation, program control functions, etc. | \n
| <string.h> | \nString manipulation functions, such as `strlen`, `strcpy`, etc. | \n
| <math.h> | \nMathematical function library, such as `sin`, `cos`, `sqrt`, etc. | \n
| <time.h> | \nTime and date functions, such as `time`, `strftime`, etc. | \n
| <ctype.h> | \nCharacter handling functions, such as `isalpha`, `isdigit`, etc. | \n
| <limits.h> | \nDefines limit values for various types, such as `INT_MAX`, etc. | \n
| <float.h> | \nDefines limit values for floating-point types, such as `FLT_MAX`, etc. | \n
| <assert.h> | \nAssertion macro `assert`, used for debugging checks. | \n
| <errno.h> | \nDefines the error code variable `errno` and related macros. | \n
| <stddef.h> | \nDefines common types and macros, such as `size_t`, `NULL`, etc. | \n
| <signal.h> | \nFunctions and macros for handling signals, such as `signal`, etc. | \n
| <setjmp.h> | \nMacros and functions providing non-local jump functionality. | \n
| <locale.h> | \nLocalization-related functions and macros, such as `setlocale`, etc. | \n
2 Notes Write a Note
\n- \n
- PPBgogogo
188***57825@163.com 818
When you have multiple **.h** files and multiple **.c** files, we often use a global.h header file to include all **.h** files. Then, in header files other than global.h, including global.h can achieve the inclusion of all header files without causing confusion. This makes it convenient to call functions or variables from other files in various files.#ifndef _GLOBAL_H\n #define _GLOBAL_H\n #include <fstream>\n #include <iostream>\n #include <math.h>\n #include <Config.h>
(javascript:;) PPBgogogo
188***57825@163.com 9 years ago (2017-09-04) \n - Xixi
632***357@qq.com 508
**What is the difference between `include ` and `include ""` in C language?**#include <>includes header files from the compiler's class library path.#include ""includes header files from the relative path in your program directory. If the included header file is not found in the program directory, it will then search for it in the compiler's class library path directory.
(#) Xixi
632***357@qq.com 7 years ago (2019-03-12) \n
Click me to share notes
\n(javascript:;)
\n- \n
- Bold text \n
- Insert code \n
- Unordered list \n
YouTip