Constants in C refer to fixed values that do not
change during the execution of a program. There are four basic types of
constants in C. They are integer constants, floating point constants, character
constants and string constants. Integer and floating point constants represent
numbers. They are often referred to collectively as numeric _ type constants.
The following rules apply to all numeric type constants.
1. Commas and blank spaces cannot be included within
the constants.
2. The constant can be preceded by a minus (-) if
desired. The minus sign is an operator that changes the sign of a positive
constant though it can be thought of as a part of the constant itself.
3. The value of a constant cannot exceed specified
minimum and maximum bounds.
For each type of constant, these bounds will vary
from one C compiler to another.
Integer
Constants:
An integer constant is an integer-valued number. Thus
it consists of a sequence of digits. Integer (number) constants can be written
in three different number systems: decimal (base 10), octal (base 8) and
hexadecimal (base 16). Beginning programmers rarely however use anything other
than decimal integer constants. A decimal integer constant can consists of any
combination of digits taken from the set 0 through 9. If the constant contains
two or more digits, the first digit must be something other than 0. Several
valid decimal integer constants are shown below:
0 1
143 5280 12345 9999
The following decimal integer constants are written
incorrectly for reason stated:
12,452
illegal character (,)
36.0 Illegal
character (.)
10 20 30 illegal
character (blank space)
123_45_6743 illegal
character (-)
900 the
first digit cannot be zero.
An octal integer constant can consist of any
combination of digits taken from the set 0 through 7. However, the first digit
must be 0, in order to identify the constant as an octal number.
Valid octal number (integer) constants are shown
below:
0 01 0743 07777
The following octal integer constants are written
incorrectly for the reason stated:
743 does not begin with 0.
05280 illegal character (8)
777.777
illegal character (.)
A hexadecimal integer constant must begin with either
0x or 0X. It can then be
followed by any combination of digits taken from the sets 0 through 9 and a
through f (either upper or lower case). The letters a through f (or A through
F) represent the (decimal) quantities 10 through 15 respectively. Several valid
hexadecimal integer constants are shown below:
0x 0X1 0X7FFF 0xabcd
The following hexadecimal integer constants are
written incorrectly for the reason stated:
0X12.34 illegal
character (.)
013E38
doesn’t begin with 0x or
0X.
0x.4bff
illegal
character (.)
0XDEFG
illegal character(G)
Unsigned
and Long Integer Constants:
Unsigned integer constants may exceed the magnitude
of ordinary integer constants by approximately a factor of l, though they may
not be negative. An unsigned integer constant can be identified by appending
the letter ( ) (either upper or lowercase) to the end of the constant.
Long integer constants may exceed the magnitude of
ordinary integer constants, but require more memory within the computer. A long
integer constant can be identified by appending the letter L (either upper or
lowercase) to the end of the constant. An unsigned long integer may be
specified by appending the letters UL to the end of the constant. The letters
may be written in either upper or lowercase. However, the U must precede the L.
Several unsigned and long integer constants are shown
below:
Constant Number
System
50000 U decimal
(unsigned)
123456789 L
decimal (long)
123456789 UL decimal
(unsigned long)
0123456 L
octal (long)
0777777 U octal
(unsigned)
0X50000 U hexadecimal
(unsigned)
0XFFFFFUL
hexadecimal (unsigned long)
Floating
Point Constants:
A floating point constant is a base 10 number that
contains either a decimal point or an exponent or
both).
Several valid floating point constants
0. 1.
0.2
827.602
500. 0.000743 12.3
2E.8 0.006e.3 1.6667e+8
The following are not valid floating point constants
for the reason stated.
1
Either a decimal point or an exponent must be present.
1,000.0
Illegal character (,)
2E+10.2 The
exponent must be an integer (it cannot contain a decimal point)
3E 10 Illegal
character (blank space) in the exponent.
The quantity 3×105 can be represented in C by any of the following
floating point constants:
300000. 3e5 3e+5
3E5 3.0e+5
.3e5 0.3E6 30E4 30.E4 300e3
Character
Constants:
A character constant is a single character, enclosed
in apostrophes (i.e. single quotation marks).
Several character constants are shown below:
‘A’ ‘X’ ‘3’ ‘?’
‘ ´
Character constants have integer values that are
determined by the computer’s particular character set. Thus, the value of a
character constant may vary from one computer to another. The constants
themselves, however, are independent of the character set. This feature eliminates
the dependence of a C program on a particular character set. Most computers,
and virtually all personal computers make use of ASCII (i.e. American Standard
Code for Information Interchange) character set, in which each individual
character is numerically encoded with its own unique 7-bit combination (hence a
total of 27=128 difference
characters).
Several character constant and their corresponding
values, as defined by ASCII character set are shown below:
Constant Value
‘A’
65
‘X’
120
‘3’ 51
‘?’ 63
‘ ’ 32
These values will be the same for all computers that
utilize the ASCII character set.
String
Constants:
A string consists of any number of consecutive
characters (including none), enclosed in (double) quotation marks. Several
string constants are shown below:
“green”
“Washington , D.C. 2005” “207-32-345”
“$19.95” “THE
CORRECT ANSWER IS” “2*(I+3”
“ ”
“Line 1\n Line 2\n line 3”
“ ”
The string constants “Line 1\n Line 2\n Line 3”
extends over three lines, because of the new line characters that are embedded
within the string. Thus, the string would be displayed as
Line 1
Line 2
Line 3
The compiler automatically places a null character (\0)
at the end of every string constant, as the last character within the string
(before the closing double quotation mark). This character is not visible when
the string is displayed. A character constant (e.g. ‘A’) and the corresponding
single-character string constant (“A’) are not equivalent. A character constant
has an equivalent integer value, whereas a single character string constant
does not have an equivalent integer value and in fact, consists of two
characters – the specified character followed by the null character (\o).
Variables:
A variable is an identifier that is used to represent
some specified type of information within a designated portion of the program.
In its simplest form, a variable is an identifier that is used to represent a
single data item, i.e., a numerical quantity or a character constant. The data
item must be assigned to the variable at some point in the program. A given
variable can be assigned different data items at various places within the
program. Thus, the information represented by the variable can change during
the execution of the program. However, the data type associated with the
variable are not change. A C program contains the following lines:
int a,b,c ;
char d ;
- - - - -
- - - - -
a = 3 ;
b = 5 ;
c = a + b ;
d = ‘a’ ;
- - - - -
- - - - -
a = 4 ;
b = 2 ;
c = a – b ;
d= ‘w’
The first two lines are not type declaration which
state that a, b and c are integer variables, and that d is a character type.
Thus, a, b and c will each represent an integer-valued quantity, and d will
represent a single character. The type declaration will apply throughout the
program. The next four lines cause the following things to happen: the integer
quantity 3 is assigned to a, 5 is assigned to b and the quantity represented by
the sum a+b (.e. 8) is assigned to c. The character ‘a’ is assigned then
assigned to d. In the third line within this group, the values of the variables
a and b are accessed simply by writing the variables on the right-hand side of
the equal sign. The last four lines redefine the values assigned to the
variables as the integer quantity 4 is assigned to a, replacing the earlier
value, 3; then 2 is assigned to b, replacing the earlier value, 5; The
difference between a and b (i.e. 2) is assigned to c, replacing the earlier
value 8. Finally the character ‘w’ is assigned to d, replacing the earlier
character, ‘a’.
Comments
Post a Comment