C Macro questions

G

gouqizi.lvcha

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
 
U

user923005

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
 
C

CBFalconer

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

CBFalconer

user923005 said:
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?
 
G

gouqizi.lvcha

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

Billy Bong

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

Jack Klein

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
 
J

James Kuyper

Billy said:
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.
 
K

Keith Thompson

CBFalconer said:
user923005 said:
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).
 
C

CBFalconer

Keith said:
CBFalconer said:
user923005 said:
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.
 
J

J. J. Farrell

Billy said:
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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C macro 3
Linux: using "clone3" and "waitid" 0
reasoning for a macro 14
Lexical Analysis on C++ 1
More Macro questions 15
MACRO help 23
C function macro question 5
macro to access structure's member 8

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top