What is a Constant? What are the Different Types of Constants?

Constants in C refer to fixed values that do not change during the execution of a program. C supports several types of constants.

constants

Integer constants:

An integer constant refers to a sequence of digits. These are 3 types of integers (or) no. systems.

1. Decimal integer (base 10)
2. Octal integer (base 8)
3. Hexa – decimal integer (base 16)

 Decimal integer :

Integer of a set of digits 0 through 9. Preceded by a optional – or + sign
Ex: 123    – 180   0  + 78
Embedded spaces, commas, & non-digit characters are not permitted between digits
Ex : 15700                 20,000                        1000   are illegal

Octal integer

An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0.
Ex: 037                        0          0551

Hexa Decimal integer :

 A sequence of digits 0 to 9 preceded by ox or OX is considered as hexadecimal integer.

They may also includes alphabets A through F or a through F. The letter A through F represent the numbers 10 through 15.
Ex: OX2                      Ox9F              oXbcd
X The allowed range for a integer constant is – 32768 to 32767.

Real constants:

Integer numbers are not suitable for representing quantities that vary continuous by. Such as temp_. prices, heights etc.

These quantities are represented by no containing fractional parts like 28.250 such no are called real (floating point constant).

Ex:  75.789 ­® fractional part         
            ¯         
    Whole no

 If is also possible to omit digits before (or) after the decimal point
Ex: 215.           .95 are valid real numbers.

A real numbers may also be expressed in exponential (or scientific) notation.
Ex : 215.65 may be written as 2.1565e2 in exponential notation. E2 means multiply by 102 . the general form is

Syntax: maintissa e exponent
Ex: 3.42e -4
Mantissa  exponent

The mantissa is either a real numbers 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 & the exponent can be written in either lower case or upper case.

Exponential notation is useful for representing numbers that are either very large or very small in magnitude.

Ex 335.76 Þ mantissa 3.3576       exponent  3357.6e0.1        
                  = 3.3576 x 102                                  =3357.6 x 1/10                 
                  = 335.76                                           = 335.76

Single character constants:

A single character constant contains a single character enclosed within a pair of single quote mark.
Ex:   ‘5’     ‘x’       ‘;’
Note that the constant ‘5’ is not the save as no.5.

Characters constants have integer values known as ASCll values
Ex: printf (“ % d”, a); o/p 97 (Ascll of a)
       printf (“% C”, 97); o/p a

String constants:

A string constant is a sequence of characters enclosed in double quotes. The characters may be letter, numbers special characters  & blank spaces.
Ex: “ Hello!”     “1234”         “?.....!”        “5 +3”        “X”

Backslash character constants:

C supports some special backslash character constants that are used in o/p functions
Ex: ‘\n’ stands for new line character note that each one of them represents one character, although they consist of 2 characters these characters combinations are known as escape sequences.

            \t  – horizontal tab
            \b – back space
            \v – vertical tab
            \’  – simple quote
            \” – double quote

Related

C Language 6895164642792815574

Post a Comment

emo-but-icon

item