Logo

dev-resources.site

for different kinds of informations.

How constant is const in C?

Published at
11/15/2023
Categories
programming
c
pointers
tips
Author
mellen
Categories
4 categories in total
programming
open
c
open
pointers
open
tips
open
Author
6 person written this
mellen
open
How constant is const in C?

Let's look at the simple statement

const int a = 2;
Enter fullscreen mode Exit fullscreen mode

If you come from a C# perspective, you expect the keyword const to mean that the value in a will never change. It's a way to help the compiler optimise the code.

In C, however, const is not exactly what you would expect.
For sure if you later wrote

a = 4;
Enter fullscreen mode Exit fullscreen mode

then the compiler will throw an error, something along the lines of:

error: assignment of read-only variable ‘a’

But the thing is, this is C, so if you want to shoot yourself in the foot, you can quite easily do it.

(In C# it is possible to change the value of a const with reflection, or unsafe code. It's a fair amount of code, but it can be done.)

This is all the work it takes:

#include <stdio.h>
int main()
{
  const int a = 2;
  printf("a: %d\n", a);
  int* x = &a;
  *x = 4;
  printf("a: %d\n", a);
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Just like that. Create a pointer, give it the address of your constant and set the value via the pointer.

Now, depending on the compiler, and your compiler options, you will likely get a warning, something like:

warning: initialization discards ‘const’ qualifier from pointer target type

So, it doesn't go by unnoticed, but it compiles and the code will run just fine.

This is part of the reason I use #define for constants. The other is it's just what I've always done.

I think, for C, it's helpful to think of const as only referring to the variable name, and not the data behind the variable. It's akin to a promise that the variable won't be used to change the value, and not that the value will never change.

pointers Article's
30 articles in total
Favicon
CS50 - Week 4
Favicon
Pointers in Modern C
Favicon
Go: Pointers & Memory Management
Favicon
Pointers and Arrays
Favicon
Understanding Memory Management, Pointers, and Function Pointers in C
Favicon
Pointers : what are they pointing to?
Favicon
Pointers in Go Programming Language
Favicon
An ode to Stacks and Pointers in Go!
Favicon
The Absolute Minimum Every Software Developer Must Know About Pointers
Favicon
Pointers in C programming.
Favicon
How constant is const in C?
Favicon
Pointers in C Programming: How Hard?🤔
Favicon
In C++, is a free function taking a struct as an argument faster than a class with a member function to do the same thing?
Favicon
How to assign an Address, contained inside a string, to a pointer in C
Favicon
Understanding pointers in Go
Favicon
Reversing a Linked List: A Hallway Analogy
Favicon
Exploring the Power of Pointers in C Programming
Favicon
Learn It Once: “Golang Pointers are Powerful”
Favicon
C++23: std::out_ptr and std::inout_ptr
Favicon
Pointers , Arrays & Strings in C
Favicon
C - Even more pointers, arrays and strings
Favicon
C - Pointers, arrays and strings
Favicon
A common pitfall when using sizeof() with pointers
Favicon
Pointers
Favicon
Storing references of pointers in containers in C++
Favicon
C++ basics: Pointers vs iterators
Favicon
I broke production 3 times in 3 weeks - Part II
Favicon
WTF*&n is Pointers in Golang (Bahasa Version)
Favicon
I broke production 3 times in 3 weeks - Part I
Favicon
Golang 101: İşaretçiler (Pointers)

Featured ones: