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.





Saturday, 26 May 2012

Keywords And Identifiers

C Programming Keywords Identifiers :
Every C word is classified as either a keyword or an identifier. All keywords have fixed meanings and these meanings cannot be changed. Keywords serve as basic building blocks for program state¬ments. The list of all keywords of ANSI C are listed in Table 2.3. All keywords must be written in lowercase. Some compilers may use additional keywords that must be identified from the C manual.


Identifiers refer to the names of variables, functions and arrays. These are user-defined names and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used. The underscore character is also permitted in identifiers. It is usually used as a link between two words in long identifiers.