Monday, February 21, 2011

Constants in C

             Constants refer to the values that are not changed during execution of program in C .There are two types of constants in C language basically :
  • Numeric constants
  • Character constants
Numeric constants are further divided in two parts real constants and Integer constants
Integer constant
An integer constant means the set of digits from 0 to 9 generally. Hexadecimal and octal integer are less in use in C programming .
  • Hexadecimal refer to digit 0 to 9 and alphabets from A to F where A means 10 and F means 15
  • Octal refer to the digit from 0 to 7 .

Example
#include<stdio.h>
#include<conio.h>

void main()
{
   clrscr();
   printf("This are integer\n");
   printf("%d\n %d\n",3456,-234);
   getch();
}

Output
This are integer
3456
-234




Real Constants
Numbers which are not possible to be representation in Integer form as they change there value or they have fraction part in them for e.g pi = 3.14 or any fraction number such as 15.45 which are in fraction part are known as real constants .
A real number can be executed in exponential form also and the general syntax is as follows
number e exponent
example 0.35e4 , 45e-5
The letter e separates the number from the exponents.
Character constants are are of two types single character constant and string constant .

Single character constant
A single character constant contain one character in a pair of single quote marks. e.g such as 'S' or '45'.

#include<stdio.h>
#include<conio.h>

void main()
{
   clrscr();
   printf("This are single character constant \n");
   printf("%c\n %d\n",'c','F');
   getch();
}

Output
This are single character constant
c
F
String Constant

  • A string constant is a sequence of character enclosed in double quotes. This character can be letter,number,special characters and blank space .

  • Example is seen in above program “This are single character constant” , this is a string with letters and blank space .
  • Backslash characters are also allowed in the string constants as “\n” is example of this type in the above program

No comments:

Post a Comment