C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Preprocessing directives

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Jack Adam
Guest





PostPosted: Thu Nov 13, 2003 3:35 am    Post subject: Re: Preprocessing directives Reply with quote





Ron wrote:
Quote:
Please, consider the following code:

#include using namespace std;

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?

A processing directive like that can check an integral constant
expression, but prior to evaluation the identifiers that are macros are
replaced with their expansion and the other identifiers are replaced
with 0. There are also some peculiarities when evaluating the expression.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Richard Smith
Guest





PostPosted: Thu Nov 13, 2003 3:35 am    Post subject: Re: Preprocessing directives Reply with quote



Ron wrote:

Quote:
#include using namespace std;

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.

There seems to me to be no doubt that this is a perfectly
legal program that should print 100. The processor knows
nothing about the variable a -- the definition will not have
been parsed by the time the #if directive is evaluated.
When the preprocessor sees an unknown identifier, it will
replace it with the number 0 [see 16.1/4]. Thus the
conditional of the #if directive reads 0 == 1, which is
clearly false. Thus, VAR is defined to 100 and that is what
the program should output.

I'm intrigued that you have found a compiler under which it
prints 200. Which compiler is this?

--
Richard Smith

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Steve Clamage
Guest





PostPosted: Fri Nov 14, 2003 2:36 am    Post subject: Re: Preprocessing directives Reply with quote



Ron wrote:
Quote:
Please, consider the following code:

#include <iostream
using namespace std;

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.

That compiler does nto conform to the rules about phases of translation
in section 2.1 of the C++ standard.

In the line
#if a == 1
the 'a' can refer only to a name defined in a #define directive, not to
the 'a' in the const int declaration. If no such 'a' has been #define'd,
this 'a' has the value 0.

(I'm assuming the course.)

--
Steve Clamage, [email]stephen.clamage (AT) sun (DOT) com[/email]

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.