Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do preprocessor commands have to start as first nonwhite space

I am trying to do a #ifndef part way through a setter line and I received this error

"Error 20 error C2014: preprocessor command must start as first nonwhite space"

I am aware of the error means, I am just curious of why it is like that? Is it a compiler choice? What is the reasoning behind this? That it is easier for the user to notice?

Here is the code if someone is wondering:

inline void SetSomething(int value) { #ifndef XBOX ASSERT(value <= 1); #endif test = value; };
like image 715
marsh Avatar asked Dec 04 '25 10:12

marsh


2 Answers

At first C did not have any standard preprocessor. Then people started using preprocessing as an external tool. You might note that the # is the same as with comments in general in Unix-land shell scripts.

As the language evolved the preprocessor became integrated with the compiler and more part of the language proper, but it kept its totally different structure, namely, in particular that it's line oriented while the C and C++ core languages are free form.

After that the lines have blurred a bit more. Now the preprocessing typically adds #line directives or the equivalent for use by the core language compiler, also #pragma directives are for the core language compiler, and in the other direction we now have _Pragma (IIRC). Still the structure is mostly as it was originally. C and C++ are evolved languages, not designed languages.

like image 81
Cheers and hth. - Alf Avatar answered Dec 07 '25 14:12

Cheers and hth. - Alf


Taking a look into the standard (section 16 "Preprocessing Directives") starting with # as the frirst non whitespace character is what makes a preprocessing directive by definition.

A preprocessing directive consists of a sequence of preprocessing tokens that satisfies the following constraints: The first token in the sequence is a # preprocessing token that (at the start of translation phase 4) is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character.

like image 45
mikyra Avatar answered Dec 07 '25 15:12

mikyra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!