Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > using macros ...

Reply
Thread Tools

using macros ...

 
 
Jeffrey D. Smith
Guest
Posts: n/a
 
      10-13-2003
"Ravi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 13-Oct-2003, http://www.velocityreviews.com/forums/(E-Mail Removed) (hasadh) wrote:
>
> > probably this may be a simple qn to u all but I need an answer plz.
> > In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who
> > included this code as a library into his module said that in his code
> > he couldnt use the above words as function names or variable names and

>
>
> BTW you define macros like this
> #define TRUE 1
> etc.
> and after that you would not use them as function name
>
> OTOH
> #define printf PRN
> and then you may use PRN as a function name.
>
> > got compilation errors.

>
> Which compiler? What did you do, some more details
>
>
> > He advised me to use them as enums instead of
> > macros. is he right ???

>
> Was he trying to say this would solve the problem with
> compilation?


Yes it will. Changing to enums will avoid the
preprocessor substitution. The compiler can
see the enum symbols and distinguish them from
other uses (like function calls).


 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-13-2003
"E. Robert Tisdale" wrote:
> Joona I Palaste wrote:
>
> > It's a common spelling convention that
> > variable and function names are in lower case
> > and macro names are in upper case.

>
> Which is an anachronism now that the const keyword
> and inline functions have been introduced into the language.


Don't spout nonsense and corrupt the children. Those have quite
distinct uses, and cannot substitute for macros.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-13-2003
"E. Robert Tisdale" wrote:
> hasadh wrote:
>
> > In my software, I used macros like OK, TRUE, FALSE, FAILURE.
> > A friend who included this code as a library into his module
> > said that, in his code, he couldn't use the above words
> > as function names or variable names and got compilation errors.
> > He advised me to use them as enums instead of macros.
> > Is he right?

>
> The problem is name space pollution.
> Your definitions of global names conflict with other definitions.
> This problem is easily solved by prepending a unique prefix
> to all of the global names in your module:
>
> #define moduleName_OK 1
> #define moduleName_FALSE 0
> #define moduleName_TRUE (!(moduleName_FALSE))
> #define moduleName_SUCCESS 0
> #define moduleName_FAILURE (!(SUCCESS))
>
> C preprocessor macros are dangerous.
> An enumeration or, better yet, const global objects are much safer.


This is bad advice, and indicative of failure to limit access to
the narrowest possible scope and failure to understand the
language.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Jeremy Yallop
Guest
Posts: n/a
 
      10-13-2003
E. Robert Tisdale wrote:
> Joona I Palaste wrote:
>
>> It's a common spelling convention that
>> variable and function names are in lower case
>> and macro names are in upper case.

>
> Which is an anachronism now that the const keyword
> and inline functions have been introduced into the language.


Not at all. There are lots of things that are possible to achieve
with macros, but impossible with const and inline functions. In fact,
although inline functions can occasionally be used in place of macros,
const cannot generally be used to replace #define-d symbolic
constants, since the value of a const variable cannot be used in a
constant expression.

This has been explained to you before, but you continue to spread the
same misinformation. Please stop.

http://groups.google.com/groups?selm...B3%40yahoo.com
http://groups.google.com/groups?selm...gnus%40usa.net

Jeremy.
 
Reply With Quote
 
Jeremy Yallop
Guest
Posts: n/a
 
      10-13-2003
E. Robert Tisdale wrote:
> Joona I Palaste wrote:
>
>> It's a common spelling convention that
>> variable and function names are in lower case
>> and macro names are in upper case.

>
> Which is an anachronism now that the const keyword
> and inline functions have been introduced into the language.


Not at all. There are lots of things that are possible to achieve
with macros, but impossible with const and inline functions. In fact,
although inline functions can occasionally be used in place of macros,
const cannot generally be used to replace #define-d symbolic
constants, since the value of a const variable cannot be used in a
constant expression.

This has been explained to you before, yet you continue to spread the
same misinformation. Please stop.

