473,770 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preprocessing directives

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
3 2145
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
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
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2050
by: Dave | last post by:
Hello all, In standard C++, is it required that preprocessing directives (#include, #if, etc...) start in column 0, or is that just a C thing? Thank, Dave
12
1984
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
1
2760
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many colors are referred in different subsystems in our projects. They are defined as enumeration constants and a single color must be the same value all across our projects.
5
5511
by: cody | last post by:
the following leads to an error (note that TEST is not defined): #if TEST string s = @" #"; // <-- the error is "preprocessing directive expected" #endif also, here we get the same error: #if TEST
12
8246
by: Thomas Carter | last post by:
Imagine that there is some include file f.h that contains the following line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it possible for the preprocessor to detect that ui32 already exists, when preprocessing F.c? The idea is that F.c will typedef ui32 as above if and only if such typedef is not already in some include file used by F.c.
18
2163
by: gutmant | last post by:
Say you have a file, a.h with an include guard. If you include it twice and look at the preprocessed output, you see there's no sign for the second inclusion. However, if you include it twice - once from a relative path, and once from an absolute one - you see that the second inclusion indeed occurs (enters the file and leaves immediately due to the include guard). Why does this happen (I have my speculations, but I want some...
11
2363
by: andreyvul | last post by:
What I'm trying to do is have the preprocessor parse one file twice. The file has three parts, and each is dependent on the previous. Example (file name is foo.c): #ifndef ONCE /* first part */ #define D #define #define ONCE #include "foo.c" #endif #ifdef ONCE /* second part */ D x int
5
6299
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing directive in the middle of macro arguments, which comes handy e.g. to
40
1790
by: Bill Cunningham | last post by:
I have been thinking about hiding headers from my compiler's preprocessor and allowing others to be shown. I want to use the most used and add others is necessary. Would this be how it is properly done. I want to ask ahead of time because what I do might work but it might not be the "correct" or standard way. #include <stdio.h> #include <stdlib.h> #ifdef LINUX #include <unistd.h>
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.