www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C Tutorial - Lesson 2: Variables

Declaring and Using Variables

by John Kopp

Support this site at no cost to you

Welcome to the second lesson in easyCPlusPlus.com's C tutorial. This lesson will teach you how to declare and use variables in C. A variable is used to hold data within your program. A variable represents a location in your computer's memory. You can put data into this location and retrieve data out of it. Every variable has two parts, a name and a data type.

Variable Names
Valid names can consist of letters, numbers and the underscore, but may not start with a number. A variable name may not be a C keyword such as if, for, else, or while. Variable names are case sensitive. So, Age, AGE, aGE and AgE could be names for different variables, although this is not recommended since it would probably cause confusion and errors in your programs. Let's look at some variable declarations to better understand these rules. Note that int, float and double are built in C data types as explained latter in this lesson.

List of C Keywords

Which of the following are valid variable names?

int idnumber;
int transaction_number;
int __my_phone_number__;
float 4myfriend;
float its4me;
double VeRyStRaNgE;
float while;
float myCash;
int CaseNo;
int CASENO;
int caseno;

ANSWERS

Data Types
C provides built in data types for character, float and integer data. A mechanism, using the keyword typedef, exists for creating user-defined types. As an aside, in C you may assign a value to a variable when you declare it.

Integer variables are used to store whole numbers. There are several keywords used to declare integer variables, including int, short, long, unsigned short, unsigned long. The difference deals with the number of bytes used to store the variable in memory, long vs. short, or whether negative and positive numbers may be stored, signed vs. unsigned. These differences will be explained in more advanced tutorials. For now, use int to declare integer variables. On most 32-bit systems, int is synonymous with signed long.

Examples:

int count;
int number_of_students = 30;


Float variables are used to store floating point numbers. Floating point numbers may contain both a whole and fractional part, for example, 52.7 or 3.33333333. There are several keywords used to declare floating point numbers in C including float, double and long double. The difference here is the number of bytes used to store the variable in memory. Double allows larger values than float. Long double allows even larger values. These differences will be explained in more advanced tutorials. For now, use float to declare floating point variables.

Examples:
float owned = 0.0;
float owed = 1234567.89;


Character variables are used to store character values. The use of characters and strings will be covered in a latter tutorial. Character variables are declared with the keyword char.

Examples:
char firstInitial = 'J';
char secondInitial = 'K';

Previous Page       Next Page