473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

empty macro that won't give compiler errors?

Need to define a macro as, say,

#ifdef NEED_FUNCTION

foo(x)

#else

#endif

Say that macro is called FOO(x) and is used by writing

FOO(x);

with a semicolon after it so it looks like any other statement.

When NEED_FUNCTION is defined, then compiler sees

foo(x);

When NEED_FUNCTION is not defined, compiler sees

;

If I want to place

FOO(x);

in a larger function _before_ the local variable declarations, will I get
a compiler error (for doing something I shouldn't before declarations)?

If so, why?

If not, why not?

If I will get a compiler error, what can I define that macro as in the
else section so that that text, followed by the semicolon, will be an
allowed statement even before local variable declarations?

Thanks!

Tom Harris
t.******@ieee.o rg
http://talkaboutquality.wordpress.com

Oct 5 '07 #1
2 2196
On Fri, 05 Oct 2007 02:27:41 +0200, talkaboutqualit y
<t.******@ieee. orgwrote in comp.lang.c:
Need to define a macro as, say,
Why?
#ifdef NEED_FUNCTION

foo(x)
This is not a macro. Do you mean:

#define FOO(x) foo(x)

....???
>
#else
No need for an empty #else, eliminate it.
#endif

Say that macro is called FOO(x) and is used by writing

FOO(x);

with a semicolon after it so it looks like any other statement.

When NEED_FUNCTION is defined, then compiler sees

foo(x);

When NEED_FUNCTION is not defined, compiler sees

;

If I want to place

FOO(x);

in a larger function _before_ the local variable declarations, will I get
a compiler error (for doing something I shouldn't before declarations)?
Depends on the compiler and version of the C standard it conforms to.
What happened when you tried it?
If so, why?
If you do, it is because doing so is a constraint violation according
to the version of the C standard your compiler is conforming to.
If not, why not?
If you don't, it is because doing so is a legal practice according to
the version of the C standard your compiler is conforming to.
If I will get a compiler error, what can I define that macro as in the
else section so that that text, followed by the semicolon, will be an
allowed statement even before local variable declarations?
Why do you want to put a macro that expands to a function call prior
to declarations in a block? Both the function call expression and the
empty statement are executable statements. If your implementation
objects to one, it will object to the other. Both are "allowed" under
all versions of the C standard, the issue is whether declarations, of
any sort, are allowed after any executable statements in a block.

For versions of the C standard prior to 1999, they are not. For
versions of the C standard as of 1999 and later, they are.
Thanks!
Please set your newsreader to post a proper signature delimiter, which
consists of "-- " followed by a newline.

I'm not sure I understand what you are trying to do, and why. One
thing you could do is define your macro expansion to include the
terminating semicolon. If you accidentally forget and type a
semicolon after the macro when you use it, it will merely be an empty
statement in the body of the function.

That will cause no harm at all, unless you write code like:

if (some_condition )
FOO(x);
else
/* something */

In that case, the expansion of FOO(x) results in:

if (some_condition )
foo(x);;
else

....but I would not expect a professional engineer with an ieee.org
email address to write such unprofessional code that he would not
fully brace enclose all code controlled by conditional statements.

But again, if the conditional macro is not defined and your compiler
objects to the empty statement, it would object to the function call
expansion of the macro as well, when the conditional is defined, so
what are you hoping to gain?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Oct 5 '07 #2
talkaboutqualit y said:

<snip>
>
When NEED_FUNCTION is defined, then compiler sees

foo(x);

When NEED_FUNCTION is not defined, compiler sees

;
#ifdef NEED_FUNCTION
#define FOO(x) foo(x)
#else
#define FOO(x)
#endif

If I want to place

FOO(x);

in a larger function _before_ the local variable declarations, will I get
a compiler error (for doing something I shouldn't before declarations)?
Yes in C90 (which is what you're actually using).
If so, why?
Because an empty statement is still a statement, and you can't mix
statements with declarations.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 5 '07 #3

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

Similar topics

1
5020
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I double-checked the path to my error log. It is in /var/www/logs/php_error_log Thanks. :) -Wayne Stevenson
25
3220
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...
699
33517
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...
10
3271
by: Agent Mulder | last post by:
Hi group, Almost 3 weeks ago I posted a short question here and in comp.std.c++ and I got exactly 1 respons, from Kevin Goodsell. He said he didn't want it. I post it here again, this time in a thread named 'Empty arguments', because that is what I got from you. Can you spare a minute and try to see my point? struct Room { Room(bool...
7
23532
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. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
3
3135
by: Charlie Zender | last post by:
Hi, I want to have a CPP macro that tests the value of a token and returns the string "No" if the token is undefined (or 0) and returns "Yes" if the token is defined (non-zero). Then I can have C code that self-diagnoses its configuration with, e.g., #define TKN2YESNO(x) ((x)==0 ? ("No"):("Yes"))
28
2621
by: richardlang | last post by:
Anyone out there ever come across a preprocessor macro that compares an argument value against the predefined __DATE__ macro in order to control conditional compilation based on date. Something along the lines of... # define DateLaterThan(x) ... that could be used for things like
7
6619
by: MtiPaulo | last post by:
I know FoxPro has excellent macro substitution but I am trying to find through MS Access. Wondering if MS Access has Macro Substitution? I will give you my basic example. In FoxPro Language Code For i = 1 to 5 strCmd = "Client" + Alltrim(Str(i)) + "=" + Alltrim(Str(i)) &strCmd && Client1 = 1 Next
9
7655
by: Two-Horned Unicorn | last post by:
The following code compiles and works as expected when compiled with gcc. /* empty-args.c */ #include <stdio.h> #define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"") #define bar(x) puts("foo: x=\"" #x "\"")
0
7618
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...
1
7679
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...
0
7983
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...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3657
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...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
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...

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.