tags:

views:

87

answers:

2

How could I include a unit separator (value 31 in ascii table) in a string other than using snprintf()? I want to do like we normally initialize a string.

eg

char[100] a = "abc"
+7  A: 

31 in dec = 0x1f in hex. Therefore,

char x[] = "blah\x1f" "blah";
//              ^^^^ unit separator.

The string is split into two to avoid the compiler reading the escape sequence as 0x1fb. Alternatively you could use octal sequence:

char x[] = "blah\037blah";
//              ^^^^ unit separator.
KennyTM
thanks for your reply. I tried to implement it but I think because my string include other int value as well,say "\x1f15". Hence, I think the compiler try to take the 15 as well. It gives the warning of hex sequence out of range. As way around this?
Ashley
@Ashley: See update.
KennyTM
+2  A: 

You can do:

char str[] = {'a',31,'b','c',0};
codaddict
You can - but would you really want to? It might not be too painful for this string, but for anything longer, or doing it more than once ... ouch!
Jonathan Leffler