Tutorial 2: Variables and Data Types

Variable is a memory location that can store a value. The stored value can be retrieved or modified when necessary. Initialization of a variable means a value is given to the variable during its declaration. When a variable is declared, its name and data type are specified.

A variable name can contain letters, digit and underscore (_). It cannot begins with a digit. C is case sensitive, therefore v and V are considered two different variable names. Its data type is given to specify what type of data can be stored in the variable.

There are four basic data types in C programming language: int, float, double and char. Each data type has its own size. The size of a variable is the memory space allocated in the memory required to store its value. The following example demonstrates on how to declare a variable for each data type.



The statements declare four variables of types int, float, double and char with the name of num1, num2, num3 and letter. Now that we have declared the variables, lets look at how to initialize them.



We use '=' (assignment operator) to initialize the variables such as the value of 5 into variable num1. We could also perform the declaration and initialization on a single statement. This is demonstrated in the following program.

0 comments:

Post a Comment