Character to Integer in C

Level 269 keystrokes

Converts the digit character '7' to the integer 7 by subtracting '0' from it, since digit characters have consecutive codes, and prints "Number: 7".

C code

// '7' is a character, not the number 7: its ASCII code is 55
1char digit = '7';
// Digits '0' to '9' have consecutive codes, so '7' - '0' = 55 - 48 = 7
2int num = digit - '0';
// Prints the integer value: "Number: 7"
3printf("Number: %d\n", num);

Output

Number: 7
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • '× 4
  • ;× 3
  • "× 2
  • =× 2
  • -× 1
  • ,× 1
  • :× 1
  • (× 1
  • )× 1
  • \× 1

More C examples at level 2

← C typing practice