473,804 Members | 4,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More Macro questions

mdh
I have asked a few questions about Macros...and think what I have been
missing ...and which all who have replied clearly understand...is that
this is a Pre-processor action.
Hopefully the above is true.
So, could someone look at this code, and help me understand why I get
this error.
#include <stdio.h>

#define get_char() getc(stdin)
#define put_char(x) putc(x, stdout)

int main (int argc, const char * argv[]) {
int c;
while ( (c = get_char) != EOF) /* error: 'get_char' undeclared
(first use in this function) */
put_char(c);
return 0;
}

I **thought** what would happen is that get_char would be replaced by
"getc(stdin )".

Thanks
Sep 30 '08
16 2777
mdh
On Sep 30, 1:06*pm, Keith Thompson <ks...@mib.orgw rote:
mdh <m...@comcast.n etwrites:

[...]
Just out of curiosity, when would you choose to use a function-like
Macro? *I think we spoke before about that being *the way that C
implements *va_list. But have not seen good examples of user-written
function-like Macros.

va_list itself isn't (necessarily) a macro at all, but va_arg,
va_copy, va_end, and va_start are all function-like macros.

Use a function-like macro when you want something that looks and
(mostly) behaves like a callable function, but when an actual function
won't do the job. *In the old days, the most common reason was
performance, but modern compilers are generally able to inline small
functions. *A function-like macro can be used when you want to pass
something other than an expression value.

The standard offsetof macro is a good example; its second argument is
a member name, something that you couldn't pass to a function.

And any standard function may additionally be implemented as a macro,
usually for performance reasons.

Here's my favorite example of a horrid mis-use of the macro facility,
a demonstration that Douglas Adams was right in The Hitchhiker's Guide
to the Galaxy:

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
* * printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
* * return 0;

}
Being one who follows good advice ( as far as I can) I take Richard
Heathfield's admonition seriously. "Avoid macros except on occasions
where their advantages outweigh their disadvantages, which is pretty
rare", but I am still curious.

Emininently reasonable on both counts.

--
Keith Thompson (The_Other_Keit h) ks...@mib.org *<http://www.ghoti.net/~kst>
Nokia
"We must do something. *This is something. *Therefore, we must do this."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"
Thank you Richard for that explanation.
Sep 30 '08 #11
mdh
On Sep 30, 1:06*pm, Keith Thompson <ks...@mib.orgw rote:
mdh <m...@comcast.n etwrites:

[...]
Just out of curiosity, when would you choose to use a function-like
Macro? *I think we spoke before about that being *the way that C
implements *va_list. But have not seen good examples of user-written
function-like Macros.

va_list itself isn't (necessarily) a macro at all, but va_arg,
va_copy, va_end, and va_start are all function-like macros.

Use a function-like macro when you want something that looks and
(mostly) behaves like a callable function, but when an actual function
won't do the job. *In the old days, the most common reason was
performance, but modern compilers are generally able to inline small
functions. *A function-like macro can be used when you want to pass
something other than an expression value.

The standard offsetof macro is a good example; its second argument is
a member name, something that you couldn't pass to a function.

And any standard function may additionally be implemented as a macro,
usually for performance reasons.

Here's my favorite example of a horrid mis-use of the macro facility,
a demonstration that Douglas Adams was right in The Hitchhiker's Guide
to the Galaxy:

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
* * printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
* * return 0;

}
Being one who follows good advice ( as far as I can) I take Richard
Heathfield's admonition seriously. "Avoid macros except on occasions
where their advantages outweigh their disadvantages, which is pretty
rare", but I am still curious.

Emininently reasonable on both counts.

--
Keith Thompson (The_Other_Keit h) ks...@mib.org *<http://www.ghoti.net/~kst>
Nokia
"We must do something. *This is something. *Therefore, we must do this."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"
Red in Face!!!

Thanks Keith!
Sep 30 '08 #12
mdh <md**@comcast.n etwrites:
On Sep 30, 1:06*pm, Keith Thompson <ks...@mib.orgw rote:
>mdh <m...@comcast.n etwrites:
[...]
Being one who follows good advice ( as far as I can) I take Richard
Heathfield's admonition seriously. "Avoid macros except on occasions
where their advantages outweigh their disadvantages, which is pretty
rare", but I am still curious.

Emininently reasonable on both counts.

--
Keith Thompson (The_Other_Keit h) ks...@mib.org *<http://www.ghoti.net/~kst>
Nokia
"We must do something. *This is something. *Therefore, we must do this."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"

Red in Face!!!

Thanks Keith!
You're welcome.

