Constants in C Programming

In this lesson, we will talk about constants in C programming. Constants sometimes referred to as literals, are entities that do not change, while variables are entities that can change. Constants are fixed values used in a program and their value remains the same during the entire execution of the program. We will focus on constants, their types and rules for creation in this lesson, and discuss variables in the next lesson.

Types of Constants

There are mainly two types of constants in C language.

  1. Primary Constants
  2. Secondary Constants

Both of these can be further divided into sub-types as mentioned below:

constants in C Language
Type of Constants in C Language

In this particular lesson, we will focus on primary constants. We will discuss secondary constants in detail later.

Numeric Constants

These are digits that may or may not have decimal points depending upon their sub-type as mentioned below.

  1. Integer Constants
  2. Floating Point or Real Constants

There are some rules for creating Numeric Constants[1].

  • They must have at least one digit.
  • Space, commas or any other special symbols are not allowed.
  • They can be both positive and negative numbers. If no sign is mentioned, then by default positive is assumed.

Integer Constants

Integer constants are digits that do not have a decimal point (.). They can be written using either decimal numbers, octal numbers or hexadecimal numbers.

Decimal Constants

They contain digits from 0 to 9 and can not begin with a zero.

Octal Constants

They contain digits from 0 to 7 and must begin with a zero.

Hexadecimal Constants

They contain digits from 0 to 9 and letters from a to f (both upper and lower cases are allowed). They must also begin with either 0x or 0X.

The C language also allows mixing all three sub-types, i.e. decimal, octal, and hexadecimal, into one variable or constant. Octal and hexadecimal types are commonly used while writing low-level programs.

Note: The type of number system we use has no effect on how the data gets stored internally in memory. Eventually every number or character is stored as streams of binary digits (zeros and ones).

By default, the integer constant is of type int and depending upon the operating system, takes up either 2 or 4 bytes of memory. If the value of an integer constant is too large to fit into int then the type constant is promoted to long int. If the value is still too large, then it is promoted to unsigned long int.

We can explicitly specify the type of an integer constant as long int in the code by appending l or L at the end of the constant.

We can do the same for unsigned int by appending u or U at the end of the constant.

Both of these can be combined for the type unsigned long int.

Floating Point or Real Constants

Numeric constants that have a decimal point are called floating-point or real constants. They can be written in two ways[2]:

  1. Fractional Forms
  2. Exponential or Scientific Forms
Fractional Forms

The rules for creating floating-point constants in fractional forms are mentioned below:

  • It must contain at least one digit.
  • It must have the decimal point.
  • It can be both positive or negative but by default positive is assumed.
  • Commas, blanks or any other special symbols are not allowed.
Exponential / Scientific Forms

This form is used in cases where the number is either too small or too large. For example, 0.000000314 can be represented as 3.14e-7. Here, the part of the number before e is called the mantissa (3.14) whereas, the part after e is called the exponent (-7).

Some rules for creating floating-point constants in exponential forms are mentioned below:

  • The mantissa and the exponent must be separated by e or E symbol.
  • The mantissa can be both positive and negative but by default positive is assumed.
  • The exponent must have at least one digit.
  • The exponent can also be both positive and negative but by default positive is assumed.

Floating-point constants are of type double by default. We can explicitly specify the type as float by appending f or F at the end of the constant. Similarly, we can specify the type as long double by appending l or L at the end of the floating-point constant.

Character Constants

Character constants are single alphabets, digits or special symbols enclosed inside single quotes (' ').

Note: Character constants must always be inclosed in single quotes.

Also, the length of a character constant is 1 byte (or 1 character). This means that we can not put more than one character inside the single quote as mentioned below:

As discussed above, everything in computers is stored in binary form. It is easy to understand how an integer or a floating-point constant is stored as binary. But the same is not true for character constants in C programming. This is possible because every character constant in C has a unique integer value associated with it. This is known as the ASCII value. The table below shows characters in the English language in their ASCII representation:

ASCII-Table
ASCII Table for constants in C

Backslash Character Constants

As discussed in the Keywords & Identifiers lesson, C supports some character constants having a backslash in front of it. These backslash characters have a specific meaning which is known to the compiler. They are also termed as “Escape Sequences”.

Escape SequenceNameUse
\nNewlineUsed to move the cursor to the beginning of a new line
\tTabUsed to move the cursor to the next Tab stop
\bBackspaceUsed to move the cursor back by one space on the current line
\rCarriage ReturnUsed to move the cursor to the beginning of the current line
\aBell(alert)Used to produce a beep sound from the system
\\BackslashUsed to print the Backslash (\) character
\0NullDenotes a null character
\'Single QuoteUsed to print the Single Quote (') character
\"Double QuoteUsed to print the Double Quote (") character

String Constants

String constants in C consist of zero or more characters enclosed in double quotes (" "). At the end of every string, the compiler places the null character ('\0').

Note: Strings are not officially part of primary constants in C. They are mentioned here for the sake of completeness only. Also, C has no data type for String. They are stored as arrays of character constants.

Symbolic Constants

Constants in C that are to be used multiple times in a program can be given a specific name. For example, if we want to use the constant Π = 3.141592 at multiple places in the program then instead of writing the long number everywhere, we can define it as a symbolic constant. They are generally defined at the beginning of a program using the syntax mentioned below:

Here, #define is a preprocessor directive just like #include. This is the reason that the statement does not end with the semicolon (;). NAME here indicates the name we want to give to our constant. VALUE is the actual numeric, character or string constant that we want to use multiple times in our program.

When the program is compiled, the preprocessor replaces every occurrence of PI with 3.141592 in the program. The use of symbolic constants makes the program much more flexible. Taking the example of the above case, if we now want to increase the accuracy of PI from 3.141592 to 3.14159265359. We can do so easily by just updating the symbolic constant as mentioned below instead of updating every occurrence of the constant in the program.

How to Declare Constants in C?

Apart from the symbolic constants mentioned above which are declared using the preprocessor directive #define, all other types of constants are declared using the keyword const. Some examples are mentioned in the code snippet below.

References

  1. C Programming 101 – Constants in C(OverIQ)
  2. C-Constants-Real Numbers(Tutorials Point)
  3. C Tutorials – Constants in C(W3schools)

Best C Programming Books

List of C Programming books curated for beginners as well as experienced programmers.

C Programming Books

Sharing is Caring

If you liked this post, then feel free to share it with your loved ones!

Leave a Reply