Thread: Newbie question about the define macro

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    20

    Newbie question about the define macro

    I happened to read some code recently and I have the following doubts.

    What does this statement mean:

    #define stricmp strcasecmp


    Is this supported in c++?

    Does the statement mean " stricmp = strcasecmp ; "
    if then what is the type?

    Is there a better way other than using a macro?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Edit: nm. I was wrong.
    Last edited by King Mir; 05-18-2006 at 01:46 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#define stricmp strcasecmp
    It means you need to be prepared for nightmarish sessions of code maintenance. If the author thought this was a good idea, I can only imagine what else he did. Basically, the author realized that strcasecmp isn't a standard function and decided to change the name to something that (he thought) was more portable.

    >Is there a better way other than using a macro?
    Yes. Write an inline function that calls strcasecmp. That way your inline function has an interface that never changes and you have only one point of failure that needs to be changed when the code is ported. Alternatively, you can write your own case insensitive string comparison algorithm. Playing name games is never a good solution.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Prelude
    >#define stricmp strcasecmp
    It means you need to be prepared for nightmarish sessions of code maintenance. If the author thought this was a good idea, I can only imagine what else he did. Basically, the author realized that strcasecmp isn't a standard function and decided to change the name to something that (he thought) was more portable.

    >Is there a better way other than using a macro?
    Yes. Write an inline function that calls strcasecmp. That way your inline function has an interface that never changes and you have only one point of failure that needs to be changed when the code is ported. Alternatively, you can write your own case insensitive string comparison algorithm. Playing name games is never a good solution.
    You got that backwords. stricmp is changed to srtcasecmp, not the other way around.

    Stricmp is a standard function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by King Mir
    Stricmp is a standard function.
    Nope.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >stricmp is changed to srtcasecmp, not the other way around.
    You're thinking of typedef. For #define, the new name comes first, then the replacement text.

    >Stricmp is a standard function.
    Really. Would you care to post a quote from the standard proving this?
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Fairly standard. Actually, were can I get a copy of the real standard?

    My C library reference lists this function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    http://www.gotw.ca/gotw/029.htm

    A snippet from that site:
    Write a ci_string class which is identical to the standard 'string' class, but is case-insensitive in the same way as the (nonstandard but widely implemented) C function stricmp():

    The "how can I make a case-insensitive string?" question is so common that it probably deserves its own FAQ -- hence this issue of GotW.

    Note 1: The stricmp() case-insensitive string comparison function is not part of the C standard, but it is a common extension on many C compilers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Fairly standard.
    Fairly standard? You mean sort of standard but not completely? That's like being sort of dead, or sort of pregnant. Something is either standard or it's not. There's no inbetween here.

    >Actually, were can I get a copy of the real standard?
    You can download the PDF for $18 from www.ansi.org.

    >My C library reference lists this function.
    I don't doubt it. My compiler compiles it too, but that doesn't mean it's a standard C function.
    My best code is written with the delete key.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    edit::

    C examples on the C++ board just seems wrong. My mistake.
    Last edited by kermit; 05-18-2006 at 03:04 PM.

  11. #11
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    stricmp strcasecmp
    One neede for Unix the other needed for Windows? Eh, maybe I wrong.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Thanks everyone.
    Bottomline: Macros are bad practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  2. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 04:55 PM
  3. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM