Logo

Programming-Idioms

History of Idiom 120 > diff from v22 to v23

Edit summary for version 23 by programming-idioms.org:
[C] Proper imports

Version 22

2016-06-05, 11:49:18

Version 23

2016-10-02, 16:15:21

Idiom #120 Read integer from stdin

Read an integer value from the standard input into variable n.

Idiom #120 Read integer from stdin

Read an integer value from the standard input into variable n.

Imports
// Hmmm ... angle brackets don't show up!
stdio.h
stdlib.h
Imports
#include <stdio.h>
#include <stdlib.h>
Code
char inbuff[1000];

int main(int, char **) {
   int n;
   if (!fgets(inbuff, sizeof(inbuff), stdin))
      fabort("Can't read from input");
   n = atoi(inbuff);
   printf("You entered %d\n", n);
}
Code
char inbuff[1000];

int main(int, char **) {
   int n;
   if (!fgets(inbuff, sizeof(inbuff), stdin))
      fabort("Can't read from input");
   n = atoi(inbuff);
   printf("You entered %d\n", n);
}