using macros ...

Discussion in 'C Programming' started by hasadh, Oct 13, 2003.

  1. hasadh

    hasadh Guest

    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.

    regards,
    prakas
     
    hasadh, Oct 13, 2003
    #1
    1. Advertising

  2. hasadh <> scribbled the following:
    > 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.


    If this is some kind of debate, I'm willing to take your side on this.
    Your friend is correct that he can't use OK, TRUE, FALSE or FAILURE as
    variable or function names, but why should he? It's a common spelling
    convention that variable and function names are in lower case and macro
    names are in upper case.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "When a man talks dirty to a woman, that's sexual harassment. When a woman talks
    dirty to a man, that's 14.99 per minute + local telephone charges!"
    - Ruben Stiller
     
    Joona I Palaste, Oct 13, 2003
    #2
    1. Advertising

  3. hasadh

    Ravi Guest

    On 13-Oct-2003, (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?

    --
    main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ravi, Oct 13, 2003
    #3
  4. hasadh

    Guest

    Hello Joona I Palaste,

    On 13-Oct-2003, Joona I Palaste <> wrote:

    > It's a common


    >spelling

    Oh! really?

    > convention that variable and function names are in lower case and macro
    > names are in upper case.
     
    , Oct 13, 2003
    #4
  5. hasadh

    Ravi Guest

    On Mon, 13 Oct 2003 20:18:35 +0530, Ravi
    <> wrote:

    >On 13-Oct-2003, (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


    #define PRN printf

    sorry, happens!



    >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?


    --
    main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ravi, Oct 13, 2003
    #5
  6. lid <> scribbled the following:
    > Hello Joona I Palaste,


    > On 13-Oct-2003, Joona I Palaste <> wrote:


    >> It's a common


    >>spelling

    > Oh! really?


    >> convention that variable and function names are in lower case and macro
    >> names are in upper case.


    I have no clue as to what you are meaning with your question.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You could take his life and..."
    - Mirja Tolsa
     
    Joona I Palaste, Oct 13, 2003
    #6
  7. Name Space Pollution: using macros ...

    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.
     
    E. Robert Tisdale, Oct 13, 2003
    #7
  8. 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.
     
    E. Robert Tisdale, Oct 13, 2003
    #8
  9. hasadh

    Guest

    Hello Joona I Palaste,

    On 13-Oct-2003, Joona I Palaste <> wrote:

    > lid <> scribbled the following:
    > > Hello Joona I Palaste,

    >
    > > On 13-Oct-2003, Joona I Palaste <> wrote:

    >
    > >> It's a common

    >
    > >>spelling

    > > Oh! really?

    >
    > >> convention that variable and function names are in lower case and macro
    > >> names are in upper case.

    >
    > I have no clue as to what you are meaning with your question.


    I was just ranting. You don't call that a 'spelling' convention I guess :)
    If I'm wrong please correct me.
     
    , Oct 13, 2003
    #9
  10. hasadh

    Eric Sosman Guest

    Re: Name Space Pollution: using macros ...

    "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.


    Why in the world should the programmer's use of the
    identifiers reserved for the programmer be called "name
    space pollution?" Or to put it another way: What is
    there about the identifier `moduleName_OK' that makes it
    one whit less "polluting" than `OK'?

    C preprocessor macros are dangerous -- in the wrong
    hands, as are all C constructs. The error in the example
    above suggests that ERT's hands may not be the right ones,
    and may go some ways toward explaining why he finds macros
    dangerous. Programmers who deploy a little more skill and
    a little more care will find macros both safe and helpful.

    --
     
    Eric Sosman, Oct 13, 2003
    #10
  11. "Ravi" <> wrote in message
    news:...
    > On 13-Oct-2003, (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).
     
    Jeffrey D. Smith, Oct 13, 2003
    #11
  12. hasadh

    CBFalconer Guest

    "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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 13, 2003
    #12
  13. hasadh

    CBFalconer Guest

    Re: Name Space Pollution: using macros ...

    "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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 13, 2003
    #13
  14. 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=
    http://groups.google.com/groups?selm=

    Jeremy.
     
    Jeremy Yallop, Oct 13, 2003
    #14
  15. 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=
    http://groups.google.com/groups?selm=

    Jeremy.
     
    Jeremy Yallop, Oct 13, 2003
    #15
  16. hasadh

    Richard Bos Guest

    Re: Name Space Pollution: using macros ...

    "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.


    > 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
     
    Richard Bos, Oct 14, 2003
    #16
  17. hasadh

    Alex Guest

    "Jeffrey D. Smith" <> wrote in message
    news:LUCib.9243$...
    > 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
     
    Alex, Oct 14, 2003
    #17
  18. hasadh

    pete Guest

    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
     
    pete, Oct 14, 2003
    #18
  19. hasadh

    hasadh Guest

    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" <> wrote in message news:<LUCib.9243$>...
    > "Ravi" <> wrote in message
    > news:...
    > > On 13-Oct-2003, (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).
     
    hasadh, Oct 14, 2003
    #19
  20. hasadh <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
    - Dr. Michael Hfuhruhurr
     
    Joona I Palaste, Oct 14, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    80
    Views:
    2,435
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    451
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    498
  4. Michael T. Babcock

    Re: Explanation of macros; Haskell macros

    Michael T. Babcock, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    520
    Michael T. Babcock
    Nov 3, 2003
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    497
    S.Tobias
    Jul 24, 2004
Loading...

Share This Page