Printing a char with printf

后端 未结 6 1497
粉色の甜心
粉色の甜心 2021-01-01 11:36

Are both these codes the same

char ch = \'a\';
printf(\"%d\", ch);

Will it print a garbage value?

I am confused about this

6条回答
  •  被撕碎了的回忆
    2021-01-01 12:13

    In C char gets promoted to int in expressions. That pretty much explains every question, if you think about it.

    Source: The C Programming Language by Brian W.Kernighan and Dennis M.Ritchie

    A must read if you want to learn C.

    Also see this stack overflow page, where people much more experienced then me can explain it much better then I ever can.

提交回复
热议问题