473,404 Members | 2,170 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,404 software developers and data experts.

Macro substitution: curly braces behaviour?

TJ
I'd like to know how the preprocessor treats curly braces, as compared
to how it treats parentheses.

What happens if I pass this:

{ a_function( arg1, arg2, arg3 ) }

as an argument to a function macro? Does it get passed in whole, or
split into smaller parts like so:

"{ a_function( arg1"
"arg2"
"arg3 ) }"

I tried it with gcc 4.0 and it seems that the curly braces group the
stream of tokens into one argument, just like parentheses do. However
I'd like to know if this is proper C89 behaviour, or just GCC.

Thanks.

TJ

Feb 13 '07 #1
5 8888
TJ wrote:
I'd like to know how the preprocessor treats curly braces, as compared
to how it treats parentheses.
It doesn't. It just copies them without any regard to their position,
and does not interpret them in any way.
What happens if I pass this:

{ a_function( arg1, arg2, arg3 ) }

as an argument to a function macro? Does it get passed in whole, or
split into smaller parts like so:
It is a single argument, because the commas are parenthesised.

Feb 13 '07 #2
Harald van D?k wrote:
>{ a_function( arg1, arg2, arg3 ) }

as an argument to a function macro? Does it get passed in whole, or
split into smaller parts like so:

It is a single argument, because the commas are parenthesised.
When are they not parenthesized within the context of a function or
function-like macro call?

Anyways, I don't see his behavior:

$ cat cpp.c
#define func(a, b, c) some_func(a, b, c);

{ func(1, 2, 3) }
func({ other_func(1, 2, 3) })

$ cpp -ansi cpp.c
# 1 "cpp.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "cpp.c"
{ some_func(1, 2, 3); }
cpp.c:4:29: error: macro "func" requires 3 arguments, but only 1 given
func
Feb 13 '07 #3
Christopher Layne wrote:
Harald van D?k wrote:
{ a_function( arg1, arg2, arg3 ) }

as an argument to a function macro? Does it get passed in whole, or
split into smaller parts like so:
It is a single argument, because the commas are parenthesised.

When are they not parenthesized within the context of a function or
function-like macro call?
macro(a, b)

The first argument is a. The second argument is b. For both arguments,
there are no
parentheses.

macro({ a_function( arg1, arg2, arg3 ) })

The first and only argument is { a_function( arg1, arg2, arg3 ) }. The
parentheses and nested commas are part of the argument.
Anyways, I don't see his behavior:

$ cat cpp.c
#define func(a, b, c) some_func(a, b, c);

{ func(1, 2, 3) }
func({ other_func(1, 2, 3) })

$ cpp -ansi cpp.c
# 1 "cpp.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "cpp.c"
{ some_func(1, 2, 3); }
cpp.c:4:29: error: macro "func" requires 3 arguments, but only 1 given
func
That's the behaviour the OP claimed to see.

Feb 13 '07 #4
Harald van D?k wrote:
>When are they not parenthesized within the context of a function or
function-like macro call?

macro(a, b)

The first argument is a. The second argument is b. For both arguments,
there are no
parentheses.

macro({ a_function( arg1, arg2, arg3 ) })

The first and only argument is { a_function( arg1, arg2, arg3 ) }. The
parentheses and nested commas are part of the argument.
Right. I think we're on the same page in the first place. :) I thought you
were saying that it was a single argument since the commas were parenthesized
in general.
Feb 13 '07 #5
TJ
<Harald van Dijk>
What happens if I pass this:
{ a_function( arg1, arg2, arg3 ) }
as an argument to a function macro? Does it get passed in whole, or
split into smaller parts like so:

It is a single argument, because the commas are parenthesised.
Ah ok, bad example.
... It just copies them without any regard to their position,
and does not interpret them in any way.
Yes that explains it. I also tested with a better example and it ended
up just like you said. Thanks.

TJ

Feb 14 '07 #6

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

Similar topics

21
by: deko | last post by:
Do I need to use curly brackets in PHP if .. else statements? other constructs? Does it matter? What are Best Practices? Why? thanks in advance... This seems to work WITHOUT curly brackets:...
6
by: STF | last post by:
While reading the C++ tutorial in this page: http://www.cplusplus.com/doc/tutorial/tut2-2.html I'm astonished to learn that we could omit curly brackets in function declaration for single...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
7
by: Karim Thapa | last post by:
I defined a MACRO 'removebrace' as following #define removebrace(x) x main() { ... MyFunc(1, 2, removebrace("hello", 5, 6));
17
by: Russell Shaw | last post by:
Hi, How do i make an if/then/else macro act as a function so that the whole thing looks like the return value? I tried this lame attempt for starters: #define A_FROM_B(b) \ ( \ if(b < 10)...
5
by: krbyxtrm | last post by:
Hi is there a way to 'manage' function execution using macros? #define MY_CALL_MACRO(MacroName) { g_MacroStack.push_back(MacroName); <some code here...>} such that when i use the...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
3
by: Grande News | last post by:
Hi, I've got some PHP code that looks like this $cura= ord($data{$pnum*2}); $curb = ord($data{$pnum*2+1}); Notice the curly braces -- does using them make any difference, or will square...
2
by: mauricioarango | last post by:
Hi everybody, I'm a PHP beginner, so please forgive me for this question. I am working on code someone else wrote a long time ago. The code was written without curly braces in many places, which...
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
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...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.