Reading A Number
At the end of this page, you will learn how to read a number from the character input into an int
variable. You could've heard that there's a function called scanf
, but we won't use it just yet; instead, I'll ask you to write your own function int read_int()
to read an integer and return it.
But before we do it, we'll need to understand how to read digits and get their numeric values.
Character codes
When we read one character, as in c = getchar()
, the value of c
– if not EOF
– will be an ASCII code of a character. If a character is '0'
– yes, we use single quotes to specify a single character value in C – the actual value in c
will be 48, 1
is 49, and so on: this is how characters are arranged in ASCII table.
'0' '1' '2' '3' '4' '5' '6' '7' '8' '9'
48 49 50 51 52 53 54 55 56 57
So, to convert e.g. 3
to its numeric value 3, you will need to subtract 48 from it. But the fun thing is that you never need to remember the constant 48: 48 is '0'
. Since the digits follow each other in the ASCII table, and you know that c
is a digit, you can calculate c - '0'
to get it's numeric value.
Indeed:
'0' - '0'
is 0,'1' - '0'
is 1,'2' - '0'
is 2,- ...
'9' - '0'
is 9.
Now, how can we check that c
is indeed a digit and not something else? Same way: we can compare it to 0
and 9
and verify that it is between of them: c >= '0' && c <= '9'
. If you don't want to write this condition every time you need to check that c
is a digit, there is a special function isdigit(c)
; to use it, you will need to #include <ctype.h>
in addition to stdio.h
which we normally include for printf
, getchar
, and putchar
.
Let's get to an exercise! Assume that the input for your program is a positive number. Implement a function int read_int()
which would read that number, character by character, using getchar()
. You must stop when you see a non-digit! When you have your digit, get its numeric value by subtracting '0'
from it.
Let's imagine your input is 123
, that is: 1
, 2
, 3
, and possibly \n
at the end of line, and EOF
. Your function will read them character by character and need to calculate the resulting number in a variable, let's say, int result
, which initially has 0:
- read
'1'
: numeric value 1, result must become 1 - read
'2'
: numeric value 2, result must become 12 - read
'3'
: numeric value 3, result must become 123 - read
'\n'
: not a digit, should exit the loop - return your result, which is 123.
Take some time and write this code! I'll include ctype.h
for you so you could use isdigit(c)
.
To test your code, let's read the value and print this value plus one. This is to make sure you are actually reading it into an int
, and not just printing it as is. Make sure all tests pass!
#include <ctype.h>
#include <stdio.h>
int read_int() {
int result = 0;
/* your code here */
return result;
}
int main() {
int value;
value = read_int();
printf("%d\n", value + 1); /* print value plus one */
return 0;
}
Were you able to make it work?
If you noticed that exercises are becoming a little bit harder, that's because they are! We are learning new functions and new language constructs, which will allow us to write bigger pieces of code. Keep reading!