Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are macros/definitions case sensitive in C++?

Tags:

c++

Say you had a header file named Add.h. Would the program link if you had

#ifndef ADD_H
#define Add_H

as your include guards?

Edit: Header file

#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
like image 253
Genbowl Avatar asked Oct 20 '15 03:10

Genbowl


1 Answers

Yes, macros are case sensitive in C++. It's probably a good rule of thumb to assume that everything in most programming languages is case sensitive. In only a few languages would you be able to access, for example, foo and Foo as the same variable (I'm thinking Visual Basic - not sure how many others there are).

If you're unsure with something as simple as this, the easiest thing to do would be to try it out with your own compiler :)

Edit: To know whether or not it does actually work, and the linker isn't just overlooking it because it doesn't necessarily need to be defined (might happen depending on the ifdef logic/circumstances) try the following code:

#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
}

If the console displays the number 1 (std::cout converts true to 1 and false to 0, unless you tell it to do otherwise) then the macros are, indeed, case insensitive

Update: I just tried compiling the above code in Visual Studio 2015, and it does give an output of 0.

like image 139
brads3290 Avatar answered Sep 29 '22 04:09

brads3290