# constants vs. literals

This topic is 5236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I'm not sure of the difference between a constant and a literal. I tried searching for it on google but the sites I was led to oly confused me more. Can anyone give me a simple explenation of a constant and a literal?

##### Share on other sites
Well, a constant generally refers to a variable or identifier that stands as a placeholder for a particular value. Once set, a constant's value cannot be changed.

Example (C++):
const int myConst = 5;myConst = 20; //Compiler Error, can't set a constantprintf("I have %i fingers.", myConst); //prints "I have 5 fingers."

A literal, however, is where you have the value directly in code.

Example (C++):
return 9; //9 is a literal.

##### Share on other sites
A "literal" is a value given in plaintext in a program. Consider the following code:

int i = 7;const double PI = 3.14;printf("Hello, world!\n");

In this sample, there are three literals: 7, 3.14, and "Hello, world!". C++ has support for boolean literals (true and false), number literals, and string literals. These values are extracted directly from the code.

The term "constant" is considerably more nebulous, but generally refers to any value that does not change. Thus, PI would be considered a constant.

Ok. thanks guys.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 12