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

Home Posts Topics Members FAQ

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 2543
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.re mcomp.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
3260
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 clarification on what 'macro' means. A Lisp macro is a way of modifying code when that code is first defined. It can rearrange the structure of the code, and add and remove parts of it. Unlike C's #define macro language, Lisp macros understand the...
699
34227
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
3
2077
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 python, but the module imported above # will use this block internally and prevent it
3
2633
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
2435
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
2005
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
2211
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
6
2312
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 the following small example ? #include <stdio.h>
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
6
6975
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 not using # to concatenate strings. If I use "\# include", then I receive the following two errors: error C2017: illegal escape sequence error C2121: '#' : invalid character : possibly the result of a macro
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
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.