Table of contents
1.
Introduction
2.
What is a constant in C?
3.
How to Define Constant in C?
3.1.
Using #define preprocessor
3.1.1.
Example
3.2.
C
3.3.
Using the const Keyword
3.3.1.
Example
3.4.
C
4.
Types of constants in C
4.1.
Integer Constants
4.2.
Floating-Point Constants
4.3.
Character Constants
4.4.
String Constants
5.
Rules for Constructing Constants in C
5.1.
Integer Constants
5.2.
String and Character Constants
6.
Properties of Constant in C
7.
Difference Between Constants and Literals
8.
Frequently Asked Questions
8.1.
Why C is a constant?
8.2.
Why do we use constants?
8.3.
In which c is a constant?
8.4.
What are the different types of constants?
9.
Conclusion
Last Updated: Dec 24, 2024
Easy

Constants in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the C programming language, constants are variables that cannot be changed once defined, meaning their value stays the same after initialization. Also known as literals, constants can be of any data type, such as int or char. There are also enumeration constants in C.

constant in C

Also Read : C Static Function

What is a constant in C?

In C, a constant is a fixed value that you define in your code, and this value cannot be altered once it's set. When you declare a constant, you specify a value, and that value remains unchanged throughout the execution of the program.

Constants can be of various types, such as integers, floating-point numbers, characters, or even strings. For example, if you declare an integer constant with a value of 10, that value will always be 10, and you can't change it later in the program.

In C, there are also symbolic constants, which are typically defined using the #define preprocessor directive. These constants are also unchangeable, and they can represent various data types.

How to Define Constant in C?

There are two ways in which we can declare and define a constant in C, which are as follows:

Using #define preprocessor

‘#define’ is a preprocessor directive which is used to create constants in the C language. When creating constants using ‘#define’, we must define them in the beginning of the program as all the preprocessor directives are written before the global declaration.

Syntax

#define identifierName value

 

Example

  • C

C

#include <stdio.h>
#define l 20
#define b  3
#define change_line '\n'

