CONSTANTS IN C Language

CONSTANTS-: Constants are like a variable that don’t change during execution. C refer to fixed value that do not change during the execution of a program. There are two two types of constants in c language :
1. Numeric constants
◆ Integer constants
◆ real constants
2. Character constants
◆ single character constants
◆ string constants

about contstant

● Integer constants
An Integer constants refers to a sequence of digits. There are three types of Integer,namely,decimal Integer,octal Integer and hexadecimal Integer.
Decimal in Integer consists of a set of digits 0 through 9 preceded by an optional – or + sign. Valid examples of decimal integer constant are:

123       -321       0      654321    +78

Embedded spaces, commas and non digit characters are not permitted between digits. For example,

15   750   20,000    $1000

These are illegal numbers.

Note: ANSI C supports unary plus which was not defined earlier.
An octal Integer constant consists of any combination of digits from the set 0 through 7 which a leading 0 . Some examples of octal Integer are:

037     0    0435     0551

A sequence of digits preceded bye 0x or 0 X is considered as hexadecimal Integer. They may also include alphabets A through F or a through F.The letter A through D represent the number 10 through 15. Following are the examples of valid hex Integer:

0X2     0x9f     0Xbcd     0x

We rarely use of octal and hexadecimal numbers in programming.

●Real constants
Integer numbers are inadequate to represent quantities that very continuously such as distances ,Heights ,temperatures, prices, and so on. These quantities are represented by numbers containing fractional parts like 17 .548. Such numbers are called real constants. Further examples of real constants are:

0.0083  -0.75  435.36  +247.0

● Single character constants
A single character constants contents a single character enclosed within a pair of single quote marks. Example of character constants are:

'5'      'X'      ';'    ' '

Note-:  that the character constant ‘5’ is not the same as the number 5.It means these are different to each other.  The last constant is a blank space.Character constants have Integer value known as ASCII values.

● String constants
A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank space. For example:

"Hello!"   "1987"   "WELL DONE"  "?..."  "5+3" 

remember that a character constant is not equivalent to the single character string constant.Further, a single character string constant does not have an equivalent integer value while a character constant has an integer value. Character strings are often used in programs to build meaningful programs.

A complete example of constant c Program  is given here

#include<stdio.h>  
#define val 20  
#define floatVal 8.5  
#define charVal 'Netnic'  
  
int main()  
{  
    printf("Integer Constant value: %d\n",val);  
    printf("Floating point Constant value: %.1f\n",floatVal);  
    printf("Character Constant value: %c\n",charVal);  
      
    return 0;  
}  

 

The output of the program is here

Integer constant value :20
Floating point constant value:8.5
Character constant value: Netnic

 

Leave a Comment