2

I am currently reading about constants on the c++ tutorial from TutorialsPoint and, where it says:

Constants refer to fixed values that the program may not alter and they are called literals. (Source)

I do not really get this. If constants are called literals and literals are data represented directly in the code, how can constants be considered as literals? I mean variables preceded with the const keyword are constants, but they are not literals, so how can you say that constants are literals?

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • 7
    I would advise learning C++ from a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of random online tutorials. – NathanOliver Jun 26 '17 at 19:34
  • Well, that statement by itself is just false. – DeiDei Jun 26 '17 at 19:35
  • Yeah that quote is wrong or at the very least misleading. – juanchopanza Jun 26 '17 at 19:36
  • @NathanOliver Well, it was not really that random...Anyway, thanks for the constructive comment :) –  Jun 26 '17 at 19:37
  • _how can you say that constants are literals_ ... The compiler is allowed to turn any variable that it can prove to be constant into a literal (in the generated machine code), whether it is marked `const` or not. But you are not guaranteed that so I think the cited statement is incorrect. – zett42 Jun 26 '17 at 20:28
  • @zett That is a completely different meaning of "literal" – Lightness Races in Orbit Oct 23 '18 at 13:19
  • _literal_ is just notation(genarally, expression) for _constant_. – rosshjb Dec 01 '20 at 08:38

8 Answers8

14

Here:

 const int MEANING = 42;

the value MEANING is a constant, 42 is a literal. There is no real relationship between the two terms, as can be seen here:

 int n = 42;

where n is not a constant, but 42 is still a literal.

The major difference is that a constant may have an address in memory (if you write some code that needs such an address), whereas a literal never has an address.

  • So a constant can be both a variable and literal(since literals are constant)? If so, how would you describe a constant and a literal? Like you have given some examples but a definition of both that could help distinguish the terms. –  Jun 26 '17 at 19:47
  • @Thei I've edited my answer to hopefully make this clearer. –  Jun 26 '17 at 19:57
  • 6
    *"whereas a literal never has an address"* How about literal C-String ? – Jarod42 Jun 26 '17 at 20:19
  • The C-String literal resides in the code memory. This would be similar to the address you assign to a function pointer (The address of a function.. not any other typecasts!). All the const/non-const variables/objects can reside in different memories like bss/heap/stack etc depending on how the linker script has been written, the scope of the particular object, underlying architecture etc.. – RohitMat Sep 25 '18 at 05:39
8

I disagree with the claim "...There wasn't a thing called const in C originally so this was fine." const is actually one of the 32 C keywords. Google to see. With that rested, I think the man missed something at TP. To be fair to them at Tutorials Point, they had an article that explained the difference thus (full quote, verbatim): https://www.tutorialspoint.com/questions/category/Cplusplus

A literal is a value that is expressed as itself. For example, the number 25 or the string "Hello World" are both literals.

A constant is a data type that substitutes a literal. Constants are used when a specific, unchanging value is used various times during the program. For example, if you have a constant named PI that you'll be using at various places in your program to find the area, circumference, etc of a circle, this is a constant as you'll be reusing its value. But when you'll be declaring it as:

const float PI = 3.141;

The 3.141 is a literal that you're using. It doesn't have any memory address of its own and just sits in the source code.

Pls don't disparage those fellows doing what you call "random tutorials". Kids from poorer homes and less developed world can't afford your " good C++ textbooks " e.g. Scott Myers Effective C++ It is these online free tutorials they can have, and most of these tutorials do better explaining than the "good books". By any means read them guys. Get confused some then come over here to StackOveflow or Quora to have your confusion cleared. Happy coding guys.

Mallam Awal
  • 223
  • 3
  • 9
4

The author of the article is confused, and spreading that confusion to others (including you).

In C, literals are "constants". There wasn't a thing called const in C originally so this was fine.

C++ is a different language. In C++, literals are called "literals", and "constant" has a few meanings but generally is a const thing. The two concepts are different (although both kinds of things cannot be mutated after initial creation). We also have compile-time constants via constexpr which is yet another thing.

In general, read a good book rather than random tutorials written by randomers on the internet!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

While the first part of the statement makes sense

Constants refer to fixed values that the program may not alter

the continuation

and they are called literals

is not really true.

Neil has already explained the semantical difference between the literal and the constant in his answer. But I would also like to add that the values of constant variables in C++ are not necessarily known at compile time.