int main() {
int area; 

area = l * b;
printf("Area of rectangle is : %d", area);
printf("%c", change_line);

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Area of rectangle is : 60

Using the const Keyword

The keyword ‘const’ is used to create constants of any data type in a program. The keyword ‘const’ is used as a prefix when declaring a constant. The method used for declaring constants using the keyword ‘const’ is the same as defining a variable, with the only difference being we have to use ‘const’ as a prefix.

Syntax 

const dataType variableName = value;

Example

  • C

C

#include <stdio.h>

int main()
{
 // int constant
 const int intVal = 63;

 // char constant
 const char charVal = 'X';

 // string constant
 const char stringVal[10] = "CODING";

 printf("The value of Integer constant:%d \n", intVal);
 printf("The value of Character constant: %c\n", charVal);
 printf("The value of String constant: %s\n", stringVal);

 return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

The value of Integer constant:63 
The value of Character constant: X
The value of String constant: CODING

Types of constants in C

Integer Constants

These are the constants which can be an octal integer, decimal integer, or hexadecimal. A prefix is used to specify the base, like ‘O’ is used as a prefix with octal integer, ‘0x’ is used as a prefix with a hexadecimal integer. While we specify decimal integer value directly as an integer value. Integer constants can be of the unsigned or long type also. ‘u’ is used as a suffix with unsigned constant value whereas ‘l’ is used as a suffix with long integer constant value and ‘ul’ is used as a suffix with unsigned long integer constant.

E.g

74         - decimal 
0321       - octal 
0x3a       - hexadecimal 
45         - int 
42u        - unsigned int 
31l         - long
65ul       - unsigned long

Floating-Point Constants

These constants are made up of integer part, decimal part, fractional part, and exponent part. When represented using the decimal form: the decimal point, the exponent, or both must be included. Whereas when represented using exponent form: the integer part, fractional part, or both must be included. The signed exponent is represented by e or E.

E.g., floating-point value 3.14 is represented as 3E-14 in exponent form.

Also read - Bit stuffing program in c

Character Constants

These constants are enclosed within single quotes. It can contain only one character. In the C language, there are certain predefined character constants known as escape sequences, where each sequence has its own special functionality. ‘\’ is used as a prefix with each of these sequences. Escape sequences are used in output functions such as printf().

E.g., ‘\t’ for horizontal tab, ‘\n’ for new line.

String Constants

These are the constants which are a collection of various special symbols, digits, characters, and escape sequences. String constants are enclosed within double-quotes. A long line can be divided into multiple lines using string constants and are separated using white spaces.

E.g.,

"Hello World"
"Hello \
World"
"Hello " "World"

 

All of the above-mentioned forms yield the same identical string.

You can also read about the jump statement and Short int in C Programming

Rules for Constructing Constants in C

Integer Constants

  • The constant must contain at least one digit.
  • The constant cannot have a decimal point, blanks, or commas.
  • The constant can be both positive or negative. By default, if there is no sign, the integer constant is assumed to be positive. 

String and Character Constants

  • A character constant can be either a single alphabet, a single digit, or a single special symbol. 
  • A character constant must be enclosed within single quotes.
  • The maximum length of a character constant is one single character.
  • The string constant must be enclosed within double-quotes.

Properties of Constant in C

In C programming, constants have specific properties that make them unique. Here are the key properties of constants in C:

  • Fixed Value: Once you assign a value to a constant, it cannot be changed during the program's execution. The value remains constant, ensuring that it cannot be accidentally modified.
     
  • Type-Specific: Constants can be of various data types, such as integer, floating-point, character, or string. The data type of a constant determines the kind of value it can hold.
     
  • Readability: Constants improve code readability by replacing hard-coded values with meaningful names. This makes the code easier to understand and maintain.
     
  • Memory Allocation: Constants are stored in memory, just like variables. However, their memory location is typically marked as read-only, preventing any modification during the program's runtime.
     
  • Scope and Lifetime: The scope of a constant (whether it's accessible throughout the program or just within a certain block of code) depends on where it's defined. Its lifetime is the same as the program or the block of code in which it’s defined.
     

For practice, you can implement it on a C compiler for better understanding.

Difference Between Constants and Literals

ConstantsLiterals
Constants are named values that cannot be modified once they are defined.Literals are fixed values that are directly used in the program without any name.
Constants are declared using the const keyword followed by a data type and a name.Literals do not require any declaration or name; they are directly used in the program.
Constants can be of any data type, such as integer, floating-point, character, or string.Literals can also be of any data type, such as integer, floating-point, character, or string.
Constants are stored in memory, and their values are accessed using their names.Literals are not stored separately in memory; they are directly embedded in the program code.
Constants improve code readability and maintainability by providing meaningful names to values.Literals are used for immediate values and do not contribute to code readability or maintainability.
Constants can be defined once and used multiple times throughout the program.Literals are used directly in the program and do not have a separate definition.
Constants follow naming conventions, typically using uppercase letters and underscores.Literals do not have any naming conventions since they are not named entities.
For example : const int MAX_SIZE = 100;, const double PI = 3.14159;For example: 10, 3.14, 'A', "Hello"

Frequently Asked Questions

Why C is a constant?

In programming, C is often used as a symbolic constant, meaning its value is fixed and does not change during program execution.

Why do we use constants?

We use constants to make code more readable, prevent accidental modification, and ensure that certain values remain unchanged throughout the program.

In which c is a constant?

In programming, a constant in C is a value or variable defined with const or #define that cannot be altered during the program's execution.

What are the different types of constants?

Different types of constants include integer constants, floating-point constants, character constants, and string constants in programming.

Conclusion

In this article, we discussed constants in the C language. We began by understanding what constants are and their importance in programming. We then looked into the various types of constants available in C, like integer, floating-point, character, and string constants. Lastly, we learned how to declare constants using the `const` keyword, which ensures their values remain unchanged throughout the program's execution.

Live masterclass