anon
anon

Reputation:

Print unsigned char as character (which format specifier?)

I have unsigned char variable which stores ASCII code of some symbol (e.g., '1'). I want to print the symbol. Am I using the correct format specifier? Is this well defined? or should I cast it to int inside sprintf?

unsigned char unsignedCharVar = 49;
sprintf(dest,"%c", unsignedCharVar); // should print '1'

Upvotes: 0

Views: 2498

Answers (2)

cruelcore1
cruelcore1

Reputation: 578

u specifies unsigned for all types so try %uc.

Upvotes: 1

Cantfindname
Cantfindname

Reputation: 2138

You do not need to cast to char, since (non-extended) ASCII has codes 0-127, which means the representation is the same in both unsigned and signed char. You just have to make sure that your unsigned char contains a valid value.

Upvotes: 0

Related Questions