Saturday, October 13, 2007

Variables,constants and Data types

Programming is all about processing data to get the desired results. Programs accept data and perform some kind of task. The task could be as simple as calculating percentage of marks or as complex as DNA fingerprint analysis.

In this article, you will learn how C programs work with data and perform tasks.

Data is generally defined as the raw facts input into the computer. However, in reference to C, data has a more specific meaning. It refers to two kinds of values — those associated with constants and those represented by variables.

Variables and constants

Constants refer to data that remains unchanged throughout the program execution. For example, in a program to find the area of a circle, the value of ∏ (3.1412) is constant. Some more examples of constants include numbers such as 10, 100, 22.34 and characters such as ’A’,’ ’Z’, ’*’ and ’$’.

Variables, on the other hand, store data that may vary during program execution. In the example of finding the area of a circle, the area will change from circle to circle.

In programming, each variable actually refers to a memory location where a constant is stored. Every variable has a name. You should note the following rules before naming a variable:

  1. Only alphabets, numerals, and underscores (_) are permitted (spaces and special symbols are not allowed).
  2. The name cannot start with a digit.
  3. Upper case and lower case letters are treated as different.
  4. A variable cannot be a C language keyword.

Note: Always choose meaningful names for variables.

Examples of variable names: marks1, student_no, sum, product, etc.

Data types

The C compiler must be informed about the type of data (numbers, alphabets, or special characters) you intend to store in a variable. For this, you can use C’s built-in data types.

There are three primary data types in C — int, float, and char. Other data types such as long and double are derived from these.

The int data type

The int data type can be used to store both positive and negative integers, and thus stands for ‘signed integer’. This data type also offers variations such as long and short to accommodate larger and smaller values. See Table 1 for information on the amount of memory this data type occupies and the range of values you can store).

The float data type

The float data type is used to store fractional numbers (real numbers). So, if you wish to write a program to compute something, you should use the float data type. A float value consists of an exponential and a fractional portion. Consider these values:

2.3456e-1 =2.3456 x 10-1 = 0.23456

2.3456e+2 =2.3456 x 10+2 = 234.56

Here, the values to the left of the character ‘e’ denote the fraction while those to the right denote the exponent. The complier uses the e to separate and identify these two components. The exponent must have a positive or negative value (positive by default). To accommodate larger fractions, C provides another data type called double. See Table 1 for the memory this data type occupies and the range of values you can store using float and double).

The char data type

The char data type is used for storing characters such as alphabets and special symbols (e.g. ‘*’, ‘$’, and ‘#’). Computers use a numerical code called ASCII (American Standard Code for Information Interchange) to represent these characters. There are 256 characters in the ASCII set, all of which can be stored using the char data type.

Note: All the data types discussed above can be prefixed with the keywords signed or unsigned. The keyword signed represents both positive and negative values, whereas unsigned refers only to positive values.

Table 1

Data type

Memory size (in bytes)

Range

int

2 or 4 (depends on whether you are using 16-bit processor or 32-bit processor)

-32,768 to +32767 (2-bytes)

-2,147,483,648 to +2,147,483,648 (4-bytes)

short

2

-32768 to +32767

long

4

-2,147,483,648 to +2,147,483,648

float

4

±10 e-37 to ±10e+37

double

8

±10 e-307 to ±10e+307

char

1

-128 to +127

Declaring variables in a C program

As mentioned earlier, you have to inform the compiler about each variables name and the kind of data it is meant to store. This is called variable declaration. For example, you can create a variable called ‘num’ to store an integer value in memory by writing the statement:

int num;

Here, int is the data type and num is the name of the variable.

No comments: