Javatpoint Logo
Javatpoint Logo

Constants in C

In programming languages like C, constants are essential because they give you a mechanism to store unchanging values that hold true throughout the course of the program. These numbers may be used for several things, such as creating mathematical constants or giving variables set values. We will discuss the idea of constants in C, their syntax, how to declare and use them and give illustrated examples along with their anticipated results in this blog article. By the end of this article, you'll have a firm grasp of constants and their importance in C programming.

A constant in C is a value that doesn't change as the program runs. Integers, floating-point numbers, characters, and strings are just a few of the several types of constants that may be employed. When a constant has a value, it cannot be changed, unlike variables. They may be utilized in various operations and computations and serve as the program's representation of fixed values.

Advantages of C Constants:

There are several advantages of C Constants. Some main advantages of C Constants are as follows:

  1. Programmers may use constants to provide names that have meaning to fixed numbers, which makes the code simpler to comprehend and update.
  2. Constants assist in avoiding the usage of magic numbers, which are hard-coded values, in the code. Instead, constants offer named representations of such values, enhancing the code's readability.
  3. Constants are reusable throughout the program, allowing for constant values in various locations and lowering the possibility of errors brought on by typos or inconsistent values.
  4. Calculations or processes inside the program can be optimized by using certain constants, such as mathematical or physical constants.
  5. 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.

There are different types of constants in C programming.

List of Constants in C

Decimal Constant10, 20, 450 etc.
Real or Floating-point Constant10.3, 20.2, 450.6 etc.
Octal Constant021, 033, 046 etc.
Hexadecimal Constant0x2a, 0x7b, 0xaa etc.
Character Constant'a', 'b', 'x' etc.
String Constant"c", "c program", "c in javatpoint" etc.

2 ways to define constant in C

There are two ways to define constant in C programming.

  1. const keyword
  2. #define preprocessor

1) C const keyword

The const keyword is used to define constant in C programming.

Now, the value of PI variable can't be changed.


The value of PI is: 3.140000

If you try to change the the value of PI, it will render compile time error.


Compile Time Error: Cannot modify a const object

2) C #define preprocessor

The #define preprocessor is also used to define constant. We will learn about #define preprocessor directive.

Types of constant:

There are different types of Constants in C. Some of them are as follows:

Decimal Constant

A whole number represented in base 10 is known as a decimal constant. It has digits that range from 0 to 9. Declaring a decimal constant has a simple syntax that just requires the value to be written.



The decimal constant is: 42

Real or Floating-Point Constant:

A fractional component or exponentiation of a number is represented by a real or floating-point constant. It can be expressed with a decimal point, the letter "E", or the symbol "e" in exponential or decimal notation.



The real constant is: 3.140000

Octal Constant:

A base 8 value is represented by an octal constant. It is prefixed with a '0' (zero) to show that it is an octal constant and has digits ranging from 0 to 7.



The octal constant is: 52

Hexadecimal Constant:

A base-16 value is represented by a hexadecimal constant. It uses letters A to F (or a to f) and numbers 0 to 9 to represent values from 10 to 15. It is prefixed with '0x' or '0X' to identify it as a hexadecimal constant.



The hexadecimal constant is: 2a

Character Constant

A character constant represents a single character that is enclosed in single quotes.



The character constant is: A

String Constant:

A series of characters wrapped in double quotes is represented by a string constant. It is a character array that ends with the null character \0.



The string constant is: Hello, World!

Rules for constructing constants:

The creation of constants must follow specified guidelines. These guidelines specify the format that constants of various kinds must follow in order for the compiler to accept them as legitimate. The guidelines for creating constants in C are as follows:

Integer Constants:

  1. The digits of an integer constant can range from 0 to 9.
  2. A decimal point should not be present in an integer constant.
  3. Positive or negative integer constants are also possible.
  4. You can add a suffix to a constant's name to define its type. 'U' or 'u' stands for unsigned, 'L' or 'l' for long, or 'LL' or 'll' for long long.

Floating-Point Constants:

  1. A decimal point, an exponent, or digits can all be found in floating-point constants.
  2. Either exponential notation or decimal notation can be used to write them.
  3. 'E' or 'e' can be used to denote an exponent.
  4. You can add a suffix to a constant's name to define its type. For instance, "F" or "f" stands for float and "L" or "l" for long double.

Octagonal Constants

  1. Base 8 is used for writing octal constants.
  2. They are made up of the numerals 0 through 7.
  3. A '0' (zero) should come before any octal constants.

Hexadecimal Constants:

  1. Constants in hexadecimal are expressed in base 16.
  2. To represent numbers from 10 to 15, they are made up of numerals 0 to 9 and letters A to F (or a to f).
  3. Prefixing a hexadecimal constant with '0x' or '0X' is appropriate.

Character Constants:

  1. Individual characters are represented as character constants when they are in single quotes.
  2. A single letter or an escape sequence, such as "n" for newline or "t" for tab, can be used as these characters.

String Constants:

  1. A series of characters surrounded in double quotes is represented by string constants.
  2. They are essentially character arrays that are closed with the null character "0".


As a result of their representation of fixed values that don't change during the course of the program, constants are crucial in C programming. By following the rules for making constants, programmers may create reliable and practical representations of data in their programs.

By giving descriptive names to fixed values, minimizing the usage of "magic numbers", and encouraging self-documenting code, constants improve the readability of code. By offering a central area to edit constant values, facilitating simple modifications, and lowering the possibility of mistakes, they also aid in the maintainability of the code.

As constants may be used and referenced several times throughout the program, using them in C programs also increases code reuse. When utilizing the same value again, it assures consistency and lessens the possibility of adding errors or mistakes.

Constants can also improve computations and processes, particularly when physical or mathematical constants are involved. Programmers can create code that is more effective and optimized by utilizing constants rather than hard-coding variables.

Next TopicLiterals in C

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA