Tuesday 3 July 2012

Declaration Of Variables


After designing suitable variable names, we must declare them to the compiler. Declaration does two things:
I. It tells the compiler what the \ ariable name is.
2. It specifies what type of data the variable will hold.
The declaration of variables must be done before they are used in the program.
Primary Type Declaration
A variable can be used to store a value of any data type. That is, the name has nothing to do with its type. The syntax for declaring a variable is as follows:
data-type vl,v2,....vn
vl, v2, ....vn are the names of variables. Variables are separated by commas. A declaration statement must end with a semicolon. For example, valid declarations are:
int count ;
int number, total;
double ratio;
int and double are the keywords to represent integer type and real type data values respectively. Table  shows various data types and their keyword equivalents.




The program segment given in Fig.  illustrates declaration of variables. main() is the beginning of the program. The opening brace signals the execution of the program. Declaration of variables is usually done immediately after the opening brace of the program. The variables can also be declared outside (either before or after) the main function. The importance of place of declaration will be dealt in detail later while discussing functions.

When an adjective (qualifier) short, long, or unsigned is used without a basic data type specifier. C compilers treat the data type as an int. If we want to declare a character variable as unsigned. then we must do so using both the terms like unsigned char.


User-Defined Type Declaration
C supports a feature known as "type definition" that allows users to define an identifier that would represent an existing data type. The user-defined data type identifier can later be used to declare variables . It takes the general form:
typedef type identifier;

Where type refers to an existing data type and "identifier" refers to the "new" name given to the data type. The existing data type may belong to any class of type, including the user-defined ones. Remember that the new type is 'new' only in name, but not the data type. typedef cannot create a new type. Some examples of type definition are:
typedef int units; typedef float marks;

Here, units symbolizes int and marks symbolizes float. They can be later used to declare vari­ables as follows:
units batchl, batch2;
marks namel[50], name2[50];

batch' and batch2 are inclared as int variable and name I [501 and name2[50] are declared as 50 element floating point array variables. The main advantage of typedef is that we can create meaning­ful data type names for increasing the readability of the program.


Wednesday 20 June 2012

C Data Types


Data Types :
C language is rich in its data types. Storage representations and machine instructions to handle constants differ from machine to machine. The variety of data types available allow the programmer to select the type appropriate to the needs of the application as well as the machine.
C supports three classes of data types:

1. Primary (or fundamental) data types
2. Derived data types
3. User-defined data types

The primary data types and their extensions are discussed in this section. The user-defined data types are defined in the next section while the derived data types such as arrays, functions, structures and pointers are discussed as and when they are encountered.

All C compilers support five fundamental data types, namely integer (int), character (char), floating point (float), double-precision floating point (double) and void. Many of them also offer extended data types such as long int and long double. Various data types and the terminology used to describe them are given in Fig. . The range of the basic four types are given in Table . We discuss briefly each one of them in this section.


Integer Types
Integers are whole numbers with a range of values supported by a particular machine. Generally, integers occupy one word of storage, and since the word sizes of machines vary (typically, 16 or 32 bits) the size of an integer that can be stored depends on the computer. If we use a 16 bit word length, the size of the integer value is limited to the range —32768 to -1-32767 (that is, —215 to + 215 - 1). A signed integer uses one bit for sign and 15 bits for the magnitude of the number. Similarly, a 32 bit word length can store an integer ranging from -2,147,483,648 to 2,147,483,647.

In order to provide some control over the range of numbers and storage space. C has three classes of integer storage, namely short int, int, and long int, in both signed and unsigned forms. C defines these types so that they can be organized from the smallest to the largest, as shown in Fig. . For example, short int represents fairly small integer values and requires half the amount of storage as a regular int number uses. Unlike signed integers, unsigned integers use all the bits for the magnitude of the number and are always positive. Therefore, for a 16 bit machine, the range of unsigned integer numbers will be from 0 to 65,535.


We declare long and unsigned integers to increase the range of values. The use of qualifier signed on integers is optional because the default declaration assumes a signed number. Table shows all the allowed combinations of basic types and qualifiers and their size and range on a 16-bit machine.


Floating Point Types
Floating point (or real) numbers are stored in 32 bits (on all 16 bit and 32 bit machines), with 6 digits of precision. Floating point numbers are defined in C by the keyword float. When the accuracy provided by a float number is not sufficient, the type double can be used to define the number. A double data type number uses 64 bits giving a precision of 14 digits. These are known as douhle precision numbers. Remember that double type represents the same data type that float represents, but with a greater precision. To extend the precision further, we may use long double which uses 80 bits. The relationship among floating types is illustrated in Fig. .





Viod Types 

The void type has no values. This is usually used to specify the type of function. The type of a function is said to be void when it dose not return any value to the calling function. It can also play the role of a generic type, meaning that it can represent any of the other standard types .

Character Types 

A single character can be defined as a character (char) type data. Character are usually stored in 8 bits (one byte) of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have value between 0 and 255. signed chars have values from - 128 to 127





Thursday 14 June 2012

Variables

C Variables :
A variable is a data name that may be used to store a data value. Unlike constants that remain unchanged during the execution of a program. a variable may take different values at different times during execution. In Chapter 1, we used several variables. For instance, we used the variable amount in Sample Program 3 to store the value of money at the end of each year (after adding the interest earned during that year).
A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program. Some examples of such names are:

Average
height
Total
Counter_ 1
class_strength
As mentioned earlier, variable names may consist of letters, digits, and the underscore(_) charac-ter, subject to the following conditions:

1. They must begin with a letter. Some systems permit underscore as the first character.
2. Standard recognizes a length of 31 characters. However, length should not be normally more than eight characters, since only the first eight characters are treated as significant by many compilers.
3. Uppercase and lowercase are significant. That is, the varible Total is not the same as total or TOTAL.
4. It should not be a keyword.
5. White space is not allowed.
Some examples of valid variable names are:


John                       Value                       T_raise

Delhi                          xl                       ph_value

mark                         suml                        distance

Invalid examples include:

123 (area)
25th
Further examples of variable names and their con-ectness are given in Table .


If only the first eight characters are recognized by a compiler, then the two names
average_height 
average_weight


mean the same thing to the computer. Such names can be rewritten as
avg_height and avg_weight or
ht_average and wt_average


without changing their meanings.