Constant (const) in C programming

Constant in C language: In this tutorial, we are going to learn how to define a constant in C language, what is const in C language with examples.
Submitted by IncludeHelp, on June 28, 2020

const is a keyword in C language, it is also known as type qualifier (which is to change the property of a variable). const is used to define a constant whose value may not be changed during the program execution. It prevents the accidental changes of the variable.

Consider these two definitions,

int value1 = 10;
const int value2 = 20;

Here, value1 is an integer variable, the value of value1 can be changed any time during the run time. But, the value2 is an integer constant, and the value of value2 cannot be changed during the run time.

Here, value1 is an integer variable while value2 is an integer constant.

Defining a constant

The const keyword is used to define a constant, include the keyword const before the data type or after the data type, both are valid.

const data_type constant_name = value;
or 
data_type const constant_name = value;

Does a constant occupy memory?

Yes, a constant always occupies memory at compile time. In the above statements, value2 will take sizeof(int) bytes (that maybe 2, 4, or 8 according to the system architecture) in the memory.

C program to demonstrate the example of constants

#include <stdio.h>

int main()
{
    const int a = 10; //integer constant
    const float b = 12.3f; // float constant
    const char c = 'X'; // character constant
    const char str[] = "Hello, world!"; // string constant

    // printing the values
    printf("a = %d\n", a);
    printf("b = %f\n", b);
    printf("c = %c\n", c);
    printf("str = %s\n", str);

    return 0;
}

Output:

a = 10
b = 12.300000
c = X
str = Hello, world!

What does happen, if we try to change the value of a constant?

If we try to change the value of a constant, the compiler produces an error that constant is read-only.

Let's consider this example,

#include <stdio.h>

int main()
{
    const int a = 10; //integer constant

    // printing the value
    printf("a = %d\n", a);

    // changing the values
    a = 20;
    // again, printing the value
    printf("a = %d\n", a);

    return 0;
}

Output:

main.c: In function ‘main’:
main.c:11:7: error: assignment of read-only variable ‘a’
     a = 20;
       ^

See the output – a is an integer constant here, and when we try to change it, the error is there.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.