473,394 Members | 1,875 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,394 software developers and data experts.

A question on macro expansion

Ark
Hello, NG,
Please, help on this snippet:

#define CAT(a,b) a##b
#define COMMENT CAT(/,/)
COMMENT This is a comment

Should it compile?
It passes MS C/C++ 13.0 (Visual Studio 2002) and fails IAR ARM
compiler.
IAR claims that comments should not be processed after macro
expansion, so I have an odd situation where the preprocessor output is
as intended and is compilable, whereas the original file is not.

What is the truth, according to the standard(s)?
(I myself can read but cannot parse 'em :)

Thank you very much,
Ark
Nov 14 '05 #1
3 2519
Ark wrote:
Hello, NG,
Please, help on this snippet:

#define CAT(a,b) a##b
#define COMMENT CAT(/,/)
COMMENT This is a comment

Should it compile?
No.
It passes MS C/C++ 13.0 (Visual Studio 2002) and fails IAR ARM
compiler.
IAR claims that comments should not be processed after macro
expansion, so I have an odd situation where the preprocessor output is
as intended and is compilable, whereas the original file is not.

What is the truth, according to the standard(s)?
(I myself can read but cannot parse 'em :)


Comments are removed in translation phase 3. Macros
are expanded in phase 4.

"The preprocessor output" is really just a sort of
courtesy many compilers extend. The Standard does not
require that such output be obtainable, and certainly
doesn't require that the output (if any) have the same
meaning as the code from which it was produced. This
is a case of the latter situation: The preprocessed
output is valid C, but the source from which it was
generated is not.

--
Er*********@sun.com

Nov 14 '05 #2
This should not compile according to the standard.
According to Microsoft yes.

Problem is that windows header files use this feature to
selectiveley comment out portions of the code
according to macro definitions embedded in
#ifdefs, well the usual stuff.

This was one of the many problems with those headers.
They tend to be very Microsoft specific.
Nov 14 '05 #3
ark

"jacob navia" <ja***@jacob.remcomp.fr> wrote in message
news:cd**********@news-reader5.wanadoo.fr...
This should not compile according to the standard.
According to Microsoft yes.

Problem is that windows header files use this feature to
selectiveley comment out portions of the code
according to macro definitions embedded in
#ifdefs, well the usual stuff.

This was one of the many problems with those headers.
They tend to be very Microsoft specific.

While we are at it...
#define CAT(a,b) a##b
#define CATB(a,b,c) CAT(CAT(a,b),c)
#define CATA(a,b,c) CAT(a,CAT(b,c))
Should I expect that CATB(pro, duct, ion) expand to production, and same for
CATA ?
Thanks,
- Ark
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.722 / Virus Database: 478 - Release Date: 7/21/2004
Nov 14 '05 #4

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

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: Caleb Hattingh | last post by:
Hi Here is a script I want to be able to write (explanation appears after): *** start of script *** import MyCustomMacroLib # This does the magic I would like help for. # This is not...
3
by: John | last post by:
Please, consider this macro: #define mymacro(arg1, arg2) arg1 and arg2 Then it is used: mymacro(boys, girls) How is its expansion?
10
by: Karim Thapa | last post by:
Why following macro does not work? #define removebrace(x) x void Foo(int a, int b, char *txt, int d, int e); main() {
5
by: Patrick Kowalzick | last post by:
Hi all, Is this valid? I do not find the case in the standard: void foo( int ) {} #define FOO foo int main() {
2
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
5
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...
6
by: Bing | last post by:
Hi folks, Is there a way to define a macro that may contain #include directive in its body. If I just put the "#include", it gives error C2162: "expected macro formal parameter" since here I am...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.