Constants and Variables In C Programming - Ocean of Programming

Monday 24 April 2017

Constants and Variables In C Programming

          Every programming has some constants and some variables. Likewise, C also has some constants and variables. The constant is an entity that never changes and the variable is an entity that may get change.
          The value of the constant in the program never gets change e.g 1,2.5 etc. One thing must remember that the constants and variables must be declared before their use.
There are two types of constants.
1. Primary Constant
2. Secondary Constants.
           The primary constant constants consist of  Real Constant, Integer Constants and Character Constant. On another hand, the secondary constant consists of Array, Pointer, Structure, Union, Enum etc. In this topic, we see only primary constants only. In a later post, I will tell about secondary constants. For declaring constants it has some rules that are mentioned below.
           The point to be noted is that in earlier C version the constants need to be declared after the main. In "ANSI C" the constants can be declared anywhere within the program. You do not need to declare the constants and variables after the main. You can

1. Real Constants

The real constant also known as Floating Point Constant. The Floating Point Constant can be divided into Fractional and Exponential Form.
The Rules For Constructing Real Constants
  • It must have at least one digit.
  • It may be positive or negative.
  • If a sign is not mentioned the default sign is a positive sign.
  • No, any blank space or comma are allowed.
The floating-point constant may be declared as e.g 21.84, +85.50,-69.79 etc.
Declaring Real Constant
float 2.5;
float x=3.5, y=9.5
The exponential form is used if the value of constant is too small or too large. The exponential form consists of two forms one is mantissa and exponent. The part appearing before e is called as a mantissa and the part after e is the exponent.

The Rules For Constructing Exponential Constants

  • Mantissa part may be positive or negative.
  • The exponent must have at least one digit which must have positive or negative. Default sign is positive.
  • The Mantissa and Exponent must be separated by a letter e which may be lower case or upper case. 
  • The range for exponential constant is -3.4e38 to +3.4e38.
  • The default sign of mantissa is positive.
The exponential constants may be declared as e.g -5.6e96, +3.6E52, -2.2e-8, 4.1E+5 etc.
Declaring Exponential Form 
float 5.6e96;
float a=6.3e5,b=9.6e4; 

 2. Integer Constants

              There are three types of integer constants based on a different number system. The number of systems is decimal, octal and hexadecimal. The integer constants may be of any one of the number systems.
1. Decimal Number System: 1,2,3,4,5,6,7,8,9     (Base 10)
2. Octal Number System: 0,1,2,3,4,5,6,7             (Base 8)
3.Binary Number System: 0 or 1                         (Base 2)

  Rules For Constructing Integer Constants

  • It don't have a decimal point.
  • It can be positive or negative.
  • Integer constant must have at least on the digit.
  • No commas and blanks are allowed within the integer constants.
  • The first digit of an integer cannot be zero.
Declaring Integer Constants
int a=56;
int x=5,y=3;
int x,y,z,total=0;

3. Character Constants

              A character constant is a single character that is enclosed within single quotes. Character constant has a unique integer value associated with it. This integer is the numeric value of the character in the machine's character code. The values assigned to the integer by using ASCII (American Standard Code for Information Exchange) code.
Some ASCII values are as follows.
A-Z           ASCII value(65-90)
a-z             ASCII value(97-122)
0-9            ASCII value(48-57)
;                ASCII value(59)

Rules for constructing Character constants 

  • A character constant is a single alphabet, a single digit or a special symbol enclosed within the single inverted commas.
  • Both inverted commas should point to the left.
  • The max length of a character constant can be 1 character.
Declaring Character Constants
char ch='y';
        Please feel free to comment if you find anything incorrect or you want to share more information about the topic discussed above.  

No comments:

Post a Comment