# What Are The Constants In C?

Written by Shuvhojit DebFebruary 2, 2022
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Shuvhojit Deb

Full Stack Developer

## Constants in C

A constant is a value or variable that can't be changed in the program, for example, 10, 20, 'a', 3.4, "c programming" etc.

## Use of the Constants in C

A constant is a named memory location in a program that holds a single value throughout the execution of that program. It can be of any data type- character, floating-point, string, double, integer, etc. There are various types of constants in C. It has two major categories- primary and secondary constants. Character constants, real constants, integer constants, etc., are types of primary constants. Structure, array, pointer, union, etc., are types of secondary constants.

## Types of Constants in C

### Integer Constants

It can be an octal integer or a decimal integer or even a hexadecimal integer. We specify a decimal integer value as a direct integer value, while we prefix the octal integer values with ‘o’. We also prefix the hexadecimal integer values with ‘0x’.

The integer constant used in a program can also be of an unsigned type or a long type. We suffix the unsigned constant value with ‘u’ and we suffix the long integer constant value with ‘l’. Also, we suffix the unsigned long integer constant value using ‘ul’.

Example

55 —> Decimal Integer Constant

0x5B —> Hexa Decimal Integer Constant

O23 —> Octal Integer Constant

68ul —> Unsigned Long Integer Constant

50l —> Long Integer Constant

30u —> Unsigned Integer Constant

### Floating Point Constants / Real Constants

This type of constant must contain both the parts- decimal as well as integers. Sometimes, the floating-point constant may also contain the exponential part. In such a case when the floating-point constant gets represented in an exponential form, its value must be suffixed using ‘E’ or ‘e’.

Example

We represent the floating-point value 3.14 as 3E-14 in its exponent form.

### Character Constants

The character constants are symbols that are enclosed in one single quotation. The maximum length of a character quotation is of one character only.

Example

‘B’

‘5’

‘+’

Some predefined character constants exist in the C programming language, known as escape sequences. Each escape sequence consists of a special functionality of its own, and each of these sequences gets prefixed with a ‘/’ symbol. We use these escape sequences in output functions known as ‘printf()’.

### String Constants

The string constants are a collection of various special symbols, digits, characters, and escape sequences that get enclosed in double quotations.

The definition of a string constant occurs in a single line:

We can define this with the use of constant multiple lines as well:

” This\

is\

The definition of the same string constant can also occur using white spaces:

All the three mentioned above define the very same string constant.

## Points to Remember for Constants in C

Generally, in the C programming language, constants are assigned with some value and this value is known as literal. Hence in the C programming language constants are also known as literals.

Example

`const int max =100;`

Here “100” is constant integer literal in the above constant expression.

• We should be careful while defining the constants, as just using the “const” keyword and declaring is as simple as declaring the variables in C. But declaration should be done as
`int a = 5;`

The below two declarations are not valid for defining the constants:

`const int a;`
`const int a;`
`= 5;`
• For the declaration of constants, it is always the best practice for writing constant names in capital letters or uppercase letters. This helps in understanding and readable program to others as well as for us.
• We cannot change the value of the constant once assigned using the “const” keyword or #define directive, because it will give an error if try to assign some other value to the constant that is declared.

## Conclusion

In this article, we have discussed the constants in the C programming language. The constants are divided into two categories primary and secondary constants in which primary constants consist of numerical constants like integer, decimal, real, etc, and character constants like character, string, backslash, etc. In secondary constants, we have an array, pointer, structure, union, etc. Constants in C are those whose values cannot be altered once declared and these constants are also known as literals.

C Language
C Constants
Programming
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
You must be Logged in to comment
Code Block
Shuvhojit Deb
Full Stack Developer
117 Blog Posts
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.