Reputation:
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
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