C& CPP Tutorials
About C
Program Structure
Running C Program
Variables, constants
Operators
Control Structure
Array
Function
Pointers
Preprocessor
Structure
File
Bitwise Operators
Recursion
 Data Structures
Introduction
Stacks
Queues
Linked List
Sorting 
Searching
 Test Your Skill
Fundamentals
Input and Output
Branching and Looping
Function
Pointers I
Pointers II
Structure and Union
Sample Problems I
Sample Problems II
 Help and Support
C Forum
Source code
C  Yahoo Group
C Compilers
 

Variables and Data Type

The first thing you need to know is that you can create variables to store values in. A variable is just a named area of storage that can hold a single value (numeric or character). Every variable has a name and a value. 

The name identifies the variable, the value stores data. There is a limitation on what these names can be. Every variable name in C must start with a letter, the rest of the name can consist of letters, numbers and underscore characters. No Space allowed in variable name. C recognizes upper and lower case characters as being different. Finally, you cannot use any of C's keywords like main, while, switch etc as variable names. 

It demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.

There are five basic data types associated with variables
  int -        integer: a whole number. 
  float -    floating point value: ie a number with a fractional part. 
  double - a double-precision floating point value. 
  char -     a single character. 
  void -     valueless special purpose type which we will examine closely in later sections. 

Integer Number Variables


The first type of variable we need to know about is of class type int - short for integer. An int variable can store a value in the range -32768 to +32767. You can think of it as a largish positive or negative whole number: no fractional part is allowed. To declare an int you use the instruction: int variable name;
For example:

int a;

declares that you want to create an int variable called a.To assign a value to our integer variable we would use the following C statement.

  a=10;


The C programming language uses the "=" character for assignment. A statement of the form a=10; should be interpreted as take the numerical value 10 and store it in a memory location associated with the integer variable a. The "=" character should not be seen as an equality otherwise writing statements of the form:

 

 a=a+10;


This statement should be interpreted as take the current value stored in a memory location associated with the integer variable a; add the numerical value 10 to it and then replace this value in the memory location associated with a.

     Decimal Number Variables

As described above, an integer variable has no fractional part. Integer variables tend to be used for counting, whereas real numbers are used in arithmetic. C uses one of two keywords to declare a variable that is to be associated with a decimal number: float and double. They are each offer a different level of precision as outlined below. 

float 

A float, or floating point, number has about seven digits of precision and a range of about 1.E-36 to 1.E+36. A float takes four bytes to store. 


double 

A double, or double precision, number has about 13 digits of precision and a range of about 1.E-303 to 1.E+303. A double takes eight bytes to store. 

For example:

 float total;

 double sum;


To assign a numerical value to our floating point and double precision variables we would use the following C statement.

 

 total=0.0;

 sum=12.50;


Before you can use a variable you have to declare it. As we have seen above, to do this you state its type and then give its name. For example, int i; declares an integer variable. You can declare any number of variables of the same type with a single statement. 

For example: 

 int a, b, c;


Here is an example program that includes some of the concepts outlined above. It includes a slightly more advanced use of the printf function which will covered in detail in the next part of this course:

main()
{
  int a,b,average;
  a=10;
  b=6;
  average = ( a+b ) / 2 ;
  printf("Here ");
  printf("is ");
  printf("the ");
  printf("answer... ");
  printf("\n");
  printf("%d.",average);
}

 

 Here is the answer... 8

 

Back Next
 

Google