Take User Input in C With Coding Project Example

Share This Post

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 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
fprintf(stderr, "!> Proper syntax: $ %s\n", USAGE);

// 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]);

// 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!

That's weird... something went wrong. Please try again.
Welcome to the R U Coding Me Newsletter!

R U Coding Me Newsletter

Subscribe to our newsletter and stay updated.

We use Brevo as our marketing platform. By Clicking below to submit this form, you acknowledge that the information you provided will be transferred to Brevo for processing in accordance with their terms of use

Are You Coding?

If the answer is no, you’re probably missing out on a large opportunity here. And yes, I said the name!

Our FREE developer resources will help you start programming. Additionally, consider applying for mentorship to accelerate you to your career. Click on the Student button below to get started. It will take you to our available courses and any relevant materials to help get you started.

Ready to take your business to the next level? Creating a robust solution in a short amount of time is hard to do if you’ve never done it before. Plus, why should you juggle yet another project with your business? Click on the Business button below if you’re looking to scale your current business online but don’t want to spend a few years to learn how to do it. It will take you to our services so you can see what would best work for you.

More To Explore
Learn C Programming
Multidimensional Arrays in C With Example

FacebookTweetPinLinkedInEmail Wait, You Said There Wouldn’t Be Any More Pointers! No, if you recall from last time, I mentioned there could be pointers that point

Learn C Programming
Efficiently Debugging C Programs

FacebookTweetPinLinkedInEmail What is Debugging? Overview of Debugging Debugging is an art form, rather than a science. Debugging is the act of removing any issues that

Learn C Programming
Pointers in C Made Easy

FacebookTweetPinLinkedInEmail Pointers in C What are pointers? Why do we even need these? Why can’t I find my wallet? Great questions, let’s dive in! Pointers

R U Coding Me LLC