views:

380

answers:

3

Is there a particular reason when C / C++ were created that #elif was chosen over #elsif?

if ( ) {

} else if ( ) {

} else {

}

Fixed syntax

+5  A: 

Your example and your question don't match. There is no elsif or even elseif in C.

I don't know why the preprocessor uses #elif rather than #elsif, just the preference of Messrs. Kernighan and Richie, I suppose. The fact that they're different in perl (elsif) and python (elif) annoys me to no end.

Graeme Perrow
I agree which is why I asked the question.. thanks for your response
0A0D
It's Ritchie, actually. And only he designed the language, with some help one suspects from Ken Thompson. Brian Kernighan co-wrote the book with DMR much later.
anon
Graeme Perrow
+10  A: 

The reason it is different is because the preprocessor is independent of the compiler. It may be a separate program that can be used on other programming languages and file types. Typically, the C compiler first runs the preprocessor on a file to generate a (temporary) new file and then compiles the new file (the new file can be saved with the appropriate compiler flags if needed). See the preprocessor information here.

As to why the preprocessor is different, I can only speculate but I would guess that since there is a space in the C else if, they decided to imitate the UNIX sh 'elif'. Apparently the preprocessor in C came into existence in 1972 (taken from the article on B).

Ryan
The preprocessor is part of both C and C++, being a designated step in the compile process.
David Thornley
This answers it: "The language of preprocessor directives is agnostic to the grammar of C" from your link
0A0D
I think he means that the C++ preprocessor is no different from the C one.
Earlz
Neither the C nor the C++ standard requires the preprocessor to be a separate program.
anon
Note that the preprocessor is not required to be independent of the compiler. (I don't know if it is on various Windows compilers; it usually is a separate program on Linux/Unix compilers.)
David Thornley
Standard != rationale. The first implementations determined how the language developed, and thus their structure matters to the C grammar. The C standard did refine the language a bit, but basic choices like the preprocessor phase were already set in stone.
MSalters
A: 

For a history of C by one of its creators, see http://cm.bell-labs.com/cm/cs/who/dmr/chist.html. The preprocessor was considered a seperate step, fitting in with UNIX's tradition of providing independent building blocks.

MSalters