Chapter 2.2 - Describe Constant and Variables



CONSTANTS

Constant in C refer to fixed values that do not change during the execution of a program. C supports serval types of constants as illustrated in Fig 2.2.

          C language tutorials for beginners

          Chapter 2.1 - C character set and keyword

Describe Constant and Variables

Integer Constant

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 integers constant are:

123     -321     0     654321     +78

Integer Constant


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

15750     20,000     $1000

all illegal numbers.



An octal integer constant consist of any combination of the digit from the set 0 through 7, with a leading 0. Some examples of the octal integer are:

037     0     0435     0551

A sequence of digits preceded by 0x or 0X is considered as a hexadecimal integer. They may also include alphabet 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:

0X2     0x9F     0Xbcd     0x

We rarely use octal and hexadecimal numbers in programming.


The largest integer value that can be stored is machine-dependant. It is 32767 on 16-bit machines and 2,147,483,647 on 32-bit machines. It is also possible to store larger integer constant on these machines by appending qualifiers such as U, L and UL to the constants. Examples:

56789U             or 56789u            (unsigned integer)
98763234UL    or 98763234ul     (unsigned long integer)
987654L           or 9876543l         (long integer)

The concept of unsigned and long integers are discussed in detail in the Next Section.





WORKED-OUT PROBLEM 2.1

Representation of integer constant on a 16-bit computer.

        Download Turbo C++ Program Compiler               


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

uses of integer constant on a 16-bit machine




Real Constant

Integer numbers are inadequate to represent quantities that vary continuously, such as distances, heights, temperature, prices, and so on. These quantities are represented by numbers containing fractional parts like 17.548. Such numbers are called real (or floating point) constant. Further examples 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 points. That is,

215.     .95     -.71     +.5

are all valid 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 is:
mantissa e exponent 
The mantissa is either a real no expressed in decimal notation or an integer. An exponent is an integer number with an optional plus or minus sign. The letter e separating by 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 point form. Examples of legal floating-point constants are:


0.65e4     12e-2     1.5e+5     3.18E3     -1.2E-1

Compile Program in Android Mobile             

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.



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

Describe Constant






Single Character Constant

A single character constant (or simply character constant) contains a single character enclosed within a pair of single quote marks. Example of a character constant is as follows:

'5'      'X'     ';'     '  '

Note that the character constant '5' is not the same as number 5. The last constant is 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 also possible to perform arithmetic operations on character constants. They are discussed in another post of our site.




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"

                    Top 10 Useful Basic Commands for Linux user

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 integer value while a character constant has an integer value. Character strings are often used in programs to build meaningful programs. Manipulation of characters strings is considered in detail in our other post. 




Backslash Character Constant

C supports some special backslash character constants that are used in output functions.  For example, the symbol '\n' stands for the newline character. A list of such backslash character constants is given in Table 2.5. Note that each one of them represents one character, although they consist of two characters. These characters combinations are known as an escape sequence





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 variables amount in Worked Out Problem 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_strenght

As mentioned earlier, variables names may consist of letters, digit, and the underscore(_) character, subject to the following conditions. 

  1. They must begin with a letter. Some systems permit underscore as the first character.
  2. ANSI 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 variable Total is not the same as total or TOTAL.
  4. It should not be a keyword.
  5. White spaces are not allowed.
Some examples of valid variable names are:

John          Value         T-raise
   Delhi          x1             ph_value
  mark          sum1         distance

Invalid Examples Include:

123          (area)
        %        25th       

Chapter 2.2 - Describe Constant and Variables


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. 


Post a Comment

1 Comments

  1. Java For-each Loop with Simple Example - Simple Way to Learn
    Read Now

    ReplyDelete

Ask Me Everything About Programming.