You could un-redden your face even more by not quoting the entire
article (particularly the signature) just for a "thanks". Quote just
enough so your followup makes sense on its own.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 30 '08 #13
"mdh" <md**@comcast.n etwrote in message
news:79******** *************** ***********@z66 g2000hsc.google groups.com...
On Sep 30, 9:46 am, Keith Thompson <ks...@mib.orgw rote:
mdh <m...@comcast.n etwrites:
>
int main (int argc, const char * argv[]) {
int c;
while ( (c = get_char) != EOF) /* error: 'get_char' undeclared
(first use in this function) */
put_char(c);
return 0;
}
.
A function-like macro, like your get_char and put_char, takes zero or
more arguments. In the definition, the opening '(' immediately
follows the macro name, without even whitespace between them. An
invocation looks very much like a function call. A reference to the
name of a function-like macro that's not followed by a '(' (possibly
with whitespace in between) isn't recognized. (Possibly making this
an error would have been better.)

And that's the problem in your program; you need to write "get_char() "
rather than "get_char".

An object-like macro takes no arguments; it just expands to whatever
it's defined as. EOF and NULL are examples.
Thanks Keith.
Just out of curiosity, when would you choose to use a function-like
Macro?
[...]

You can use function macro names as function pointers for other macros...
Think along lines of something like:
_______________ _______________ _______________ _______________ _____
#include <stdio.h>

#define QUOTE_IMPL(mp_t ext)#mp_text
#define QUOTE(mp_text) \
QUOTE_IMPL(mp_t ext)
#define PLACE(mp_text)m p_text
#define MY_MACRO_FUNC_1 (mp_text) \
PLACE(Hello from my macro func 1 mp_text)

#define MY_MACRO_FUNC_2 (mp_text) \
PLACE(Hello from my macro func 2 mp_text)

#define INVOKE_MACRO_FU NC(mp_fpname, mp_params) \
mp_fpname mp_params
int main(void) {
puts(QUOTE(INVO KE_MACRO_FUNC(M Y_MACRO_FUNC_1, (invoker.))));
puts(QUOTE(INVO KE_MACRO_FUNC(M Y_MACRO_FUNC_2, (invoker.))));
return 0;
}

_______________ _______________ _______________ _______________ _____


Here is real world use of macro function pointer:

http://webpages.charter.net/appcore/...-c-v0-002.html

Look at the section under `Array Init Support' and study the PLACE macros.
Then look at the `Global Lock Table' and study the way I initialize the
arrays.


Also read here:

http://groups.google.com/group/comp....674afb7c772df8

Macro function pointers using names of func-like macros as the actual
func-pointer has advantages, such as automating static initialization.

Oct 2 '08 #14
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
mdh <md**@comcast.n etwrites:
>I have asked a few questions about Macros...and think what I have been
missing ...and which all who have replied clearly understand...is that
this is a Pre-processor action.
Hopefully the above is true.
So, could someone look at this code, and help me understand why I get
this error.
#include <stdio.h>

#define get_char() getc(stdin)
#define put_char(x) putc(x, stdout)

int main (int argc, const char * argv[]) {
int c;
while ( (c = get_char) != EOF) /* error: 'get_char' undeclared
(first use in this function) */
put_char(c);
return 0;
}

I **thought** what would happen is that get_char would be replaced by
"getc(stdin) ".

There are two different kinds of macros, "function-like" and
"object-like".

A function-like macro, like your get_char and put_char, takes zero or
more arguments. In the definition, the opening '(' immediately
follows the macro name, without even whitespace between them. An
invocation looks very much like a function call.
I am a little confused about the whitespace comment... Are you saying the
following is actually invalid:
#define PLACE(token)tok en

PLACE ((1+1)==2);
If so, then I really need to correct my awnser to mdh found here:

http://groups.google.com/group/comp....08ac8f4809f335
The 'INVOKE_MACRO_F UNC' macro expansion will insert a whitespace between the
macro "function pointer" name and its argument list... Here is simple
attempt to omit whitespace:
_______________ _______________ _______________ _______________ ___
#include <stdio.h>
#include <assert.h>

#define QUOTE_IMPL(mp_t ext)#mp_text
#define QUOTE(mp_text) \
QUOTE_IMPL(mp_t ext)
#define PLACE(mp_text)m p_text
#define MY_MACRO_FUNC_1 (mp_text) \
PLACE(Hello from my macro func 1 mp_text)

#define MY_MACRO_FUNC_2 (mp_text) \
PLACE(Hello from my macro func 2 mp_text)

#define INVOKE_MACRO_FU NC(mp_fpname, mp_params) \
PLACE(mp_fpname )mp_params
int main(void) {
puts(QUOTE(INVO KE_MACRO_FUNC(M Y_MACRO_FUNC_1, (invoker.))));
puts(QUOTE(INVO KE_MACRO_FUNC(M Y_MACRO_FUNC_2, (invoker.))));
return 0;
}

