Some basic syntax of C language include statement (introducing arbitrary files, etc.)

First, the basic syntax of the include statement in c language

First of all, as we all know, the #include statement of c language can introduce files with any suffix.

Let's see how it can import related files

1. Import standard library header files

This is the general usage, for example:

#include<stdio.h>

This is to introduce the standard input and output header file. With it, you can use related input and output functions in the following code.

In this usage, include is followed by angle brackets, and the compiler will look for this header file in the system directory.

2. Import custom files

(1) Custom files in the same directory

For example:

Let’s look at a question first: How to reference a function in another .c file in the same directory in the current .c file?

Suppose the current file name is main.c, and another .c file name is test.c.

Under normal circumstances, we declare the function in the .h file, and then implement the function in the .c file with the same name. Then use include to import the .h file in another .c file

But now, for the sake of simplicity and the ability to run the results , I directly import test.c with include in main.c.

The specific method is as follows:

For example you have two files: main.c, test.c. Both files are in the same directory.

A void type function test_out() is defined in test.c;,

Now you want to call this function inside main.c.

The contents of the two files are as follows:

test.c

#include<stdio.h>
void test_out()
{
	puts("you get it!");
}
/*
如果要在另一个.c文件里引入test.c文件,那么test.c文件里面是不能写main函数的,
否则两个文件里面都有main函数,就重复了.
*/

main.c

#include<stdio.h>
#include "test.c"
int main()
{
	test_out();
	return 0;
}

The result of running main.c:

It can be seen that in this usage, the include in the main.c file is not followed by angle brackets, but double quotes.

include "file name" can be used to import the existing header files of the system, and can also be used to introduce user-defined files.

Examples of other file types:

Just take a file format: abccd

There are two files in the same directory: main2.c and test2.abccd

The file named test2, its file type is arbitrary, the content inside is:

#include<stdio.h>
void test_out()
{
	puts("succeed!");
}

The contents of main2.c are:

#include<stdio.h>
#include "test2.abccd"
int main()
{
	test_out();
	return 0;
}

The result of running main2.c is:

why?

Assuming #include "file 1", the whole code is in line A of the current file,

Then the function of this code is to replace the content of line A of the current file with the entire content of file 1

 You can also try other types of files such as txt, which is actually feasible.

(2) Files in other directories:

Take Windows as an example, create a custom folder under the D disk, the name is abc, and there is a header file in it xyz.h, then use it at the beginning of the program #include "D:\\abc\xyz.h"to import the header file.

3. Prevent repeated introduction

Suppose your test.h is a header file that may be introduced repeatedly. In order to prevent it from being introduced repeatedly, we can do this:

#ifndef _TEST_H_//文件名大写
#define _TEST_H_//文件名大写
#include<stdio.h>
void test_out();//全局变量或者函数要被其他语句包围
int a;//全局变量或者函数要被其他语句包围
#endif

Among them, ifndef in the first line is actually the abbreviation of if not define.

When test.h is included for the first time, since _TEST_H is not defined, the condition is true, which will include (execute) #ifndef _TEST_H and

The code between #endif, when test.h is included for the second time, _TEST_H has been defined in the previous time, the condition is false, #ifndef _TEST_H and

The code between #endif will not be included again, thus avoiding redefinition.

In this way, we put the content of the header file in #ifndef and #endif. Regardless of whether your header file will be referenced by multiple files, you'd better add this.

Reference material: Compilation principle--the relationship between C language C files and header files- Know about

If there is any mistake, please correct me, communicate politely, thank you very much

Note, the above running results are from software dev c++ running screenshots, and VC6.0 can also run with correct results.

Guess you like

Origin blog.csdn.net/fly_view/article/details/127024181