473,795 Members | 2,980 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 #1
16 2775
mdh said:

<snip>
#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 )".
Then you'll want #define get_char getc(stdin)

Please bear in mind, though, that this is a poor use of a macro.

The above program would be better written as follows:

#include <stdio.h>

int main(void)
{
int c;
while((c = getchar()) != EOF)
putchar(c);
return 0;
}

Avoid macros except on occasions where their advantages outweigh their
disadvantages, which is pretty rare.

--
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
Sep 30 '08 #2
mdh
On Sep 30, 7:21*am, Richard Heathfield <r...@see.sig.i nvalidwrote:
mdh said:

<snip>
#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 )".

Then you'll want #define get_char getc(stdin)

Richard...you lost me. I thought that is what the define statement
did?

>
Please bear in mind, though, that this is a poor use of a macro.

Richard...I have no doubt that this is true. The only reason I did
this was to see **how** they work. Perhaps you could explain why it is
a poor use and when you tend to use it ( rarely...as noted below!) ,
as a good example.

>
The above program would be better written as follows:

#include <stdio.h>

int main(void)
{
* int c;
* while((c = getchar()) != EOF)
* * putchar(c);
* return 0;

}


>
Avoid macros except on occasions where their advantages outweigh their
disadvantages, which is pretty rare.
Sep 30 '08 #3
mdh wrote:
On Sep 30, 7:21 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>mdh said:

<snip>
>>>
>>#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 )".

Then you'll want #define get_char getc(stdin)


Richard...you lost me. I thought that is what the define statement
did?
with RH's
#define get_char getc(stdin)

you define a plain macro vs. your
#define get_char() getc(stdin)

which defines a function like macro.
But in your code you don't use it as a function like macro:
(c = get_char)

So either use
#define get_char getc(stdin)

or
(c = get_char())

Bye, Jojo
Sep 30 '08 #4
mdh
On Sep 30, 7:35*am, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
wrote:
>
So either use
#define get_char getc(stdin)

or
(c = get_char())

Bye, Jojo
oops...thanks.
Sep 30 '08 #5
On 30 Sep, 15:26, mdh <m...@comcast.n etwrote:
On Sep 30, 7:21*am, Richard Heathfield <r...@see.sig.i nvalidwrote:
mdh said:
#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 )".
Then you'll want #define get_char getc(stdin)

Richard...you lost me. I thought that is what the define statement
did?
Please bear in mind, though, that this is a poor use of a macro.

Richard...I have no doubt that this is true. The only reason I did
this was to see **how** they work. Perhaps you could explain why it is
a poor use and when you tend to use it ( rarely...as noted below!) ,
as a good example.
<snip>

macro substitution is carroed out by a macro processor.
The so-called "pre-processor"[1]. The pp is pretty ignorant
of C. It simply substitutes text. This means you have missed
important argument checking that the compiler proper can carry out.
You also get that multiple argument evaluation stuff.

#define SQUARE(A) A * A

looks ok? What does these do?

SQUARE(a++);
SQUARE(a + b);

Another nasty practice is people who decide C syntax can
be improved with a few macros

#define procedure void
#define integer int
#define begin {
#define end }
#define writeln(n) printf ("%d\n", n)
procedure p (integer i)
begin
writeln (i);
end
trust me, no one will thank you for this
[1] at least conceptually. A clever compiler writer may avoid having
a
separate pp stage provided he gets the same answer "as-if" he'd used
a pp. The so-called "as-if" rule.
--
Nick Keighley

"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup
Sep 30 '08 #6
mdh
On Sep 30, 8:11*am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
On 30 Sep, 15:26, mdh <m...@comcast.n etwrote:
>
<snip>

macro substitution is carroed out by a macro processor.
The so-called "pre-processor"[1]. The pp is pretty ignorant
of C. It simply substitutes text. This means you have missed
important argument checking that the compiler proper can carry out.
You also get that multiple argument evaluation stuff.

thanks Nick. I find it instructive to understand why something is poor
as well as good practice. One of the **disadvantages ** ( if there are
any) of K&R is that they do not teach you **bad** C, so this is left
to you ( as in me) to discover. Fortunately, the clc soon disavows one
of one's bad habits!!! :-)

Sep 30 '08 #7
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. 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.

--
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 #8
mdh
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? 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.
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.

Sep 30 '08 #9
mdh <md**@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"
Sep 30 '08 #10

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
2457
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
7095
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
23557
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
6975
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
9673
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
9522
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
10217
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
10167
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,...
0
10003
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...
1
7544
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
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
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
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.