You are on page 1of 6

Constant

• A constant in C refer to a fixed value that doesn't change during the


execution of a program.

Constants

Numeric Character
Constants Constants

Integer Real Constant Single String


Constants Character Constant
Constant
Numeric Constants

Numeric constants, as the name itself indicates, are those which consist of
numerals. They are further divided into two types:

(a) Integer Constants (b) Real Constants

Integer Constant

• An integer constant is an integer valued number.

• It consists of sequence of digits.

• Integer constants are divided into three different types.

• They are decimal constants (base 10), Octal constants (base 8) and
hexadecimal constants (base 16).

Rules

• Constant must have at least one digit.

• (ii) Must not have decimal point.

• (iii) Constant can be preceded by minus (-) sign, to indicate negative


constants.

• (iv) Positive integer constants can be preceded by either positive sign or no


sign.
• (v) Commas and blank spaces can’t be included with in the constant.

• (vi) The value of constant must not exceed specified minimum and
maximum bound.

Decimal Integer

• A decimal integer constant consists of any combination of digits taken


from the set 0 to 9.

• If the constant consists of two or more digits, the first digit must be other
than 0.

• Valid example

 123 -321 0 65478 +78

• Invalid decimal integer constants

• 080 First digit cannot be zero

• 50.3 Illegal character ( . )

• 12,542 Illegal character ( , )

• 25 35 60 Illegal character ( Blank Space )

Octal Integer

• An octal integer constant consists of 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 integer constants

• 00 06 0753 0663

Invalid octal integer constants


• 543 Does not begin with zero

• 07865 Illegal character ( 8 )

• 06, 593 Illegal character ( , )

• 06.512 Illegal character ( .

Hexadecimal integer constant

• A hexadecimal integer is identified by ox or OX.

• Hexadecimal integer constant consists of digits taken from the set of 0 to 9


and also include alphabets from A to F (either upper or lower case).

• The letter a to f or A to F represent the decimal values from 10 to 15


respectively i.e.,

• a=10, b=11, c=12, d=13, e=14 and f=15.

Valid hexadecimal integer constants:

0x0 0x1a 0x1BEC 0x7FFF

Invalid hexadecimal integer constants

• 0x16.8b Illegal character ( . )

• 563c Does not begin with 0 or 0x

• 0x7bcg Illegal character ( g )

• 0x12.53 Illegal character ( . )

Real constant

• It represents numbers containing fractional parts (decimal notation). It is also


called as floating point constant.

• In real constant, numbers are shown in decimal notation, having a


whole number followed by decimal notation and fractional part.

• It must have at least one digit.


• It must have a decimal point which may be positive or negative.

• Use of blank space and comma is not allowed between real constants.

• Example: +194.143, -416.41

Exponential form

Another way of representing real constants is the exponential form.

• Exponential form is also known as scientific notation.

• When representing a number in its exponential form, the real constant is


represented in two parts : the mantissa and the exponent.

• The part of the number appearing before the alphabet e is the mantissa and
the part following e is the exponent.

• The general form of the number is thus :

• mantissa e exponent

• The mantissa can be positive or negative with the default sign of the
mantissa being positive.

• - The exponent should have atleast one digit which is a positive or negative
integer.

Some valid examples of exponential form of real constants are :

• 3.2e4 (e4 means multiply by 104)

• 4.8e-2 (e-2 means multiply by 10-2)

• -43e+3 (e3 means multiply by 103)

Invalid examples of exponential form

1.5e -2.5 exponent must be a integer


Single Character Constant

• A Single Character constant contains a single character enclosed within a


pair of single quote marks.

• It is a single alphabet or a digit or a special symbol enclosed in a single


quote.

• Maximum length of a character constant is 1.

• Example: 'T', '9', '$' , ‘a’

Character constant have integer values known as ASCII values

String Constant :

A string constant is a sequence of characters enclosed in double quotes.

• It is collection of characters enclosed in double quotes.

• It may contain letters, digits, special characters and blank space.

• Example: "Programming using C, Balagurusamy"

“Hello”

“WELL DONE”

Example : character constant ‘X’ is not equivalent to string constant “X”.

Invalid example for string constant

1. My name is abinesh  Characters are not enclosed in double quotation


mark
2. “My name is abinesh  Closing quotation mark is missing
Backslash Character Constant

C supports some special backslash character constants that are used in


output functions. A list of backslash character constants is given in the table.

Constants Name
\a Alert (bell)
\b Backspace
\f Form feed
\n New line
\r Carriage returns
\t Horizontal tab
\v Vertical tab
\’ Apostrophe
\” Double quote
\? Question mark
\\ Backslash
\0 Null character

You might also like