473,287 Members | 3,286 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,287 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 2516
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.