C Macro questions

Discussion in 'C Programming' started by gouqizi.lvcha@gmail.com, Jan 16, 2008.

  1. Guest

    Hi Friends,

    I saw a usage of macro like
    #define B3 "\xA\xB\xC"

    I don't understand why B3[0] is digital 10, can ayone point what the
    logic behind this usage.

    Rick
     
    , Jan 16, 2008
    #1
    1. Advertising

  2. user923005 Guest

    On Jan 15, 8:02 pm, ""
    <> wrote:
    > Hi Friends,
    >
    > I saw a usage of macro like
    > #define B3 "\xA\xB\xC"
    >
    > I don't understand why B3[0] is digital 10, can ayone point what the
    > logic behind this usage.


    From the current C standard:
    6.4.4.4 Character constants
    Syntax
    1 character-constant:
    ' c-char-sequence '
    L' c-char-sequence '
    c-char-sequence:
    c-char
    c-char-sequence c-char
    c-char:
    any member of the source character set except
    the single-quote ', backslash \, or new-line character
    escape-sequence
    escape-sequence:
    simple-escape-sequence
    octal-escape-sequence
    hexadecimal-escape-sequence
    universal-character-name
    simple-escape-sequence: one of
    \' \" \? \\
    \a \b \f \n \r \t \v
    octal-escape-sequence:
    \ octal-digit
    \ octal-digit octal-digit
    \ octal-digit octal-digit octal-digit
    hexadecimal-escape-sequence:
    \x hexadecimal-digit
    hexadecimal-escape-sequence hexadecimal-digit
     
    user923005, Jan 16, 2008
    #2
    1. Advertising

  3. CBFalconer Guest

    "" wrote:
    >
    > I saw a usage of macro like
    > #define B3 "\xA\xB\xC"
    >
    > I don't understand why B3[0] is digital 10, can ayone point what
    > the logic behind this usage.


    It is probably bad code. However, consider what the value of hex A
    becomes.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 16, 2008
    #3
  4. CBFalconer Guest

    user923005 wrote:
    > <> wrote:
    >>
    >> I saw a usage of macro like
    >> #define B3 "\xA\xB\xC"
    >>
    >> I don't understand why B3[0] is digital 10, can ayone point what
    >> the logic behind this usage.

    >
    > From the current C standard:
    > 6.4.4.4 Character constants
    > Syntax
    > 1 character-constant:
    > ' c-char-sequence '
    > L' c-char-sequence '
    > c-char-sequence:
    > c-char
    > c-char-sequence c-char
    > c-char:
    > any member of the source character set except
    > the single-quote ', backslash \, or new-line character
    > escape-sequence
    > escape-sequence:
    > simple-escape-sequence
    > octal-escape-sequence
    > hexadecimal-escape-sequence
    > universal-character-name
    > simple-escape-sequence: one of
    > \' \" \? \\
    > \a \b \f \n \r \t \v
    > octal-escape-sequence:
    > \ octal-digit
    > \ octal-digit octal-digit
    > \ octal-digit octal-digit octal-digit
    > hexadecimal-escape-sequence:
    > \x hexadecimal-digit
    > hexadecimal-escape-sequence hexadecimal-digit


    Do you really expect someone who asked this question to understand
    this answer?

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 16, 2008
    #4
  5. Guest

    On Jan 15, 8:28 pm, user923005 <> wrote:
    > On Jan 15, 8:02 pm, ""
    >
    > <> wrote:
    > > Hi Friends,

    >
    > > I saw a usage of macro like
    > > #define B3 "\xA\xB\xC"

    >
    > > I don't understand why B3[0] is digital 10, can ayone point what the
    > > logic behind this usage.

    >
    > From the current C standard:
    > 6.4.4.4 Character constants
    > Syntax
    > 1 character-constant:
    >    ' c-char-sequence '
    >    L' c-char-sequence '
    > c-char-sequence:
    >    c-char
    >    c-char-sequence c-char
    > c-char:
    >    any member of the source character set except
    >       the single-quote ', backslash \, or new-line character
    >    escape-sequence
    > escape-sequence:
    >    simple-escape-sequence
    >    octal-escape-sequence
    >    hexadecimal-escape-sequence
    >    universal-character-name
    > simple-escape-sequence: one of
    >    \' \" \? \\
    >    \a \b \f \n \r \t \v
    > octal-escape-sequence:
    >    \ octal-digit
    >    \ octal-digit octal-digit
    >    \ octal-digit octal-digit octal-digit
    > hexadecimal-escape-sequence:
    >    \x hexadecimal-digit
    >    hexadecimal-escape-sequence hexadecimal-digit


    Thanks! I nearly forgot this usage, especially when it appears inside
    the Macro.
     
    , Jan 16, 2008
    #5
  6. Billy Bong Guest

    On Tue, 15 Jan 2008 20:28:11 -0800, user923005 wrote:

    > On Jan 15, 8:02 pm, "" <>
    > wrote:
    >> Hi Friends,
    >>
    >> I saw a usage of macro like
    >> #define B3 "\xA\xB\xC"
    >>
    >> I don't understand why B3[0] is digital 10, can ayone point what the
    >> logic behind this usage.

    >
    > From the current C standard:
    > 6.4.4.4 Character constants
    > Syntax
    > 1 character-constant:
    > ' c-char-sequence '
    > L' c-char-sequence '
    > c-char-sequence:
    > c-char
    > c-char-sequence c-char
    > c-char:
    > any member of the source character set except
    > the single-quote ', backslash \, or new-line character
    > escape-sequence
    > escape-sequence:
    > simple-escape-sequence
    > octal-escape-sequence
    > hexadecimal-escape-sequence
    > universal-character-name
    > simple-escape-sequence: one of
    > \' \" \? \\
    > \a \b \f \n \r \t \v
    > octal-escape-sequence:
    > \ octal-digit
    > \ octal-digit octal-digit
    > \ octal-digit octal-digit octal-digit
    > hexadecimal-escape-sequence:
    > \x hexadecimal-digit
    > hexadecimal-escape-sequence hexadecimal-digit


    That explains the macro, but it does not explain "the logic behind this
    usage", as the OP put it.

    In other words, the same madman who wrote that macro may well do
    something like this:

    void main(void)
    {
    }

    An explanation for "the logic behind this usage" is NOT that it is
    undefined behavior (which is true, i.e., not 0) , but rather that the
    person who wrote this is a madman (i.e., his decision to use such code
    defies logic, and he has not, or is not willing to, learn from his
    mistakes).

    Recently, I've become real good at firing--oops, I mean laying off--
    madmen.

    --
    Billy
     
    Billy Bong, Jan 16, 2008
    #6
  7. Jack Klein Guest

    On Wed, 16 Jan 2008 06:22:57 GMT, Billy Bong <>
    wrote in comp.lang.c:

    > On Tue, 15 Jan 2008 20:28:11 -0800, user923005 wrote:
    >
    > > On Jan 15, 8:02 pm, "" <>
    > > wrote:
    > >> Hi Friends,
    > >>
    > >> I saw a usage of macro like
    > >> #define B3 "\xA\xB\xC"
    > >>
    > >> I don't understand why B3[0] is digital 10, can ayone point what the
    > >> logic behind this usage.


    [snip]

    > That explains the macro, but it does not explain "the logic behind this
    > usage", as the OP put it.
    >
    > In other words, the same madman who wrote that macro may well do


    Do you think you impress by using terms like "madman"? Especially by
    using them in describing a macro that might have a perfectly valid
    use, perhaps not immediately apparent when seen out of context?

    > something like this:
    >
    > void main(void)
    > {
    > }
    >
    > An explanation for "the logic behind this usage" is NOT that it is
    > undefined behavior (which is true, i.e., not 0) , but rather that the
    > person who wrote this is a madman (i.e., his decision to use such code
    > defies logic, and he has not, or is not willing to, learn from his
    > mistakes).


    There are so many things wrong with your statements that I hardly know
    where to start.

    What possible mental quirk causes you to make the leap between the
    definition of a perfectly valid macro and the use of an incorrect
    return type for main?

    What possible reasoning do you have for proclaiming the author of the
    macro a "madman", that his code "defies logic", and is a "mistake" he
    has not learned from?

    What is there about the macro definition that makes it a mistake?

    > Recently, I've become real good at firing--oops, I mean laying off--
    > madmen.


    I've written similar macros many times for many reasons, not the least
    of which is in defining terminal control strings for an ANSI terminal:

    #define ANSI_START "\x1b["

    #define ANSI_CLEAR ANSI_START "2J"

    You have no idea whatsoever why the original programmer wrote the
    macro, or even how close to the real macro the OP is asking about to
    what he posted, since he said "something like this".

    You have no idea at all if the programmer was defining byte sequences
    required by some hardware device or some communications protocol.

    And, since no code "something like" the code that actually used the
    macro was presented, you have no idea at all if the use of the macro
    clarified the code and made it much more readable, or not.

    Gratuitous insults without justification are merely liable to get you
    ignored at best, plonked at worst.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 16, 2008
    #7
  8. James Kuyper Guest

    Billy Bong wrote:
    > On Tue, 15 Jan 2008 20:28:11 -0800, user923005 wrote:
    >
    >> On Jan 15, 8:02 pm, "" <>
    >> wrote:
    >>> Hi Friends,
    >>>
    >>> I saw a usage of macro like
    >>> #define B3 "\xA\xB\xC"
    >>>
    >>> I don't understand why B3[0] is digital 10, can ayone point what the
    >>> logic behind this usage.

    ....
    > An explanation for "the logic behind this usage" is NOT that it is
    > undefined behavior (which is true, i.e., not 0) , but rather that the
    > person who wrote this is a madman (i.e., his decision to use such code
    > defies logic, and he has not, or is not willing to, learn from his
    > mistakes).


    Could you explain about the undefined behavior? As far as I can see,
    it's perfectly well defined. I could be missing something.

    On what grounds do you label the author a madman? In addition to no UB,
    I also see nothing even faintly objectionable about this code. I've
    written code using every feature of C shown here; if I'm a madman, I'd
    like to know why.
     
    James Kuyper, Jan 16, 2008
    #8
  9. CBFalconer <> writes:
    > user923005 wrote:
    >> <> wrote:
    >>>
    >>> I saw a usage of macro like
    >>> #define B3 "\xA\xB\xC"
    >>>
    >>> I don't understand why B3[0] is digital 10, can ayone point what
    >>> the logic behind this usage.

    >>
    >> From the current C standard:
    >> 6.4.4.4 Character constants
    >> Syntax
    >> 1 character-constant:
    >> ' c-char-sequence '
    >> L' c-char-sequence '

    [22 lines deleted]
    >
    > Do you really expect someone who asked this question to understand
    > this answer?


    Well, I wasn't sure the OP would understand the answer, but apparently
    he did (see his followup).

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 16, 2008
    #9
  10. CBFalconer Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    >> user923005 wrote:
    >>> <> wrote:
    >>>>
    >>>> I saw a usage of macro like
    >>>> #define B3 "\xA\xB\xC"
    >>>>
    >>>> I don't understand why B3[0] is digital 10, can ayone point
    >>>> what the logic behind this usage.
    >>>
    >>> From the current C standard:
    >>> 6.4.4.4 Character constants
    >>> Syntax
    >>> 1 character-constant:
    >>> ' c-char-sequence '
    >>> L' c-char-sequence '

    > [22 lines deleted]
    >>
    >> Do you really expect someone who asked this question to
    >> understand this answer?

    >
    > Well, I wasn't sure the OP would understand the answer, but
    > apparently he did (see his followup).


    Evidently. See R. Heathfields favorite sig line.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 16, 2008
    #10
  11. Billy Bong wrote:
    > On Tue, 15 Jan 2008 20:28:11 -0800, user923005 wrote:
    >
    >> On Jan 15, 8:02 pm, "" <>
    >> wrote:
    >>> Hi Friends,
    >>>
    >>> I saw a usage of macro like
    >>> #define B3 "\xA\xB\xC"
    >>>
    >>> I don't understand why B3[0] is digital 10, can ayone point what the
    >>> logic behind this usage.

    >> From the current C standard:
    >> 6.4.4.4 Character constants
    >> Syntax
    >> 1 character-constant:
    >> ' c-char-sequence '
    >> L' c-char-sequence '
    >> c-char-sequence:
    >> c-char
    >> c-char-sequence c-char
    >> c-char:
    >> any member of the source character set except
    >> the single-quote ', backslash \, or new-line character
    >> escape-sequence
    >> escape-sequence:
    >> simple-escape-sequence
    >> octal-escape-sequence
    >> hexadecimal-escape-sequence
    >> universal-character-name
    >> simple-escape-sequence: one of
    >> \' \" \? \\
    >> \a \b \f \n \r \t \v
    >> octal-escape-sequence:
    >> \ octal-digit
    >> \ octal-digit octal-digit
    >> \ octal-digit octal-digit octal-digit
    >> hexadecimal-escape-sequence:
    >> \x hexadecimal-digit
    >> hexadecimal-escape-sequence hexadecimal-digit

    >
    > That explains the macro, but it does not explain "the logic behind this
    > usage", as the OP put it.
    >
    > In other words, the same madman who wrote that macro may well do
    > something like this:
    >
    > void main(void)
    > {
    > }


    Why do you think he was mad? Why do you think he may well write invalid
    code?

    > An explanation for "the logic behind this usage" is NOT that it is
    > undefined behavior (which is true, i.e., not 0)


    What is undefined about it? Looks fine to me.

    > , but rather that the
    > person who wrote this is a madman (i.e., his decision to use such code
    > defies logic, and he has not, or is not willing to, learn from his
    > mistakes).


    What's illogical about it, and what mistakes has he not learned from?

    > Recently, I've become real good at firing--oops, I mean laying off--
    > madmen.


    I doubt it; you appear to have a very strange idea of "madmen".
     
    J. J. Farrell, Jan 18, 2008
    #11
    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. Dead RAM
    Replies:
    20
    Views:
    1,118
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    582
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    504
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    477
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    462
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page