By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Preprocessing directives

P: n/a
Ron
Please, consider the following code:

const int a = 1;
#if a == 1
#define VAR 200
#else
#define VAR 100
#endif

int main() {
cout << VAR << endl;
return 0;
}

Under some compiler it prints 200. So a preprocessing directive can check
constant values. Is it a standard C++ feature?

But how can be possible? Syntax analysis, following the standard, is done
after all preprocessing directive are executed, so how can an #if
be aware about a "const int" construct?

Regards.

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ron wrote:
Please, consider the following code:

#include <iostream>
using std::cout;
using std::endl;
const int a = 1;
#if a == 1
#define VAR 200
#else
#define VAR 100
#endif

int main() {
cout << VAR << endl;
return 0;
}

Under some compiler it prints 200. So a preprocessing directive can
check constant values. Is it a standard C++ feature?
I doubt that.
But how can be possible? Syntax analysis, following the standard, is
done after all preprocessing directive are executed, so how can an #if
be aware about a "const int" construct?


It can't. That's why I doubt it. But I'm not a language guru.
Jul 19 '05 #2

P: n/a
Rolf Magnus <ra******@t-online.de> wrote in message news:<bo*************@news.t-online.com>...
Ron wrote:
Please, consider the following code:


#include <iostream>
using std::cout;
using std::endl;
const int a = 1;
#if a == 1
#define VAR 200
#else
#define VAR 100
#endif

int main() {
cout << VAR << endl;
return 0;
}

Under some compiler it prints 200. So a preprocessing directive can
check constant values. Is it a standard C++ feature?


I doubt that.


FYI

VC 7.1 prints 100

/dan
Jul 19 '05 #3

P: n/a
On Tue, 11 Nov 2003 11:12:31 GMT, Ron <no@spam.com> wrote in
comp.lang.c++:
Please, consider the following code:

const int a = 1;
#if a == 1
#define VAR 200
#else
#define VAR 100
#endif

int main() {
cout << VAR << endl;
return 0;
}

Under some compiler it prints 200. So a preprocessing directive can check
constant values. Is it a standard C++ feature?
Any compiler that outputs 200 is broken. It is not a standard
feature, it is a non-conforming "feature".
But how can be possible? Syntax analysis, following the standard, is done
after all preprocessing directive are executed, so how can an #if
be aware about a "const int" construct?

Regards.


There are almost an infinite number of ways one could write a compiler
to create this defect. If you want to know which one occurred in this
particular compiler, you need to either examine the source code if
available, or ask the people who wrote it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.