http://groups.google.com/groups?selm...B3%40yahoo.com
http://groups.google.com/groups?selm...gnus%40usa.net

Jeremy.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-14-2003
"E. Robert Tisdale" <(E-Mail Removed)> wrote:

> hasadh wrote:
>
> > In my software, I used macros like OK, TRUE, FALSE, FAILURE.
> > A friend who included this code as a library into his module
> > said that, in his code, he couldn't use the above words
> > as function names or variable names and got compilation errors.
> > He advised me to use them as enums instead of macros.
> > Is he right?

>
> The problem is name space pollution.


> C preprocessor macros are dangerous.
> An enumeration or, better yet, const global objects are much safer.


So, erm... please explain how, exactly,

enum predefined_values {no, yes};

is any less namespace-polluting than

#define NO 0
#define YES 1

Richard
 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      10-14-2003
"Jeffrey D. Smith" <(E-Mail Removed)> wrote in message
news:LUCib.9243$(E-Mail Removed) ink.net...
> The compiler can see the enum symbols and distinguish them from other
> uses (like function calls).


No it can't, although a name clash with an enum constant will probably give
a less confusing error message, eg:

#define foo 42
int foo(void) {
return 42;
}

test.c:2: parse error before numeric constant

enum {foo = 42};
int foo(void) {
return 42;
}

test.c:2: `foo' redeclared as different kind of symbol
test.c:1: previous declaration of `foo'

Alex


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-14-2003
hasadh wrote:
>
> Hello,
>
> probably this may be a simple qn to u all but I need an answer plz.
> In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who
> included this code as a library into his module said that in his code
> he couldnt use the above words as function names or variable names and
> got compilation errors. He advised me to use them as enums instead of
> macros. is he right ???
>
> (in my code the macros are used as return values from fns mostly)
>
> awaiting your replies.


About the only use I've ever had for enums,
is as return values from functions.
I think they're OK for that purpose.

--
pete
 
Reply With Quote
 
hasadh
Guest
Posts: n/a
 
      10-14-2003
Hi,

you say that "The compiler can see the enum symbols and distinguish them from
other uses (like function calls)." .
but i get compilation error if i use a enum symbol and a same fn name. eg
enum {TRUE 1}

and
void TRUE() - gives compilation error in vc++ .





"Jeffrey D. Smith" <(E-Mail Removed)> wrote in message news:<LUCib.9243$(E-Mail Removed) link.net>...
> "Ravi" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On 13-Oct-2003, (E-Mail Removed) (hasadh) wrote:
> >
> > > probably this may be a simple qn to u all but I need an answer plz.
> > > In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who
> > > included this code as a library into his module said that in his code
> > > he couldnt use the above words as function names or variable names and

> >
> >
> > BTW you define macros like this
> > #define TRUE 1
> > etc.
> > and after that you would not use them as function name
> >
> > OTOH
> > #define printf PRN
> > and then you may use PRN as a function name.
> >
> > > got compilation errors.

> >
> > Which compiler? What did you do, some more details
> >
> >
> > > He advised me to use them as enums instead of
> > > macros. is he right ???

> >
> > Was he trying to say this would solve the problem with
> > compilation?

>
> Yes it will. Changing to enums will avoid the
> preprocessor substitution. The compiler can
> see the enum symbols and distinguish them from
> other uses (like function calls).

 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-14-2003
hasadh <(E-Mail Removed)> scribbled the following:
> Hi,


> you say that "The compiler can see the enum symbols and distinguish them from
> other uses (like function calls)." .
> but i get compilation error if i use a enum symbol and a same fn name. eg
> enum {TRUE 1}


> and
> void TRUE() - gives compilation error in vc++ .


True, true... (pun not intended), but like I said in my previous reply,
why would anyone *want* to name their function "TRUE"?

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macros-loop? calling macros X times? Andrew Arro C Programming 2 07-24-2004 09:52 AM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM
Re: Explanation of macros; Haskell macros Michael T. Babcock Python 0 11-03-2003 01:54 PM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 5 11-01-2003 01:09 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments