# Constants in C

A constant or sometimes called literal is an entity that doesn’t change, whereas a variable is an entity that can change. In this section we will discuss constants, variables are discussed in lesson Variables in C.

## Types of constants

In C there is two type of constants:

1. Primary constants.
2. Secondary constants.

Both types are further divided into more categories as shown in the figure.

Here we will discuss only Primary constants, Secondary constants will be discussed later. To create Primary constants you need to obey the following rules.

## Numeric Constants

Numeric constants are digits that may or may not have a decimal point. The rules for creating Numeric constants are:

1. Must have at least one digit.
2. No space, comma or any other special symbols are allowed.
3. It can be positive or negative if no sign precedes integer constant then it is assumed to be positive.

Numeric Constants are two types:

1. Integer constants
2. Floating point or real constants

### Integer constants

Integer constants do not have decimal point (`.`). They can be written using decimal numbers (base 10), octal numbers(base 8) and hexadecimal (base 16).

Decimal constants

It contains digits between `0` and `9`, but should not begin with a zero.

Octal constants

It contains digits between `0` and `7`, and must begin with a zero.

It contains digits from `0` to `9`, and letters from `a``f` (either in uppercase or lowercase), and must always start with `0x` or `0X`.

As already said letters can be uppercase or lowercase.

We can also mix decimal, octal and hexadecimal.

Always keep in mind that whatever number system we use to represent a number; they have no effect on how they are stored internally in memory (eventually every numbers and character are stored as streams of binary digits). Octal and hexadecimal are commonly used while writing low-level programs, so, in the beginning, you will not see them much.

By default, an integer constant is of type `int`. In a 32-bit system, `int` type occupies 4 bytes of memory. If the value of an integer is too large to fit into `int` then the type of the constant is promoted to `long int`. Again, if the value is still too large for the `long int`, then it will be promoted to `unsigned long int` eventually.

Rules for determining the type of octal and hexadecimal are slightly different and work as follows:

The compiler will go through the following type (top to bottom) until it finds a suitable type that can represent the constant.

You can also explicitly specify the type of an integer constant as `long int` by appending `l` or `L`:

Similarly, to specify the type of an integer constant as `unsigned int` append `u` or `U` after it:

We can also use `u` or `U` and `l` or `L` to specify the type of an integer constant as an `unsigned long int`.

`89ul` – is of type `unsigned long int`

### Floating point or real constants

Numeric constants having decimal point are called floating point or real constants.

Floating-point constants can be written in two forms:

1. Fractional forms
2. Exponential form or Scientific notation

Here are the rules for creating floating point constants in Fractional form:

1. Must have one at least one digit
2. Must have a decimal point
3. Can be positive or negative, the default is positive
4. No comma, blanks, or any other symbols are allowed

Here are some examples:

Exponential form is used in cases when a number is too small or too large. For example, `0.00000941` can be represented as `9.41e-6`. The part of the number before `e` is called mantissa i.e `9.41`, whereas, the part following `e` is called the exponent i.e `-6`.

Here are the rules for creating floating point constants in Exponential form:

1. Mantissa and exponent must be separated by `e` or `E`.
2. Mantissa can be positive or negative, default is positive.
3. Exponent must have at least one digit.
4. The exponent can be positive or negative default is positive

Some examples of floating point numbers in exponential form are:

By default, floating constants are of type `double`. We can explicitly mention the type of a floating-point constant as a `float` by appending `f` or `F` at the end of the constant. For example:

Similarly, We can explicitly mention the type of a floating-point constant as `long double` by appending `l` or `L` at the end.

## Character Constants

A Character constant is a single alphabet, digit or any special symbol enclosed using single quotes. Here are some examples:

Note: Character constant must be always enclosed in single quotes, so the following is wrong.

The maximum length of a character constant is 1 character long. That means you can not put more than one character inside single quotation marks like this:

As you may already know inside computer memory everything is stored as binary. It is easy to see how integer and floating point constants are stored as binary numbers. But how character constants are stored in the memory? It turns out that Every character constant has a unique integer associated with it. An ASCII table represents the decimal number used to represent every known character in the English Language.

Consider the following statement:

Here we are declaring a variable `ch` of type `char` and assigning a character constant `'a'` to it (don’t worry too much about the syntax right now, in the next chapter we will discuss variables in detail). Although it might appear that we are assigning `'a'` to variable `ch` but actually what’s gets assigned to `ch` is the ASCII value of `'a'` i.e 97. Here is the little secret – In C, characters are treated as small integers, so they actually have `int` type rather than the `char` type. A `char` or `signed char` has range -127 to 128, while an `unsigned char` has range 0-255.

## String Constant

String constants consist of zero or more characters enclosed in double quotes (`""`). At the end of the string, the null character i.e `'\0'` is automatically placed by the compiler.

Here are some examples of string constants:

Note: Although not officially part of Primary constants, string constants are given here for the sake of completeness. C has no data type for string, they are stored as an array of characters. We will learn more about strings in a great deal in its own chapter.

## Symbolic constants

If we want to use constant several times in a program, then we can provide it a name. For example: If there is a need to use constant `Π = 3.141592` at several places in the program, then we can give it a name and use that name instead of writing this long number. This constant is called Symbolic constant. It is generally defined at the beginning of the program. Here is the syntax of creating a Symbolic constant.

`#define` is a preprocessor directive just like `#include`, that’s why it doesn’t end with the semicolon (`;`).

`NAME` indicates the name we want to give to our constant and is generally written in uppercase.

`VALUE` can be numeric, character or string constant.

Let’s create a symbolic constant called `PI`.

When the program is compiled, the preprocessor replaces every occurrence of `PI` by its value. So that the statement:

becomes

Use of symbolic constant makes the program more maintainable and readable. For example: let’s say we want more accurate results, so we decided to update the value of `Π` from `3.141592` to `3.14159265359`, if we had not used symbolic constant then we would have to go through each occurrence of `3.141592` and update all of them. However, since we have already defined `PI` in a `#define` directive, we only need to make changes in the single place.