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

varible declaration in macro

Hi.If I define a macro which contains a variable declaration,then
during expansion the declaration will not be placed at the beginning of
the program,but somwhere within it.How can I overcome it?

Eric

Nov 7 '06 #1
2 6626
di**********@yahoo.com said:
Hi.If I define a macro which contains a variable declaration,then
during expansion the declaration will not be placed at the beginning of
the program,but somwhere within it.How can I overcome it?
By not declaring the variable within the macro.

But if you must do so, then you can use {} to create a local scope for the
variable:

#define swap(arithmetictype, x, y) \
{ arithmetictype temp = x; x = y; y = temp; }

Usage:
int i = 42;
int j = 6;
swap(int, i, j)

The variable ceases to exist at the point that the } is reached. Although it
is not, strictly speaking, necessary, some people like to wrap a dummy
do-while around the compound statement, so that they can see a nice normal
semicolon at the end of the macro. Other people, such as myself, prefer not
to play such games with macros at all, and would rather write a function to
do whatever-it-is instead.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 7 '06 #2
dis_is_ea...@yahoo.com wrote:
Hi.If I define a macro which contains a variable declaration,then
during expansion the declaration will not be placed at the beginning of
the program,but somwhere within it.How can I overcome it?
C's macros are purely text substitution and cannot ever insert text
other than where they are used. You can either use a mode of your
compiler which accepts variable declarations in the middle of a program
(in the current version of the C standard, this is allowed), or you can
rewrite the macro (for example, if it is to be used as a statement, by
surrounding it with { and }).

Nov 7 '06 #3

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

Similar topics

17
by: Bob Kensworth | last post by:
Hello. What do these two statements mean? 1) #define stuff(x) (x >> 3) 2)
12
by: bingfeng | last post by:
Hi, everyone, Is the following code <CODE> void foo(int n) { int a; ..... } </CODE>
6
by: The8thSense | last post by:
how to declare static varible and static method inside a class ? i try "public static ABC as integer = 10" and it said my declaration is invalid Thanks
1
by: Peter | last post by:
Does anybody know how to convert a string to a varible? For example, I have defined 2 structures Structure SS dim Name as string dim ID as short end structure Structure TT
3
by: zhangyue.zl | last post by:
Before I thought C is simple and convient , but now I dont think so.There is really some ugly thing in C.Today I see some macro declaration like this: void va_end (va_list); /* Defined in...
11
by: rherring | last post by:
Hello, I have some C++ code that builds against 2 headers files that contain the same function name declaration (gethostname). The header files are not directly included. They are included via...
13
by: Carramba | last post by:
Hi! I have written some peace of code, but I wonder if it's legal for ansi-c I have no problem to compiling it, but since I'm inexperience and the output is not correct I have doubts. Thank you...
3
by: Sachin | last post by:
Hi Folks, I have one query in regarding const varible interpretaion by C/C++ compiler. How Compiler actually differentiate between normal stack varible and const varible during program execution?...
3
by: prix prad | last post by:
Hi All, I encountered the above error when I tried to expand a macro as follows: #define EXPAND(array) int M_ ## array The problem occurs when we have 'array' itself as a macro: say...
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: 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:
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
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,...
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.