Echoing Input
Consider the problem of writing a program called echo that do the following:
- Ask the user to enter a whole number (no decimal places)
- Display the number that user entered back to the user.
Here is a video of how this program will work:
This program is not particularly interesting but it adds a few important elements to our original hello world program
Hello world was exclusively an output program. In this program we are also going to get input for our program by reading information from the user. This information will then be displayed back to the user so that they know it was properly received. This displaying is also sometimes called echoing. Thus the name of our program.
What we need
To accomplish this task we need to do the following
- Create a place to put the information we read from the user.
- Prompt the user for a number
- Read in the number using a library function
- Display information that we have stored
Currently we already know how to do step 2 as this is just an alteration of what we did for hello world. Our goal now is to add the other 3 steps.
Echo:
Here is the source code for this program. In the source code below, at the end of each line there is a statement that starts with //. These are called comments. A comment is a statement we put into a program to describe what is happening. In practice we do not comment every line of code but it is an effective way to deliver an explanation in the code itself.
#include <stdio.h>
int main(void){
int userInput; //declare a variable
printf("Please enter a number: "); //print a prompt
scanf("%d",&userInput); //read in the information
printf("You entered: %d\n", userInput); //echo the input
return 0;
}
Explanation
A Place to Store Information
This line of code in our program declares a variables. This is essentially asking for a spot in memory to store a piece of information.
int userInput; //declare a variable
The above can be visualized as follows:
In C, the syntax to declare any variable is the following:
typename variablename;
The typename of a variable defines: _ amount of memory we use for our data _ the type of data that we can store
The variablename is a label that we use to refer to this piece of information. While there are some rules for what is allowed for this label, we can use anything we want as long as it conforms to those rules.
We want the user to enter a whole number. The typename for this kind of variable is int which is short for integer.
The variablename is userInput. We want to name our variables so that it is a descriptive label for what we are storing.
A Function for Input
One of the functions that we can use to read data from the user is the scanf() function. This function is also part of the stdio library so we any program where we use this function will require that we include its header.
For both printf() and scanf() we have to provide arguments. Both of these functions expect a string as its first argument. This string is called a format string. In the case of the printf(), the format string indicates what is printed to the screen and the formatting of that output. In the case of scanf(), the format string indicates the format of the incoming data. We will go over this in more detail in a later section of the notes. For now, let us look at the 3 lines of our program that involves printf and scanf and look at how these work together.
This first line is not all that different from the code we wrote for Hello world.
printf("Please enter a number: "); //print a prompt
"Please enter a number: " is our format string. It is a simple output statement. What you will notice is that there is no \n at the end of this format string. The reason for this is because we want what the user enters to be placed on the same line as this prompt. If we wanted the data entry to be on the next line, we would put a \n at the end of this prompt.
This next line is our first line for input.
scanf("%d",&userInput); //read in the information
To do input, we use the scanf() function. Note that input and output are completely separate in C. scanf() can only do input, printf() can only do output. They are not interchangeable and you can't do input and output with the same function.
In this scanf() there are two arguments:
- "%d"
- &userInput
the first argument is always format string. All subsequent arguments to scanf are locations of where the data that is entered gets placed.
The format string is "%d". %d is what we call a format code. A format code tells the scanf() function what the expected input looks like. All format codes begin with %. the d indicates that the input is a base 10 whole numbers - d for decimal...or base 10.
The second argument is &userInput. userInput is the variable that we had declared earlier. The & is the address-of operator. Thus, &userInput is address of userInput. In otherwords we are providing the function with the location of userInput. This is needed because scanf has to place the values read from the user into this variable. It must therefore know where it is. It is not good enough to simply know the contents of that variable.
Displaying Stored Information
The last line before the return statement is this:
printf("You entered: %d\n", userInput); //echo the input
Here we see something new in how we use this printf() statement. This function call to printf() has two arguments
- "You entered: %d\n"
- userInput
This format string is different from previous example as it contains the format code %d. This %d is an indication to printf of where to put the value stored inside our variable.
The second argument userInput is the variable that contains the value we wish to place into the format code. For each format code present in the format string you will need a different variable.
Styling Conventions
You will notice that in the program above, all of the statements inside the main() function were indented. They were the width of 4 spaces from the left edge of the page. You will also notice that the main() function statement and the curly brackets that indicate the start and end of main were not indented. This is by design and is part of the styling conventions we follow. the curly brackets indicate the start and end of of a block of code. We generally indent by a fixed width within a block of code. The number of spaces used per indentation is fixed within the same program. for consistency. However the exact number of spaces depends on the project's style guide. For this course, we will use 4 spaces for indentation width. The actual indentation can be created by setting a tab width of 4 characters and hitting the tab key. Alternatively you can simply type 4 spaces (or set up your IDE so that it turns a tab keystroke into 4 spaces). Whether you indent with spaces or tabs is your decision... however you need to be consisten about this. Thus you should always use tabs or always use spaces but not both.
For more information about styling check out the style guide