// x might be obtained at runtime
// for instance, from the user input
void print_square(int x)
{
    const int square = x*x;
    std::cout << square << '\n';
}

Literals are values that are known at compile-time, which allows the compiler to put them to a separate read-only address space in the resulting binaries. You can also enforce your variables to be known at compile-time by applying constexpr keyword (C++11).

constexpr int meaning = 42;

P.S. And I also do agree with a comment suggesting to use a good book instead of tutorialspoint.

Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25
2

If constants are called literals and literals are data represented directly in the code, how can constants be considered as literals?

The article from which you drew the quote is defining the word "constant" to be a synonym of "literal". The latter is the C++ standard's term for what it is describing. The former is what the C standard uses for the same concept.

I mean variables preceded with the const keyword are constants, but they are not literals, so how can you say that constants are literals?

And there you are providing an alternative definition for the term "constant", which, you are right, is inconsistent with the other. That's all. TP is using a different definition of the term than the one you are used to.

In truth, although the noun usage of "constant" appears in a couple of places in the C++ standard outside the defined term "null pointer constant", apparently with the meaning you propose here, I do not find an actual definition of that term, and especially not one matching yours. In truth, your definition is less plausible than TutorialPoint's, because an expression having const-qualified type can nevertheless designate an object that is modifiable (via a different expression).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Well, thank you. However, I am not really sure that I get what your point is... Did you somewhere explain those terms? Btw. thanks for the correction about my statement about constants :) –  Jun 26 '17 at 20:27
  • 1
    what about the definitions themselves needs explaining, @TheiMang? You seemed to understand the words just fine. As far as I can tell, your confusion arises purely from the fact that TP is defining the term differently that you expect, *but they can do that*. You must interpret their comments in terms of their definition. If you find the term used differently elsewhere, then you must interpret it there according to the definition in use there. I don't see what else there is to say. Language can be messy. – John Bollinger Jun 26 '17 at 20:44
  • Thanks man. But if constants mostly refer to actual variables and literals refer to values represented directly in the code, why are constant and literals always explained in the same chapter/section? –  Jun 27 '17 at 10:01
  • @TheiMang, I can't speak to why any particular author juxtaposes discussion of those two similar but distinct concepts, but why should they *not* be covered close together? Covering them together seems logical to me, given the overlap in their use cases and the comparative importance of drawing the distinction between them. – John Bollinger Jun 27 '17 at 13:56
0

Constant is simply a variable declared constant by keyword 'const' whose value after being declared shouldn't be altered during the course of the program (and if tried to alter it will result in an error).

On the other hand, literal is simply what is used and represented as it is typed in. For example, 25 when used in an expression (x+4*y+25) will be termed as literal.

Whenever we use String values or directly supply it in double quotes ("hello"), then that value in double quotes is called literal. For example, printf("This is literal");

And if you are assigning a string value to a variable then thereafter you will refer to the variable (which could be declared constant if desired) and not exclusively to the value you have stored in it, i.e., only till the point you are supplying a value (string type of any other type) to the variable, the value is referred to as literal value, after that the variable is talked about whenever referring that value.

Once again, the value(25) in expression : x+4*y+25 is literal. The value(4) in the term 4*y is also a literal (since it is exactly as we see it and is known to compiler beforehand).

--> The value(4) in the term 4*y is called numerical coefficient in algebraic terms and y is called literal coefficient in algebraic terms.

Hence, All the above explanation I have given is in computer terms only. The meaning of literals and constants in Algebra are somewhat different than used in computer terms.

Kushagra
  • 1
  • 1
0

"Constants refer to fixed values that the program may not alter and they are called literals. (Source)"

The sentence construction is weird which is leading to the confusion. Here, the the "they" that are referring to are the the fixed values and not constants. I would phrase it as "Constants refer to fixed values, that the program may not alter, called literals." which is less confusing I hope. Constants are variables that can't vary, whereas Literals are literally numbers/letters that indicate the value of a variable or constant.

0

I can explain it this way.

  • Basically, constants are variables whose value cannot change.

  • Literals are notations that represent fixed values. These values can be Strings numbers etc

  • Literals can be assigned to variables

Code :

var a = 10;
var name = "Simba";
const pi = 3.14;

Here a and name are variables. pi is a constant. ( Constants are those variables whose value doesn't change. )

Here 10, "Simba" and 3.14 are literals.