Signup/Sign In
LAST UPDATED ON: SEPTEMBER 17, 2024

C Constant value Variables - const Keyword

  • In C programming, a constant value is a value that cannot be changed.

  • Using constants, you can assign a fixed value to a variable, so that the value of the variable is not changed by mistake during program execution.

  • To define a constant value variable you can use the const keyword.

Types of Constants

In C programming Literals are constants. Following are the different types of constants in C programming:

1. Integer Constants

Integer values are values that have no fractional part or decimal point. For example, 7, 0, -7, etc.

2. Floating-point Constants

Floating-point values are real numbers with fractional parts or decimal point. For example, 3.14, -2.65, etc.

3. Character Constants

These are single-character constants enclosed within single quotes, for example, 'A', 'a', '#', etc.

4. String Constants

These are set of characters, or text, enclosed within double quotes, for example, "Studytonight", "I love C", etc.

5. Enumeration Constants

An enumeration in C language is a set of named integer values. Enumeration or Enum is called user-defined constants.

Using const Keyword

  • The const keyword is used to create a variable as a constant.

  • When you create a variable using the const keyword, you have to assign it a value while creating the variable.

  • Once you have assigned a value to a const variable, the value stored in the variable cannot be changed.

  • You can use the const keyword with any type of variable.

Here is a simple example of using the const keyword,

const int totalmarks = 100;

In the above code, we have defined a variable totalmarks of type int and assigned it a value of 100. Because we have used the const keyword while defining the variable hence we cannot change the value of the totalmarks variable.

Change const Variable value

If you will try to change the value of the const variable in the program after it has been defined, then the compiler will give an error.

For example,

#include <stdio.h>

int main() {
   // initialize a constant variable 
   const int total_marks = 10;

   // try changing value
   total_marks = 80;
   
   return 0;
}


error: assignment of read-only variable 'total_marks'
total_marks = 80;
^

Run Code →

  • As you can see in the error message, the compiler says that the variable is a read-only variable, hence its value cannot be changed.

  • Because const variables are read-only variables, the compiler can place these variables into the read-only memory (ROM).

  • When we define a variable as a const, then nothing in that program can change the value of that variable.

  • Hence, if the value of the const variable changes, then you can say that something outside of the program changed the value, it can be the hardware device or any external event.

Code Examples

Here are a few code examples.

1. Creating a const variable,

const int DAY_IN_WEEK = 7;

 

2. Using a constant variable in expression,

const int DAY_IN_WEEK = 7;
int num_of_weeks = 10;
int total_duration = num_of_weeks * DAY_IN_WEEK;

 

3. Declaring a constant pointer,

int x = 7007;
const int* ptr = &x;

 

Frequently Asked Questions (FAQ)

Q1. Can I use constants to set array sizes?

A. Yes, you can use constants to define array sizes. You can define the array sizes at the start of the program, and then use it wherever required. In case you want to change the value, you will have to change only a single line of code.

Q2. Should I create a constant variable or use the value directly in the program?

A. You can use a value directly in the program but it is good practice to define a constant value at the beginning of the program by creating a constant variable and then using it in the program. Using a constant variable is a better approach also because it makes updating the constant value, if required, easy, because you just have to update the value of the variable.

Q3. What is the difference between a Constant and a Variable?

A constant has a fixed value that cannot be changed whereas a variable's value can be changed.