MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Constants

A constant is an identifier whose value can not be changed throughout the execution of a program whereas the variable value keeps on changing. In C there are four basic types of constants. They are:

  1. Integer constants
  2. Floating point constants
  3. Character constants
  4. String constants

Integer and Floating Point constants are numeric constants and represent numbers.

Rules to form Integer and Floating Point Constants

  1. No comma or blankspace is allowed in a constant.
  2. It can be preceded by – (minus) sign if desired.
  3. The value should lie within a minimum and maximum permissible range decided by the word size of the computer.


Integer Constants 

Further, these constant can be classified according to the base of the numbers as:

  1.   Decimal integer constants
    These consist of digits 0 through 9 and first digit should not be 0
    For example, 1,443,32767 are valid decimal integer constants
  2. Invalid Decimal integer Constants
    12 ,45                         not allowed
    36.0                            Illegal char
  3. Octal integer constants
    These consist of digits 0 through 7. The first digit must be zero in order to identify the constant as an octal number.
    Valid Octal INTEGER constants are: 0, 01, 0743, 0777   
    Invalid Octal integer constants are:
    743                                    does not begin with 0
    0438                               illegal character 8 
  4. Hexadecimal integer constants
    These constants begin with 0x or OX and are followed by combination of digits taken from hexadecimal digits 0 to 9, a to f or A to F.
    Valid Hexadecimal integer constants are: OX0, OX1, OXF77, Oxabcd.
    Invalid Hexadecimal integer constants are:
    OBEF                                 x is not included
    Ox.4bff                            illegal char (.)   

Maximum values these constants can have are as follows:

Integer Constant TypeMaximum Value
Decimal32767
Octal32767
Hexadecimal32767

 

Unsigned interger constants: Exceed the ordinary integer by magnitude of 2, they are not negative. A character U or u is prefixed to number to make it unsigned.

Long Integer constants: These are used to exceed the magnitude of ordinary integers and are appended by L.  

For example,
 50000U                                      decimal unsigned.
 1234567889L                          decimal long. 


Floating Point Constants

What is a base 10 number containing decimal point or an exponent. Examples of valid floating point numbers are:

0., 1.,  000.2,  5.61123456  etc

Examples of Invalid Floating Point numbers are:
1                                      decimal or exponent required.
1,00.0                      comma not allowed 

A Floating Point number taking the value of 5 x 104 can be represented as:
5000.                                       5e4
5e+4                                           5E4
5.0e+4                                     .5e5

The magnitude of floating point numbers range from 3.4E –38 to a maximum of 3.4E+38, through 0.0. They are taken as double precision numbers. Floating Point constants occupy 2 words = 8 bytes.


Character Constants

This constant is a single character enclosed in apostrophes ‘ ’ . For example, some of the character constants are shown below:

‘A’, ‘x’, ‘3’, ‘$’

‘\0’ is a null character having value zero.

Character constants have integer values associated depending on the character set adopted for the computer. ASCII character set is in use which uses 7-bit code with 27 = 128 different characters. The digits 0-9 are having ASCII value of 48-56 and ‘A’ have ASCII value from 65 and ‘a’ having value 97 are sequentially ordered. For example,

‘A’ has 65,                               blank has 32 


ESCAPE SEQUENCE

There are some non-printable characters that can be printed by preceding them with ‘\’ backslash character. Within character constants and string literals, you can write a variety of escape sequences. Each escape sequence determines the code value for a single character. You can use escape sequences to represent character codes: 

  1. you cannot otherwise write (such as \n)
  2. that can be difficult to read properly (such as \t)
  3. that might change value in different target character sets (such as \a) 
  4. that must not change in value among different target environments (such as \0)

The following is the list of the escape sequences:

CharacterEscape Sequence
""
''
??
\\
BEL\a
BS\b
FF\f
NL\n
CR\r
HT\t
VT\v

 

 

String Constants

It consists of sequence of characters enclosed within double quotes. For example,
“ red ”                               “ Blue Sea ”                                                  “ 41213*(I+3) ”.
 

Report an issue

Reporting: Constants (topic)

Related Posts