Declaring Variables, Integers, Working with Viewer Input, and Arithmetic Operators

variable stores information. In C, we use different types of variables to store different types of information, such as numbers, letters, or words. The variable holds the information until you are ready to use it.

Declaring (Initializing) Variables

Whenever you use a variable in a C program, you must let the computer know two things: what type of variable you’re using and what the name of the variable is. This is called declaring (or initializing) your variables.

We declare variables inside the main function, before typing any body functions. For example, in this source code I have declared a variable: int year = 1971;:

#include <stdio.h>
void main( )
{
int year = 1971;
printf(“\nC was developed in %d.\n”, year);
 

This declaration tells the computer to expect an integer type of variable (int) named year and that this variable stores the number 1971. Another way to say this is: the integer variable “year” has the value “1971”.

To understand what this means, and how to read the rest of the source code, let’s learn about the different types of variables.

Integers

An integer is any number without a decimal point, such as 5 or 24567 or -98.

The range of integers that C can handle depends on your computer and your compiler. If your computer/compiler stores integers in 2 bytes of memory, then you can use any number from -32,768 to +32,767. If your computer/compiler stores integers in 4 bytes of memory, then you can use any number from -2,147,438,648 to +2,147,438,647.

There are two ways to declare an integer at the beginning of your main function:

  1. with a value: int year = 1971;
  2. without a value: int year;

Let’s study an example using the first type of declaration. In this example, we set the integer variable “year” to a value of “1971”. Type this script in your editor:

#include <stdio.h>
void main( )
{
int year = 1971;
printf(“\nC was developed in %d.\n”, year);
 

The second last line looks strange. To use the variable “year” in a printf statement, we must tell the computer/compiler what type of format to use when printing the variable. This is called a format specifier, and for integers, we use the format specifier %d which means “digit”. After the ” at the end of the statement, we add a comma and the name of the variable. The compiler will put the value of the variable (1971) into the digit format where you’ve typed %d.

Save your source code as third.c. Compile it. Link it. Run it. You will see this statement on your screen:

C was developed in 1971.  

The value 1971 appears where you typed %d. This is how we use variables in printf statements.

Working with Viewer Input

You can ask someone (called the computer “viewer”) a question in C, then use the answer in a calculation and/or printf statement. To do this, we must save the viewer’s answer in a variable. This requires four steps:

  1. declare a variable without a value (called an “empty variable”)
  2. ask the viewer a question
  3. store the viewer’s answer in the variable (called “scanning the input”)
  4. use the variable in a calculation and/or printf statement

The most important step is #3: scanning the input. To do this, use the scanf function and tell the computer two things: what format specifier you will be using and what variable you are assigning this value to. Look at this example:

#include <stdio.h>
void main( )
{
int year;
printf(“\nWhat year were you born? “);
scanf(“%d”, &year);
printf(“\nYou were born in %d.\n”, year);
 

The scanf line has four parts: the function scanf, the format specifier for digits “%d”, and the assignment operator & with the variable name year. The assignment operator assigns whatever value the viewer types to the variable “year”. So if you type “1985”, then the assignment operator will make the variable “year” equal 1985.

Type this source code in your editor and save it as fourth.c then compile it, link it, and run it. Answer the question “What year were you born?” and hit the Enter key. Your answer should appear on the screen.

Arithmetic Operators

You can add, subtract, multiply, and divide integers in C programs by using the standard symbols: + – * / with your variables. For example, let’s use your birth year to calculate your age in the year 2010:

#include <stdio.h>
void main( )
{
int year;
int age;
printf(“\nWhat year were you born? “);
scanf(“%d”, &year);
printf(“\nYou were born in %d.\n”, year);
age = 2010 – birth;
printf(“\nSo in 2010 you’ll be %d years old.\n”, age);

 

Open fourth.c in your editor. Make the changes highlighted in red above. Then compile it, link it, and run it. Answer the question “What year were you born?” and hit the Enter key. Your answer should appear on the screen.

Continue to Floating Point Variables.

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *