0% found this document useful (0 votes)
27 views9 pages

Constants in C

Constants in C are variables that cannot be modified once declared. The const keyword is used to define constants, which must be initialized at declaration. Constants can be of different data types like integer, character, float. They are immutable and cannot be reassigned after initialization.

Uploaded by

Mary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

Constants in C

Constants in C are variables that cannot be modified once declared. The const keyword is used to define constants, which must be initialized at declaration. Constants can be of different data types like integer, character, float. They are immutable and cannot be reassigned after initialization.

Uploaded by

Mary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

5/12/24, 9:50 PM Constants in C - GeeksforGeeks

Constants in C
Last Updated : 08 Jan, 2024

The constants in C are the read-only variables whose values cannot be modified once they are
declared in the C program. The type of constant can be an integer constant, a floating pointer
constant, a string constant, or a character constant. In C language, the const keyword is used to
define the constants.

In this article, we will discuss about the constants in C programming, ways to define constants in
C, types of constants in C, their properties and the difference between literals and constants.

What is a constant in C?
As the name suggests, a constant in C is a variable that cannot be modified once it is declared in
the program. We can not make any change in the value of the constant variables after they are
defined.

How to Define Constant in C?


We define a constant in C language using the const keyword. Also known as a const type
qualifier, the const keyword is placed at the start of the variable declaration to declare that
variable as a constant.

Syntax to Define Constant

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 1/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

const data_type var_name = value;

Example of Constants in C

// C program to illustrate constant variable definition


#include <stdio.h>

int main()
{

// defining integer constant using const keyword


const int int_const = 25;

// defining character constant using const keyword


const char char_const = 'A';

// defining float constant using const keyword


const float float_const = 15.66;

printf("Printing value of Integer Constant: %d\n",


int_const);
printf("Printing value of Character Constant: %c\n",
char_const);
printf("Printing value of Float Constant: %f",
float_const);

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 2/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

return 0;
}

Output

Printing value of Integer Constant: 25


Printing value of Character Constant: A
Printing value of Float Constant: 15.660000

One thing to note here is that we have to initialize the constant variables at declaration.
Otherwise, the variable will store some garbage value and we won’t be able to change it. The
following image describes examples of incorrect and correct variable definitions.

Types of Constants in C
The type of the constant is the same as the data type of the variables. Following is the list of the
types of constants

Integer Constant
Character Constant
Floating Point Constant
Double Precision Floating Point Constant

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 3/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

Array Constant
Structure Constant

We just have to add the const keyword at the start of the variable declaration.

Properties of Constant in C
The important properties of constant variables in C defined using the const keyword are as
follows:

1. Initialization with Declaration

We can only initialize the constant variable in C at the time of its declaration. Otherwise, it will
store the garbage value.

2. Immutability

The constant variables in c are immutable after its definition, i.e., they can be initialized only
once in the whole program. After that, we cannot modify the value stored inside that variable.

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 4/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

// C Program to demonstrate the behaviour of constant


// variable
#include <stdio.h>

int main()
{
// declaring a constant variable
const int var;
// initializing constant variable var after declaration
var = 20;

printf("Value of var: %d", var);


return 0;
}

Output

In function 'main':
10:9: error: assignment of read-only variable 'var'
10 | var = 20;
| ^

Difference Between Constants and Literals


The constant and literals are often confused as the same. But in C language, they are different
entities and have different semantics. The following table lists the differences between the
constants and literals in C:

Constant Literals

Constants are variables that cannot be modified Literals are the fixed values that define
once declared. themselves.

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 5/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

Constant Literals

Constants are defined by using the const


They themselves are the values that are
keyword in C. They store literal values in
assigned to the variables or constants.
themselves.

We cannot determine the address of a literal


We can determine the address of constants.
except string literal.

They are lvalues. They are rvalues.

Example: const int c = 20. Example: 24,15.5, ‘a’, “Geeks”, etc.

Defining Constant using #define Preprocessor


We can also define a constant in C using #define preprocessor. The constants defined using
#define are macros that behave like a constant. These constants are not handled by the compiler,
they are handled by the preprocessor and are replaced by their value before compilation.

#define const_name value

Example of Constant Macro

// C Program to define a constant using #define


#include <stdio.h>
#define pi 3.14

int main()
{

printf("The value of pi: %.2f", pi);


return 0;
}

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 6/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

Output

The value of pi: 3.14

Note: This method for defining constant is not preferred as it may introduce bugs and
make the code difficult to maintain.

FAQs on C Constants

Q1. Define C Constants.

Answer:

Constants in C are the immutable variables whose values cannot be modified once they
are declared in the C program.

Q2. What is the use of the const keyword?’

Answer:

The const keyword is the qualifier that is used to declare the constant variable in C
language.

Q3. Can we initialize the constant variable after the declaration?

Answer:

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 7/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

No, we cannot initialize the constant variable once it is declared.

Q4. What is the right way to declare the constant in C?

Answer:

The right way to declare a constant in C is to always initialize the constant variable when
we declare.

Q5. What is the difference between constant defined using const qualifier and #define?

Answer:

The following table list the differences between the constants defined using const qualifier
and #define in C:

Constants using const Constants using #define

They are the macros that are replaced by


They are the variables that are immutable
their value.

They are handled by the compiler. They are handled by the preprocessor.

Syntax: const type name = value; Syntax: #define name value

Q6. Is there any way to change the value of a constant variable in C?

Answer:

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 8/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks

Yes, we can take advantage of the loophole created by pointers to change the value of a
variable declared as a constant in C. The below C program demonstrates how to do it.

// C Program to change the value of constant variable


#include <stdio.h>

int main()
{

// defining an integer constant


const int var = 10;

printf("Initial Value of Constant: %d\n", var);

// defining a pointer to that const variable


int* ptr = &var;
// changing value
*ptr = 500;
printf("Final Value of Constant: %d", var);
return 0;
}

https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 9/9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy