C Basics

C Language


In this lesson, we will discuss some of the basics of C language, including variables and user inputs.


Variables are spaces in the computer memory that stores values. Imagine it like this: variables are boxes, and the values stored in the variables are things you put in the boxes. You can change what you put in the box anytime. This is useful to store results of operations or data for future use.

Declaring a Variable

To use a variable, you first need to declare it, or tell the computer that you will be using variable, what kind of variable, and the name for it. Let's look at an example first:

#include <stdio.h>

int main()
  int temperature;
  printf("The temperature today is %d degrees Celsius.\n",temperature);

The temperature today is 27 degrees Celsius.

As shown in the program above, the declaration for a variable is formatted like this:
int temperature;
The first part of this line, "int", tells the computer what type of variable this is. It is important because different types of variables takes up different amount of memory in the computer, so the computer need to know beforehand and "reserve" the space for the variable. In this case, the number is an integer, or whole number and their opposites. "int" is the short for integer and tells the computer that the data stored in the variable is going to be numbers without decimals. There are several types of variables, as shown in this table (those are the most used ones):

Variable Type Meaning Examples
int Integer 3, -4, 15, 232, -9232
float Floating Point (decimal) 2.39252, -2032.2, 2.3339, -0.5
char Character 'a' , 'A' , '!' , '(' , ' ' , '9'
long int Longer Integers 298342, -282523, 9325

int defines an integer, float defines a floating point decimal, char defines a character (a single character, which can be a letter, and symbol, or a number, basically everything on the keyboard), and long int is used when the number that needs to be stored is a large number.

The next part in the declaration of a variable is the name that you give the variable. This can be anything, but it should convey what the variable will be used to store, so that you don't mix it up with other variables you are going to use and it is easy to remember. Also, the name has to only consist of letters and underscores ( _ ), and cannot repeat (you cannot name a variable "num" and name another one "num" too, which will get mixed up by the computer). For example, in the example program, I used the variable "temperature" to store the daily temperature.

At last, don't forget the semicolon at the end of the statement.

Defining Multiple Variables in One Line

As long as the type of the variables are the same, you can define multiple variables in a single line, separated by commas. For example:
int num_1,num_2,num_3;
float grade,average;
This will declare all of the listed variables and save time.

Assigning Values

Now you have made a "box" to store things in the computer by declaring a variable, what about putting things in it? You can store values in the variable by assigning values to it. Let's say you want to let temperature to be 27. To do this, simply type in:
First, put the variable that you want to assign the value to; then an equal sign for "assigning"; and at last, put the value that you want to assign. This value can even be another variable. Be careful when you assign values to a character variable. A character is always enclosed by single quotation marks, so when you assign values to a character variable, you will need to remember to put the single quotations:
Another thing you need to remember: the computer will assign the value after the equal sign to the variable before it, and it will not work in the opposite way:
This will assign the value of temperature to 27, which cannot be done since 27 is a constant that cannot change.

Declaring and Assigning in One Line

You can also assign a value to a variable in the same line that it is declared. Just put '=' then the value after the name of the variable:
int number=3;
Before you can use a variable, there must be a value stored in it. For example, before you print out "temperature" in the example program, you must first assign it a value, or else it will be a random number, which can be very dangerous and cause disastrous results in your program, so it may be a good idea to assign a value to a variale once it is declared.

Printing a Value

To print a value of a variable through the printf statement, you cannot just put the variable in the statement. Similar to the declaration, you also need to tell the computer what type of variable it is, and which one you want to print out.

printf("The temperature today is %d degrees Celsius.\n",temperature);

In this statement, you can infer that the %d prints out the value in temperature. The %d tells the computer that it is going to print out a value in a variable, and specifically, an integral variable. The computer will continue to read the statement and skip to after the end of the quotation marks. It will see that the variable you want to print out is "temperature". So when you want to print out an integral variable in a printf statement, put %d instead of the variable in the place where you want it to print, put a comma after the quotation ends, and put the name of the variable that you want to print after it. You can also print out multiple variables like this:

printf("The first number is:%d, the second number is:%d.\n",num_1,num_2);

Just put the variables in order of occurance after the quotation, the computer will match them.

To print other types of variables, just put %f for float, %c for char, and %ld for long int. The other parts will be the same as printing an integer.

Operations with Variables

