Home > Software engineering >  Why does "%d" can print out ASCII code not only decimal?
Why does "%d" can print out ASCII code not only decimal?

Time:02-11

I heard that '%d' prints decimal, but if I import printf("%d",'a'), it prints out a ASCII code of 'a'. I cannot understand why %d which only can print out decimal also prints ASCII code. is ASCII code a decimal?

CodePudding user response:

Computers do not know anything about "decimals". Compueters know numbers. Generally everything is a number in computers. Characters, strings - even machine code instructions.

Humans need numbers to be displayed in an understandable form. We are used to use base 10 numbers called by you "decimals".

Characters are numbers too. We (humans) have invented encodings to know what number represents a particular character. Very popular is ASCII system. When you printf("%d\n", 'A'); then you simple print number representing character 'A' using the base 10 representation.

  • Related