I have problem in this code. This is behaving so weirdly. please explain.
printf("CONTROLS:\nw , %c: up\n"
"a , %c: left\n"
"s , %c: down\n"
"d , %c: right\n"
"q , ESC: EXIT\n"
,24,27,25,26);
OUTPUT:
CONTROLS:
w , ↑: up
a , left
s , ↓: down
d , →: right
q , ESC: EXIT
I tried removing the colon after %c but it eats the 'l'
what is happening. I am interested in knowing the behind the scenes.
//this works:
for(int ch=24;ch < 28;ch++) /* 27 = Esc key */
{
printf("%c:%d\n",ch,ch);
}
Your original code doesn't work out for a few reasons, but most notably because the
printf()formatting is using%c( forchar) and the codes ( 24, 27, 25, 26 ) given to the%creferences inprintf()do not align with the symbols you desire.Here is some code that should work for you. It uses Unicode strings to get the results you want. Please also note the
printf()formatting in this instance: we use%sfor strings instead of%c-- which is used for chars.This code actually gives the output shown below, and the runnable version is here.
EDIT: In appreciation of John Bollinger's comment below -- and always finding better ways of doing things -- please note the code update for the
szUPdeclaration: it uses the newer/proper Unicode coding now. The results are in fact, identical and the runnable code is here.Output: