What is the easiest way to get an int in a console app?

Jon Chasteen picture Jon Chasteen · May 14, 2009 · Viewed 49.2k times · Source

I want to process user input as an integer, but it seems as though C has no way to get an int from stdin. Is there a function to do this? How would I go about getting an int from the user?

Answer

Marc W picture Marc W · May 14, 2009
#include <stdio.h>

int n;
scanf ("%d",&n);

See http://www.cplusplus.com/reference/clibrary/cstdio/scanf/