CONSTANTS (LITERAL)


CONSTANTS (LITERAL)

            Constants refer to the fixed values that do not change during the execution of a program.


Constants in C

Integer Constants

An integer constant is a sequence of digits. They are numbers that do not have a decimal point or an exponential part. There are 3 types of integers namely decimal integer, octal integers and hexadecimal integer.

a . Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas and non digit characters are not permitted between digits.
Example for valid decimal integer constants are
123, -31, 0, 562321, + 78
Some examples for invalid integer constants are
15 750   20,000   Rs. 1000

b/. Octal Integers constant begins with the digit 0 and contains any of the digits 0 through 7. Some examples of octal integers are
026, 0, 0347, 0676

c. Hexadecimal integer constant begins with the 0 digit followed by either an x or X, followed by any combination of the digits 0 through 9 and the letters a through f or A through F. The letters A (or a) through F (or f) represent the values 10 through 15, respectively.
Example of valid hexadecimal integers are
0X2, 0X8C, 0Xadf, 0x

Note:- We rarely use octal and hexadecimal numbers in programming.

Real Constants

Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082. A floating-point constant consists of the following:
  • An integral part
  • A decimal point
  • A fractional part
  • An exponent part
Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.
Example of real constants are
0.0026  -0.97   435.29   +487.0
we can omit digits before the decimal point, or digits after the decimal point.
421.      .78      -.34     +.4     are valid real numbers.
Real Numbers can also be represented by exponential notation. The general form is
mantissa e exponent
The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign. The letter e separating mantissa and exponent can be written either in lower case or upper case.
Examples are 0.71e4   12e-2   1.3e+3   7.5E9   -1.12E-7
7.5E9 = 7500000000              -1.12E-7 = -0.000000112

 

Single Character Constants

            A Single Character constant represent a single character which is enclosed in a pair of single quotation symbols.
Example for character constants are
 '5'     'x'     ';'      ' '    etc
All character constants have an equivalent integer value which are called ASCII Values.

To know the ASCII values of each character, just write below program.
#include<stdio.h>
#include<conio.h>
void main()
{
   int i;
   clrscr();
   for(i=0;i<128;i++)
     printf(“%c - %d”,i,i);
   getch();
}

String Constants

A string constant is a set of characters enclosed in double quotation marks. The characters in a string constant sequence may be a alphabet, number, special character and blank space. Example of string constants are
Krishna  “2004”  “2+4”  “!...?”
Note:- A single character constant ‘D’ is not equal to string constant “D”. Further, a single character string constant does not have an equivalent integer value where as single character constant has an integer value.



Backslash Character Constants [Escape Sequences]


Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character.



No comments:

Post a Comment