Shash Shash - 21 days ago 4
C Question

Why does #define not require a semicolon?

I was writing some test code in C. By mistake I had inserted a

;
after a
#define
, which gave me errors. Why is a semicolon not required for
#define
s?

More specifically :

Method 1: works

const int MAX_STRING = 256;

int main(void) {
char buffer[MAX_STRING];
}


Method 2: Does not work - compilation error.

#define MAX_STRING 256;

int main(void) {
char buffer[MAX_STRING];
}


What is the reason of the different behavior of those codes? Are those both MAX_STRINGs not constants?

Jon Jon
Answer

#define is a preprocessor directive, not a statement or declaration as defined by the C grammar (both of those are required to end with a semicolon). The rules for the syntax of each one are different.