Skip to content

Instantly share code, notes, and snippets.

@fstamour
Created April 24, 2013 01:12
Show Gist options
  • Save fstamour/5448824 to your computer and use it in GitHub Desktop.
Save fstamour/5448824 to your computer and use it in GitHub Desktop.
Minimal C code to read an int from a stream. This doesn't comply with the standard atoi at all, but it's just for simplicity.
int i = 0;
//FILE* in = stdin;
char c = getc(in);
while( c >= '0' && c <= '9' ) {
//i = i*10 + (c - '0'); //Equivalent to the line below
i = (i<<3)+(i<<1)+c-'0';
c = getchar();
}
ungetc(c, in);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment