C Tutorial (5) : Variables

You’ve heard that computers process data. Somehow you’ve got to have a way to store that data. In C, as in most programming languages, you store data in variables. A variable is nothing more than a box in your computer’s memory that holds a number or a character.

Kinds of Variables

C has several different kinds of variables because there are several different kinds of data. Not just any variable will hold just any piece of data. Only integer variables can hold integer data, only floating-point variables can hold floating-point data, and so on.

Note : Your C program’s variables vary in size, depending on the kind of data they hold, and each variable has a unique name that differentiates it from other variables.

Literal data (or sometimes constant data). Specific numbers and letters don’t change. The number 2 and the character ‘x’ are always 2 and ‘x’. A lot of data you work with—such as age, salary, and weight—changes. If you were writing a payroll program, you would need a way to store changing pieces of data. Variables come to the rescue.

Some of the Most Common Types of C Variables

c_variables

Tip : In some older C compilers, int could hold only values between 32767 and -32768. If you wanted to use a larger integer, you needed to use the long int type. In most modern compilers, though, an int type can hold the same as a long int type. If you’d like to be sure with your compiler, you can use the sizeof operator.

 

The Name column in Table lists the keywords needed when you create variables for programs. In other words, if you want an integer, you need to use the int keyword. Before completing your study of variables and jumping into using them, you need to know one more thing: how to name them.

Naming Variables

All variables have names, and because you are responsible for naming them, you must learn the naming rules. All variable names must be different; you can’t have two variables in the same program with the same name.

A variable can have from 1 to 31 characters in its name. Some compilers do allow longer names, but it’s better to stick with this limit, both for portability of code and to keep typing errors to a minimum. Your program’s variables must begin with a letter of the alphabet, but after that letter, variable names can have other letters, numbers, or an underscore in any combination. All of the following are valid variable names:

myData      pay94    age_limit    amount        QtlyIncome

Note : C lets you begin a variable name with an underscore, but you shouldn’t do so. Some of C’s built-in variables begin with an underscore, so there’s a chance you’ll overlap one of those if you name your variables starting with underscores.

The following examples of variable names are not valid:

94Pay                my Age            lastname,firstname

Note : Don’t name a variable with the same name as a function or a command. If you give a variable the same name as a command, your program won’t run; if you give a variable the same name as a function, you can’t use that same function name later in your program without causing an error.

Defining Variables

Before you use a variable, you have to define it. Variable definition (sometimes called variable declaration) is nothing more than letting C know you’ll need some variable space so it can reserve some for you. To define a variable, you only need to state its type, followed by a variable name. Here are the first few lines of a program that defines some variables:

main()
{
// My variables for the program 
char answer;
int quantity; 
float price;
/* Rest of program would follow */

You can define more than one variable of the same data type on the same line.

main()
{
// My variables for the program 
char first_initial, middle_initial;
/* Rest of program would follow. */

Note : Most C variables are defined after an opening brace, such as the opening brace that follows a function name. These variables are called local variables. C also lets you create global variables by defining the variables before a function name, such as before main(). Local variables are almost always preferable to global variables.

Storing Data in Variables

The assignment operator puts values in variables. The assignment operator is simply the equals sign (=). The format of putting data in variables looks like this:

variable = data;

The variable is the name of the variable where you want to store data. The data can be a number, character, or mathematical expression that results in a number.

answer = 'B'; 
quantity = 14; 
price = 7.95;

You also can store answers to expressions in variables:

price = 8.50 * .65;             // Gets price after 35% discount

You can even use other variables in the expression:

totalAmount = price * quantity; /* Uses value from another variable */

Note : The equals sign tells C this: Take whatever is on the right and stick it into the variable on the left. The equals sign kind of acts like a left-pointing arrow that says “That-a-way!” Oh, and never use commas in numbers, no matter how big the numbers are!

// Set up the variables, as well as define a few
char firstInitial, middleInitial; 
int number_of_pencils;
int number_of_notebooks; 
float pencils = 0.23; 
float notebooks = 2.89; 
float lunchbox = 4.99;

It’s important to note that you can reuse a variable by just assigning a new value to the variable.

Note : You can define variables and give them initial values at the same time.

printf < Prev                                                                                                             Next > Strings

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s