Constants – C Programming language

In order to construct a statement in C programming we need to combine certain keywords, variables and constants which is understandable for the C Compiler. In this tutorial, we will have a look at the C Constants. Constants are values or entities that do not change their value in the course of the program, unlike variables that are susceptible to changes according to the programmers need.

Classification of Constants

The Constants in C can be classified as follows:

  • Primary Constants
  • Secondary Constants

These sections can further be divided into sub sections as shown in the figure below:

C Constants

Here are the constants that we use in our codes. Let us see how we construct and use these constants.

For now we will have a look at the Primary Constants.

Integer Constants

Integer Constants are the numbers that come under these categories

  1. Decimal (Base 10).
  2. Octal (Base 8)
  3. Hexadecimal (Base 16)

Have a look at the rules that guide the construction of Integer Constants:

  • It should not have any floating point.
  • Integer constants should have at least one digit.
  • It can have a positive or negative sign. In case it doesn’t have any sign it is taken as a positive integer.
  • No blank spaces or commas are allowed in an integer constant.

Another important note for integer constant is it’s range. For Visual Studio or gcc compilers, the range is -2147483648 to +2147483647. For Turbo C or Turbo C++ the range is -32768 to –32767. This range basically depends on the type of compiler i.e. whether it is a 32 bit compiler or a 64 bit.

Eg: 257, +563,-3245,-56

Real Constants

Real Constants also known as Floating point Constants are numbers that have a whole number followed by a decimal point followed by the fractional number. The real constants can be expressed in two forms:

  • Fractional Form
  • Exponential Form

Have a look at the rules that guide the construction of Real Constants in Fractional Form:

  • It should have a floating point.
  • It should have at least have one digit.
  • It can have a positive or negative sign. In case it doesn’t have any sign it is taken as positive.
  • No blank spaces or commas are allowed.

Eg: 546.236, +453.89, -22.564

In case the value we are dealing with is too small like 0.00003 or too big like 300000, we can use the exponential form to express these constants as 3.0e-5 and 3.0e5. The part before the ‘e’ is known as mantissa and the part after the ‘e’ is known as exponent. Have a look at the rules that guide the construction of Real Constants in Exponential Form:

  • The mantissa and the exponential part should have a letter ‘e’ between them.
  • The exponent should have at least one digit.
  • It can have a positive or negative sign. In case it doesn’t have any sign it is taken as positive.

The range of Real constants in Exponential form is -3.4e38 to 3.4e38.

Eg: 5.6e9, +2.0e-7, -5.6e+8, -7.8e-5.

Character Constants and String Constants

A Character Constant is a single alphabet, digit or special character enclosed within ‘single quotes’.

Have a look  at the rules that guide the construction of Character Constants :

  • Should be enclosed within single quotes.
  • The inverted commas should point to the left.
  • It can have only one character, digit or special character.

Eg: ‘A’, ‘f’, ‘4’

A string constant is a combination of alphabets, digits and special characters enclosed within double quotes”. Have a look at the rules that guide the construction of String Constants:

  • Should be enclosed within double quotes.
  • It can be of any length.
  • It ends with a null character assigned to it by the compiler.

Eg: “Welcome to Codingeek”, “Good to see you”, “8 apples”, “learning is fun”.

Thats all for now in constants. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.

Do not forget to share and Subscribe.

Happy coding!! :)