how to use hex escape char with string in C?

I want it to ouput "abcd", but it dosen't.

  1 #include<stdio.h>
  2 int main()
  3 {
  4         printf("a\x62cd");
  5 }
  6 

gcc alarm.c -o alarm
alarm.c: In function 'main':
alarm.c:4:9: warning: hex escape sequence out of range

It seems that the complier joint "cd" as part of \x62.
Maybe I can write it like this:

printf("a\x62""cd");

but it is a bit urgly...

That's what I ended up having to do before. That, or seperating it out of the string entirely as in printf("a%ccd", 0x62);

Why don't the complier just translates the fixed few of characters(eg: two char) after \x to an escape char?

Why should it? There are systems where "char" isn't 8 bits... Or were, decades ago, and the C standard people latch onto this technicality like a bear trap and refuse to let go.

It plainly doesn't assume it stops at 8 bits, anyway, so the question's kind of pointless.

Unicode characters aren't 8 bits, and they've been spotted in use somewhat more recently than decades ago.

The only way to produce a deterministic result when the number of output bits can not be constrained is with a maximal-munch parser.

I tried your code with the same result as you. Then I just tried the following code:

  1 #include<stdio.h>
  2 int main()
  3 {
  4         printf("a\x62mn");
  5 }
  6

with the output:

#rob@fred:~/programs$./unixcom.exee
abmn
rob@fred:~/programs$

I think what is happening with your code is that the compiler is reading the \x62cd as one hex number - \xbcd. I would guess that Corona688's suggestion - printf("a%ccd", 0x62) - is your best bet.

HTH,

Rob.

achenle - unicode or utf-16 or whatever else - have been around for a long time. They are not considered 'char' in C, they are wchar, wide characters, a different datatype. This datatype affects file orientation, which is the primary way reads & writes occur on tyy/file/device(s). wchar are NOT char, by definition and practice

try man wchar

And there still are embedded systems with 32 bit char.

And string literals aren't char datatypes, either. They're string literals.

If you're going to be annoyingly pedantic at least be correct.

"Annoyingly pedantic" is a decent description of a few corner cases in C like this. Assumptions people make about their machine (char == 8 bits) don't square perfectly with C's model of it.