473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Preprocessor and standard

Given is a short code snippet. Executing this on VsC++2003 gives output
TEXT and Hello. Is this correct according to the standard? Why not Hello
and Hello?

#include <iostream>

using namespace std;

#define TEXT Hello
#define QUOTE(x) #x
#define QUOTE2(x) QUOTE(x)

#define DIRECT QUOTE(TEXT)
#define INDIRECT QUOTE2(TEXT)

int main()
{
cout << DIRECT << endl;
cout << INDIRECT << endl;

return 0;
}

--
http://kaba.hilvi.org
Oct 21 '05 #1
3 1319
Looks correct to me, I think this opens when you think it this way: the
QUOTE2 macro will substitute the TEXT with Hello, because you don't
have # in that macro.

Therefore the second macro you call will see #x being the definition of
the first, hence, Hello.

In short: looks logical and correct.. but could be wrong, what seems to
be the problem?

Oct 21 '05 #2
Kalle Rutanen wrote:
Given is a short code snippet. Executing this on VsC++2003 gives output
TEXT and Hello. Is this correct according to the standard? Why not Hello
and Hello?

#include <iostream>

using namespace std;

#define TEXT Hello
#define QUOTE(x) #x
#define QUOTE2(x) QUOTE(x)

#define DIRECT QUOTE(TEXT)
#define INDIRECT QUOTE2(TEXT)

int main()
{
cout << DIRECT << endl;
cout << INDIRECT << endl;

return 0;
}


Yes, it's correct according to the Standard. The difference is in the
[pre-]processing of a macro that expands into another macro and a macro
that expands into #x. In the former, the substitution of all macros
happens. In the latter, macros that preceded by '#' are not substituted
further. Or something like that, anyway. See clause 16.3.

DIRECT --> full substitution --> QUOTE(TEXT) --> since it expands in #x,
the 'TEXT' is not substituted --> "TEXT"

INDIRECT --> full substitution --> QUOTE2(TEXT) --> full substitution -->
QUOTE(Hello) --> since it expands into #x, the 'Hello' is not replaced -->
"Hello"

V
Oct 21 '05 #3
Thanks persenaama and Victor, that made it clearer.

--
http://kaba.hilvi.org
Oct 22 '05 #4

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

Similar topics

13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
5
by: Water Cooler v2 | last post by:
Sorry if this is a foolish question, but here it is. I see things like #ifndef STDC ...blah #endif #ifdef WIN32
2
by: Prashant Mahajan | last post by:
I just wanted comments from you all on the following topic: Let's say we have 2, C code files namely file1.c and file2.c. file1.c contains few pre-processor definations say #define TEST1 10 We...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
6
by: olivier.grant | last post by:
Hi All, I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE ) and end up with the following preprocessed code : #include NAME.hpp
8
by: mohdalibaig | last post by:
C preprocessor generates the expanded version of our source code. In order to view this expanded code, I made use of the command cpp filename.c on the dos prompt. This command generates a file of...
2
by: =?Utf-8?B?bWFyaw==?= | last post by:
I am contemplating converting a huge collection of functions from C++ to VB.NET. I am concerned with a style used in the original code where a variable is set to a constant value with the # define...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
3
by: Nathan Moinvaziri | last post by:
I am wonder if there is a way to use preprocessor definitions to expose code only if a particular file is included in a project. I am targeting the msvc. I am thinking of something like #if...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.