P
pandit
AIM: To read a number from standard input
WHAT I DID: I use getchar() to read a single digit. Can use strtol() for numbers more than one digit long.
PROBLEM: isn't there a better way ?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 0;
i = getchar();
if((EOF == i) && (ferror(stdin)))
{
printf("ERROR reading input @LINE = %d\n", __LINE__);
}
else
{
i = i - '0';
}
return 0;
}
-- output here --
../a.out
123
output
1
WHAT I DID: I use getchar() to read a single digit. Can use strtol() for numbers more than one digit long.
PROBLEM: isn't there a better way ?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 0;
i = getchar();
if((EOF == i) && (ferror(stdin)))
{
printf("ERROR reading input @LINE = %d\n", __LINE__);
}
else
{
i = i - '0';
}
return 0;
}
-- output here --
../a.out
123
output
1