Constants in C

 

Constants are fixed values, which do not change during the execution of a program. C supports several types of constants, which are as shown below:

 

c-constants

 

Integer Constants

 

An integer constant refers to a sequence of digits. Generally in programs, the number systems used are: decimal, octal and hexadecimal. Decimal integers consists of umbers from 0 to 9, preceded by an optional + or – sign.

 

Some valid examples of decimal integer constants are: 133, +45, -15, 0, 342332 etc. Embedded spaces and non-digit characters are cannot be used between digits.

 

An octal integer consists of numbers from 0 to 7. Octal integer numbers are always preceded by a zero. For example if we have to represent 35 in octal system, we must write it as 035 in the program. Some valid examples of octal integer constants are: 035, 02, 0435, +025 etc.

 

An hexadecimal integer consists of numbers from 0 to 9 and 10, 11, 12, 13, 14 and 15 are represented as A, B, C, D, E and F. Hexadecimal numbers are always preceded by 0x or 0X. Some valid examples of hexadecimal integer constants are: 0x54, 0X23, +0x1F, -0X56 etc.

 

Real Constants

 

Integer numbers are not sufficient to represent quantities that vary continuously, such as distances, height, prices etc. These quantities are represented by numbers containing fractional parts like 25.234. Such numbers are called as real or floating point constants. Some valid examples of real constants are: 0.067, -12.5, +4.67, .87, 121. Etc.

 

A real number may also be expressed in exponential (scientific) notation. For example, 45.2344 can be written 0.452344e2. The exponential notation is: mantissa e exponent. The mantissa is either a real number expressed in decimal notation or an integer with an optional + or – sign. The exponent is an integer number with an optional + or – sign. Some valid examples of real constants in exponential notation are: 0.34e2, 13e-2, -1.23e-1 etc.

 

Single Character Constants

 

A single character constant or character constant contains a single character enclosed in between single quotes. Some valid examples of character constants are: ‘f’, ‘A’, ‘/’, ’;’, ‘ ‘, ‘4’. The character constant ‘4’ is not equal to the number 4.

 

Every character constant will have an associated integer value known as ASCII code. Since each character constant has an associated integer value, we can perform arithmetic operations on them.

 

String Literals or String Constants

 

A sequence of characters that are enclosed between double quotes is known as a string literal or string constant. The characters in a string literal can be either letters, digits or special symbols or white spaces.

 

The string literal does not have an associated integer value like the character constants. Some valid examples of string constants are: “hai”, “hEllO”, “hi5”, “Wel come” etc.

 

Escape Sequences

 

C supports some special backslash character constants that are used in output functions like printf(). For example to move the cursor from the current line to next line, there is no standard way to achieve it. So, for such special cases, C provides escape sequences.

 

In this case \n is used to move the cursor to next new line. Even though the escape sequences consist of a backslash (\) and a character or symbol, it is treated as a single character. C supports the following escape sequences:

 

ConstantMeaning
\aAlert
\bBackspace
\fForm feed
\nNew line
\rCarriage Return
\tHorizontal tab
\vVertical tab
\’Single quote
\”Double quote
\?Question mark
\\Backslash
\0Null

 

 



  •  
  •  
  •  

Comments

Popular posts from this blog

C Tokens

INTRODUCTION TO C

Structure of a C Program