Skip to content
Snippets Groups Projects
Commit ce95e8cc authored by Bruce Cowan's avatar Bruce Cowan :airplane:
Browse files

Add insane number reading through getchar

parent dbfa2693
No related branches found
No related tags found
No related merge requests found
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#define LEN_DIGITS 18 // Safe digits of INT64_MAX
int
main (void)
{
int64_t value = 0;
printf ("Put the number in: ");
for (int i = 0; i < LEN_DIGITS; i++)
{
int ch;
if ((ch = getchar ()) == '\n')
break;
int digit = ch - '0';
value = 10 * value + digit;
}
printf ("Value is %" PRId64 "\n", value);
return 0;
}
......@@ -18,6 +18,7 @@ executable('fib', 'fib.c')
executable('fixed-sizeof', 'fixed-sizeof.c')
executable('fgets', 'fgets.c')
executable('gcd', 'gcd.c')
executable('getchar', 'getchar.c')
executable('kepler', 'kepler.c', dependencies: libm)
executable('next', 'next.c', dependencies: libm)
executable('parrot', 'parrot.c')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment