Dec 29, 2011

Variables and Data Types

Variables
Now that you know how to print (output) data on screen, you might think how to make your program reads (input) values from the keyboard? But first before performing input operation, we need a 'container' (variables) that can be used to store data (i.e input value).

A variable is a storage (memory location) where you can read and write data when necessary. A variable must be declared before it is used. Declaring a variable is performed by specifying its type and name. Consider the following program.

#include<stdio.h>

int main()
{
   int V;
   V = 15;
   printf("The value of V is %d\n", V);
   return 0;
}


The first statement declare a variable V of type int. Integer is a number without fraction component such as 3, -10 and 0. An int variable can only store integer value. The next statement is

V = 15;


The value of 15 is assigned to variable V which means V contains 15. The next statement reads,

printf("The value of V is %d\n", V);


The printf function now contains two arguments separated by a comma. The first argument is the string to be printed on the screen as explain in the previous section. But sometimes, we want to display the value of program's variables. In this example, value of V is displayed after string, The value of V is

The %d character is a special character called conversion specification. Conversion specification represents a value to be printed at that point. The type of value is printed is determined by the character specified immediately follows the percent sign. In this example, letter d signifies integer data type to be printed.

Now what is the value to be printed by printf? printf function prints the value that is specified as the second argument. Because variable V is specified, its value is printed after the string "The value of V is ".


Data Types
In the previous section we have discussed the first basic data type which is integer. As has been mentioned, integer is a sequence of one or more digits such as 15 and -382. Besides integer, C provides floating point and character data types.

Integer
Integers can be expressed in base other than decimal (base 10). If 0 is preceding the integer value, the integer is considered to be in octal notation (base 8). For example, to express 17 in octal which is equivalent to 15 in decimal, the notation is 017. An octal value can be printed by specifying its conversion specification, %o in the first argument of printf function. Hexadecimal notation (base 16) of a number is preceded by a zero and the letter x or X. For example, to express 17 in hexadecimal which is equivalent to 23 in decimal, the notation is 0x17. A hexadecimal value can be printed by specifying its conversion specification, %x in the first argument of printf function. The following two statements print variable V in octal and hexadecimal notation.

printf("The value of V is %o\n", V);
printf("The value of V is %x\n", V);


Output
The value of V is 17
The value of V is f


Floating point
Floating point is a number with decimal point (radix point) such as 3.1417, 0.158 and -2.718. Floating point number also can be expressed in exponential notation. For example, 299,792,458 can be represented as 2.997925e8 which is equivalent to 2.997925 x 108. To print floating point number, conversion specification %f is specified in the printf first argument. Conversion specification %e is used to print in exponential notation, %g is to print either in normal or exponential notation. If the value is within the range of [-4,5], %f is used, otherwise %e is used.

Character
A character in C includes letters, numerics, punctuations and control characters. Punctuations are symbols that indicates the structure and organization of written language such as colon (:), comma (,) and apostrophe ('). Control characters do not correspond to any particular symbol in natural language. Examples of control characters are carriage return, tab and backspace. Variables that are declared as character can store a single character. A character constant is formed by enclosing the character with a pair of single quotation marks such as 'A', ';' and '1'. A conversion specification, %c is specified in the first argument of printf function. The following statements assign a character to a char variable and print its value by using %c.

lttr = 'Z';
printf("lttr is %c\n", lttr);