_______________ _______________ _______________ _______________ ___
Now I use PLACE macro to output the token, then the function params
immediately follow it. The expansion should not insert any whitespaces in
between them; well, at least I think it should not! ;^/

Anyway, if I misunderstood you, well, then I am sorry for _wasting_ your
time!

;^(
[...]

Oct 2 '08 #15
"Chris M. Thomasson" <no@spam.invali dwrites:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
[...]
>There are two different kinds of macros, "function-like" and
"object-like".

A function-like macro, like your get_char and put_char, takes zero or
more arguments. In the definition, the opening '(' immediately
follows the macro name, without even whitespace between them. An
invocation looks very much like a function call.

I am a little confused about the whitespace comment...
Yes.
Are you saying
the following is actually invalid:
No.
#define PLACE(token)tok en

PLACE ((1+1)==2);
For a function-like macro, you can't have whitespace between the macro
name and the '(' *in the definition* (i.e., in the #define directive).
In a macro invocation, you can have as much whitespace as you like, or
none at all.

The reason (apart from "the standard says so!") is that, in a macro
definition, the lack of whitespace distinguishes between a definition
of a function-like macro and of an object-like macro that happens to
start with a '('. A macro invocation, on the other hand, looks and
works (in some cases) just like a function call, where all that
matters is the sequence of tokens. "foo(arg)", "foo( arg )", and
"foo ( arg )" are equally valid, and all mean the same thing.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 2 '08 #16

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
"Chris M. Thomasson" <no@spam.invali dwrites:
>"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******* *****@nuthaus.m ib.org...
[...]
>>There are two different kinds of macros, "function-like" and
"object-like".

A function-like macro, like your get_char and put_char, takes zero or
more arguments. In the definition, the opening '(' immediately
follows the macro name, without even whitespace between them. An
invocation looks very much like a function call.

I am a little confused about the whitespace comment...

Yes.
[...]

DOH! I am sorry for this retarded non-sense; I totally __MISSED__ the first
three words in the following sentence that you wrote:

[...]
In the definition, the opening '(' immediately follows
the macro name, without even whitespace between them.
[...]

Stupid ME!

ARGH!!!
;^(...

Oct 2 '08 #17

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

Similar topics

0
1184
by: Joel Just Joel | last post by:
Good day, dotnet.general Folks, I'm not sure if this is the correct forum for posting general Visual Studio ..NET questions, but I hope so. If not, please forgive and tell me where to post instead. Thanks! I have in mind a simple add-in or macro for Visual Studio .NET 2003 and I wanted some advice about it. What I envision is a simple tool to look for and warn of "broken event handler links", which is sometimes a problem with VS...
20
2459
by: Jim Ford | last post by:
I understand that some compilers define a symbol that can be used anywhere in the code in order to find out, at run time, the name of the file where the code corresponding to the current execution instant is implemented. This prompts two questions: 1) Is this an ANSI C feature, or just a compiler-dependent goody? 2) If it is an ANSI C feature, is there anything equivalent for function names, rather than file names?
8
1762
by: sathyashrayan | last post by:
/* ** PLURALTX.C - How to print proper plurals ** public domain - original algorithm by Bob Stout */ #include <stdio.h> #define plural_text(n) &"s"
17
7097
by: sounak | last post by:
How could we get a macro name from a macro value such that in a header file #define a 100 #define b 200 now the source file will be such that the user gives 100 then the value is outputted as a the user gives 200 then the value is outputted as b
7
23558
by: fei.liu | last post by:
#define ONCFILE_ERR1(funcname, name) \ { \ #ifdef DEBUG\ cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " << funcname << " " << name << endl; \ #endif \ } I want to have conditional macros inside a macro. When I compile this
1
1695
by: David | last post by:
I inherited an excel macro that compares two different worksheets and matches rows from worksheet 2. I created a module that would let me know if there are any duplicates in worksheet 2. The problem is when I run this macro it does not yield a result but rather shows me the formulas that comprise the macro and also states something is wrong within my formulas! Would you have any idea how to actually get my module/macro to evaluate...
5
3493
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record the cursor was on in the datasheet at the time I executed the macro. So, the questions are: 1) In the macro, how to I get my hands on the record key or record data of the record the cursor was on in the datasheet at the time I executed the...
13
6294
by: lightning | last post by:
I have a search form that returns records to another form, rather than a table. Here are my questions: 1) Is it possible to export the form results (as opposed to query results) to CSV? I've been successful at exporting a table of results before, but not a form. 2) If so, can this be done via macro? 3) If a macro is possible, can the macro further allow the user to choose location and filename when exporting? I've tried a...
6
6976
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
9710
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
9589
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
10593
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
10340
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...
1
10329
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,...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
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
5527
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.