在c++中,宏/定义区分大小写

Are macros/definitions case sensitive in C++?

本文关键字:定义区 大小写 c++      更新时间:2023-10-16

假设您有一个名为Add.h的头文件。如果有

,程序会链接吗?
#ifndef ADD_H
#define Add_H

作为你的include守卫?

编辑:头文件

#include <iostream>
using namespace std;
#ifndef _RATIONAL_H
#define _Rational_H
class Rational
{
    long long _p;
    long long _q;
    void simp();
public:
    Rational();
    Rational( long long P, long long Q = 1);
    Rational( const Rational& );
};
#endif

是的,宏在c++中是区分大小写的。假设大多数编程语言中的所有内容都是区分大小写的,这可能是一条很好的经验法则。在只有少数几种语言中,您可以访问,例如,fooFoo作为相同的变量(我认为Visual Basic -不确定有多少其他语言)。

如果你对这么简单的东西不确定,最简单的方法就是用你自己的编译器试试:)

编辑:要知道它是否确实实际上工作,并且链接器不只是忽略它,因为它不一定需要定义(可能取决于ifdef/环境)尝试以下代码:
#include <iostream>
#define TEST
bool success = false;
#ifdef Test
bool success = true;
#endif
void main() {
    std::cout << success << std::endl;
    system("pause"); //Wait to press enter before closing
}

如果控制台显示数字1 (std::cout将true转换为1,将false转换为0,除非您告诉它否则),那么宏确实是大小写敏感的

更新:我刚刚尝试在Visual Studio 2015中编译上述代码,它确实给出了0的输出。