Processing User Input in C
One way to make your programs cooler is to take inputs from users! There are two primary ways to do this. One is to specify command line arguments, where the user will provide additional arguments to the command. One common example is the GCC compiler. In order for this to compile your code, you need to provide the filename of the code you would like to compile.
The other technique we’ll look at in this post is runtime arguments. Unlike command line arguments, runtime arguments are given during the execution of the program (during runtime). This will completely stop the process running your program until user input is supplied.
Before we talk about user input, it is important to recognize one thing: user input is the bane of a programmer’s existence. When developing an application, users may unintentionally (or intentionally) provide incorrect feedback. Whether it is the wrong data type, way too many characters or malicious code that will destroy your system, it is important to sanitize your inputs.
scanf is one of the most popular ways of reading input from the users. However, it is insecure. When grabbing user inputs, be sure to use one of the secure methods below:
There are a couple of ways we can get runtime input from the user. The best way to do this is with the following:
char buffer[1024];
printf("Enter input: ");
fgets(buffer, 1024, stdin);
We always want to prompt our user when asking for input. Additionally, we need to consider the security of our call. Other techniques, such as scanf and fscanf do not implement checks to see if your input will fit in the buffer allotted. This can be problematic if you have inputs larger than your buffer, as it will overwrite other locations in RAM. This is called buffer overflow. The fgets method alleviates buffer overflow by only accepting however many characters you specify in the second parameter. fgets will also leave room for a null terminator at the end.
If you have been following this series, thank you -first off! Also, try reworking some of your previous programs to accept user input! If you haven’t, go check out some of our earlier posts and rework these progr
Example Project
We have a new example that should take user input. If you recall from the arithmetic section, we made a basic calculator that requires the user to enter in
Copy the below code into your environment and create a calculator app that takes user input! To keep things simple, use integers to store two numbers inputted by the user. You can also store the operation (+, -, *, /, %) as a character variable. You can use conditional logic to see what operation you need to use and display the result to the screen. NOTE: You should use typecasting for division in order to get the most accurate result.
Once you get the basic program working, you can expand your code to take decimal expressions and even both integer and decimals! Once you are content with your code, move on to the next section of the course.
#define MINIMUM_ARGUMENTS 4
#define USAGE "./a.out <number> <+ | - | * | / | %%> <number>"
// Code Entry. We take command line arguments from the user and conduct a
// mathematical expression with the given values.
int main(int argc, char **argv)
{
int i; // Incrementor for later ;D
// Check if user inputted enough arguments
if (argc < MINIMUM_ARGUMENTS)
{
fprintf(stderr, "!> Proper syntax: $ %s\n", USAGE);
exit(1);
}
// User-input is stored in an array of strings: argv.
// The number of arguments is stored in argc, where
// ./a.out is the first argument.
printf("User inputed: ");
for (i = 0; i < argc; i++)
{
printf("*%s* ", argv[i]);
}
printf("\n");
// TODO(YOU): Process user input
// TODO(YOU): Store the numbers in variables
// TODO(YOU): Determine the operation
// TODO(YOU): Perform the operation with the stored user input and
// output the results
return 0;
}
Wrapping It Up
Now that you can process user input and the basics of arrays, we can expand into some pretty fun projects moving forward! Be sure to stick around for our next post, where we’ll cover some debugging techniques and using libraries!
Founder and CEO of R U Coding Me LLC. Jacob obtained his Bachelor’s of Computer Science at the University of Central Florida. He likes to go to the gym and teach people about technology.