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.



Monday 4 June 2012

Constants

C Constants :
Constants in C refer to fixed values that do not change during the execution of program. C suports several types of constants as illustrated .





An integer constant refers to a sequence of digits. There are three types of integers, namely, decimal integer, octal integer and hexadecimal integer.
Decimal integers consist of a set of digits, 0 through 9, preceded by an optional — or + sign. Valid examples of decimal integer constants are:


123 — 321 0 654321 +78


Embedded spaces, commas, and non-digit characters are not permitted between digits. For exam¬ple,


15 750 20,000 S1000


are illegal numbers. Note that  C supports unary plus which was not defined earlier.
An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading (). Some examples of octal integer are:


037 0 0435 0551

A sequence of digits preceded by Ox or OX is considered as hexadecimal integer. They may also include alphabets A through F or a through f. The letter A through F represent the numbers 10 through 15. Following are the examples of valid hex integers.

OX' Ox9F OXbcd Ox

We rarely use octal and hexadecimal numbers in programming.
The lamest integer value that can be stored is machine-dependent. It is 32767 on 16-bit machines and 2.147.483.647 on 32-bit machines. It is also possible to store larger integer constants on these machines by appending qualifiers such as U,L and LIL to the constants. For examples:

56789U or 56789u (unsigned integer)
987612347UL or 98761234111 (unsigned long integer)
9876543L or 98765431 (long integer)
The concept of unsigned and lone integers are discussed in detail in Section 2.7.

Example Representation of integer constants on a 16-bit computer.

The program in Fig. illustrates the use of integer constants on a 16-bit machine. The output in Fig. shows that the integer values larger than 32767 are not properly stored on a 16-bit machine. However. when they are qualified as long integer (by appending L), the values are correctly stored.


Real Constants :

Integer numbers are inadequate to represent quantities that very continuously, such as distances, height, temperatures, price and so on. These quantities are represented by numbers containing  frictional parts like I 7.548. Such numbers are called real (or floating poini) constants. Further ex¬amples of real constants are:
0.0083 -0.75 435.36 +247.0
These numbers are shown in decimal notation, having a whole number followed by a decimal point and the fractional part. It is possible to omit digits before the decimal point, or digits after the decimal point. That is,
215. .95 -.71 +.5
are all valid real numbers.
A real number may also be expressed in exponential (or scientific) notation. For example, the value 215.65 may be written as 2.1565e2 in exponential notation. e2 means multiply by 102. The general form is:
mantissa C exponent
The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign. The letter e separating the mantissa and the exponent can be written in either lowercase or uppercase. Since the exponent causes the decimal point to "float-, this notation is said to represent a real number in floating poin(lin-m. Examples of legal floating-point constants are:
0.65e4 I 2e-2 I .5e+5 3.18E3 -1.2E-1
Embedded white space is not allowed.
Exponential notation is useful for representing numbers that are either very large or very small in magnitude. For example, 7500000000 may be written as 7.5E9 or 75E8. Similarly, -0.000000368 is equivalent to -3.68E-7.

Floating-point constants are normally represented as double-precision quantities. However, the suffixes f or F may be used to force single-precision and I or L to extend double precision further. Some examples of valid and invalid numeric constants are given in Table .



Single Character Constants
A single character constant (or simply character constant) contains a single character enclosed within a pair of single quote marks. Example of character constants are:
'5'
Note that the character constant '5' is not the same as the number 5. The last constant is a blank
space.
Character constants have integer values known as ASCII values. For example. the statement
printf("%d", ' a ) ;
would print the number 97. the ASCII value of the letter a. Similarly, the statement

printf("%c", '97');
would output the letter 'a'. ASCII values for all characters are given in Appendix II.
Since each character constant represents an integer value, it is also possible to perform arithmetic operations on character constants.
String Constants
A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank space. Examples are:
"Hello!" "1987" "WELL DONE" "?...!" "5+3" "X"
Remember that a character constant (e.g., 'X') is not equivalent to the single character string constant (e.g., "X"). Further, a single character string constant does not have an equivalent integer value while a character constant has an integer value. Character strings are often used in programs to build meaningful programs.
Backslash Character Constants
C supports some special backslash character constants that are used in output functions. For example, the symbol `s\n' stands for newline character. A list of such backslash character constants is given in Table. Note that each one of them represents one character, although they consist of two characters. These characters combinations are known as escape sequences.