Doing operations with variables can make your program very useful. For example, in a simple addition calculator program, you will want to add the two values together. In order to do that, you will need to get inputs from the user and add them. We will talk about adding first. It is the same concept with assigning value, and if you think about it, doing operation is just assigning new values to a variable. Here is the statement that add up the two values:


You can see, the computer assign the sum of num_1 and num_2 to result. If num_1 is 3, and num_2 is 5, the computer will do 3+5, get 8, and assign it to result. Operations you can do with variables include +, -, *, /, and % (a%b: find the remainder of a/b, Ex. 27%6=3; 45%4=1). You can also do operations with the variable itself, and assign it to itself. For example, if you want to increase the value of "temperature" by 1, you can do:
If temperature is originally 27, the computer will first do 27+1, get 28, and assign it to temperature, which change the value of temperature to 28.
These operations, such as increasing the value of a variable, can be written in a shorter way in C. Let temperature increase by 1 can also be written like this:
This will also change temperature to the original temperature+1. You can also apply to other operations:
num*=5; is the same as: num=num*5;
(this will change the value of num to 5 times as much).
Because increasing or decreasing a value by 1 is such a common operation in programming, there is a even shorter way of writing it.
num++; will increase num by 1 after the computer is done with this operation (printf("%d\n",num++); will print out the value of num, then increase it by one);
++num; will increase num by 1 before the computer do this operation (printf("%d\n",++num); will increase the value of num by 1, then print it out);
num--; will decrease num by 1 after the computer is done with this operation;
--num; will decrease num by 1 before the computer do this operation.

Inputs From User

To get inputs from user, you would need a variable to store the input that the user puts in. So before you use the scanf statement to get the input, you will need to declare a variable. Here is how you get inputs:

#include <stdio.h>

int main()
  int num;
  printf("Enter a number:\n");
  printf("You entered %d!\n",num);

Enter a number:
You entered 6!

The scanf statement is similar to the printf statement. First you need a string of characters that the user will need to put in. In the example above, the user will put in an integer (you can put anything in the quotation marks, such as "%c%d%c", which will require the user to put in a character, followed directly by an integer, then another character; or even "Input:%d", the user will need to put in "Input:", then an integer, for example "Input:5"). The computer will look after the quotation mark and find the variable that you are going to store the value into. It is almost the exact mirror of a printf statement, so you can just remember it like that, the computer will just scan in a string from the user and store in a variable, instead of printing a string and taking a value from a variable. A major difference is the '&' sign. This is very important in a scanf statement and not included in a printf statement. The '&' is an operator that takes the address of a variable. Think of it this way: each of the boxes you created have a number, or address, to help you locate it on a shelf easily. To put something in the box, you will need to know where to find it. The address here, tells you where it is. This way, you can store your value. Remember to put the '&' sign before your name of the variable to tell the computer to "get the address of that variable". You don't need this for assigning values to variables though.
This scanf statement will allow the user to put something in on the terminal, and after hitting the enter key, it will send the value to the variable that you indicated. It will assign that value to the variable.

Adder Program

This program will add two numbers that the user puts in, and print out the result. To do this, it needs three variables: result, num_1, and num_2. With scanf statement, it will get the input from the user; then assign the sum of num_1 and num_2 to result; and at last, print result out.

#include <stdio.h>

int main()
  int num_1,num_2,result;
  printf("Enter two numbers to be added separated by a comma:");

Enter two numbers to be added separated by a comma:676,231


Try executing this program:

#include <stdio.h>

int main()
  float pi=3.1415926;
  int radius;
  float area,circumference;
  printf("Enter the radius of the circle:\n");
  printf("In a circle with a radius of %d:\nThe area is %f\nThe circumference is %f\n",radius,area,

Find the mistakes in the following program:

#include <stdio.h>

int main()
  int average=95.69;
  int difference;
  printf("The average score is %d.\n",average);
  printf("Enter your score on the test:");
  printf("Your score is %d higher than the average (positive number means you scored higher than the 
  average; negative number means you scored lower than the average)\n");

Write a program that allows the user to enter three decimals, and find the sum and product of the three, and print it out.

Write a program that allows the user to enter the base and height of a triangle, and prints out the area of it.

Additional practice on variables and inputs:

Activity 3.2

<< Previous TOC Next >>