learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back


Lesson 2: How to declare and use variables in C.

This lesson will show you how to:

  • declaring and using variables
  • some keywords
  • all keywords must be typed in lowercase
  • C is case sensitive

    Q: Why won't sharks attack lawyers?
    A: Professional courtesy.

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

Variable names must start with a letter or an underscore. The remaining characters can be letters, numbers or the underscore, no other characters can be used - those are the rules guys!. A variable name must 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.

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;

Remember that variables must start with an underscore or a letter. The remaining characters must be letters, numbers or the underscore. No other characters can be used.


Data Types


C provides built in data types for character, float and integer data. You can even define your own data types in C. In C you may assign a value to a variable when you are declaring it.

Integer variables are used to store whole numbers (e.g. 1, 100, 500, 37 etc.). 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 (how large the number is), 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.

Example 1:


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 (how many decimal places the number has). 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.

Example 2:


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.

Example 3:


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

Note that when declaring a single character (e.g. A or b or X or 3) you should enclose the character in a single quotation mark ' ' (e.g. 'A', 'b' etc..). If you are assigning more than one character you should use the full double quotation marks " " (e.g. "string", "do" etc.). This will be explained later in a further tutorial.

Remember:

int x;

x = 5;          /* This is fine. */
5 = x;          /* This is illegal. */
       

back to top        go to next lesson

 

 






Hosted by www.Geocities.ws

1