473,666 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

varible declaration in macro

Hi.If I define a macro which contains a variable declaration,the n
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 6640
di**********@ya hoo.com said:
Hi.If I define a macro which contains a variable declaration,the n
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(arithmetic type, 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...@ya hoo.com wrote:
Hi.If I define a macro which contains a variable declaration,the n
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
2183
by: Bob Kensworth | last post by:
Hello. What do these two statements mean? 1) #define stuff(x) (x >> 3) 2)
12
1821
by: bingfeng | last post by:
Hi, everyone, Is the following code <CODE> void foo(int n) { int a; ..... } </CODE>
6
30554
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
1139
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
1978
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 gnulib */ #define va_end(AP) Even if we dont consider what va_end do,but doesnt it cause some name confliction as a result of its two appearances?
11
10983
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 other header files. How can I resovle this error? The error (GCC 3.3.2 on Solaris 9): /usr/include/unistd.h:278: error: declaration of C function `int
13
2196
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 for help! #include <stdio.h> #include <stdlib.h> #define __buff(s, c) s_c
3
1425
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? Regards, Sachin
3
4413
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 EXPAND(ARR(DECL)) where #define DECL _decl
0
8445
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
8871
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...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
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
5664
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
4198
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...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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
2
1776
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.