I am confused.

Here is the source: http://www.learncpp.com/cpp-tutorial/28-constants/

Literal constants

Literal constants are literal numbers inserted into the code. They are constants because you can’t change their values.

Why can't we change literal constant? I mean if we want to initialize something, let say

int x = 14;

And let say I am writing a program which involves passing x to a function, and return the new x after performing some arithmetic, then why did someone define that as a constant, when we are allowed to modify it?

Thank you.

8 Years
Discussion Span
Last Post by jwxie

Are you saying that you'd like a programmer to be able to write 14 and have it mean, say, 42? That's what changing a literal constant would mean.


Well, in general, if we say int x = 14, we make x an integer which has a value of 14.
But after googling, a lot of definitions agrees with the source, which I don't agree.

They are constants because you can’t change their values.

Programmers can change the code through coding (I am not suggesting that the programmer just removed the 14, and changed int x = 42).
So how are they constant?

Note that this definition seems to be in everyone's pocket.

This is how Microsoft describes a literal constant

A literal is a value that is expressed as itself rather than as a variable's value or the result of an expression, such as the number 3 or the string "Hello". A constant is a meaningful name that takes the place of a literal and retains this same value throughout the program, as opposed to a variable, whose value may change.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.