A method that is referenced only once by a C header file

When we write the header file ourselves, if we do not add some unique methods, it may cause repeated references, cause code redundancy, take up a lot of space, and reduce efficiency. Therefore, it is very important to ensure that it is referenced only once. Here are two methods:

1、#pragma once 

This method is relatively simple. We only need to write #pragma once in the first line of the header file we write.

2. #ifndef header file name #define header file name #endif

The header file name here is changed to: add two underscores before and after, click to replace the underscores

#pragma once  // 第一种只包含一次头文件的方法


// 下面是  第二种只包含一次头文件的方法
#ifndef __Add_h__      // if not def  如果没有定义 Add.h
#define __Add_h__      // 定义Add.h

int Add(int a, int b);  // 声明 函数原型

#endif                 // 结束if


// 这样就能保证自己所引用的头文件只引用一次,防止多次引用的事情发生。

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/132683339