Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

What does the word "literal" mean when used in context such as literal strings and literal values? what is the difference between a literal value and value?

share|improve this question

10 Answers 10

A literal is "any notation for representing a value within source code" (wikipedia)

(Contrast this with identifiers, which refer to a value in memory.)

Examples:

  • "hey" (a string)
  • false (a boolean)
  • 3.14 (a real number)
  • [1,2,3] (a list of numbers)
  • (x) => x*x (a function)
  • /^1?$|^(11+?)\1+$/ (a regexp)

Some things that are not literals:

  • std::cout (an identifier)
  • foo = 0; (a statement)
  • 1+2 (an expression)
share|improve this answer
1  
upvote for the regex literal –  Joel Coehoorn Jan 27 '09 at 20:48
    
Sadly I don't know how it manages to determine primeness. I just copied it from somewhere because it looked cool –  Iraimbilanja Jan 27 '09 at 21:08
1  
I didn't even know that's what it did: I just thought showing a regex literal at all was a good idea. –  Joel Coehoorn Jan 27 '09 at 21:15

A literal is a value that has been hard-coded directly into your source.

For example:

string x = "This is a literal";
int y = 2; // so is 2, but not y
int z = y + 4; // y and z are not literals, but 4 is
int a = 1 + 2; // 1 + 2 is not a literal (it is an expression), but 1 and 2 considered separately are literals

Some literals can have a special syntax, so you know what type the literal is:

//The 'M' in 10000000M means this is a decimal value, rather than int or double.
var accountBalance = 10000000M; 

What sets them apart from variables or resources is that the compiler can treat them as constants or perform certain optimizations with code where they are used, because it's certain they won't change.

share|improve this answer
3  
Why was this downvoted? –  Ed S. Jan 27 '09 at 20:47
3  
I love the way you just explained it, instead of trying to show how much you know, thereby confusing newbies. We need more people like you. –  Mr. Smee Jun 17 '11 at 23:32

A literal is an assignment to an explicit value, such as

int i = 4;  // i is assigned the literal value of '4'
int j = i   // j is assigned the value of i.  Since i is a variable,  
            //it can change and is not a 'literal'

EDIT: As pointed out, assignment itself has nothing to do with the definition of a literal, I was using assignment in my example, but a literal can also be passed into a method, etc.

share|improve this answer
    
hehe, someone downvoted this??? –  Ed S. Jan 29 '09 at 17:00
    
yep: literals have nothing to do with assignment ;) –  Iraimbilanja Mar 6 '09 at 17:35
    
That's true, I should have clarified the fact that I was using assignment to demonstrate what a literal is. –  Ed S. Mar 6 '09 at 20:21

A literal is when you include the value in the source code (as opposed to referencing a variable or a constant). For example:

int result = a + 5; // a is a variable with a value, 5 is a literal

string name = "Jeff Atwood"; // name is a variable initialized
                             // with the string literal, "Jeff Atwood"

int[] array = new int[] {1, 2, 3}; // C# has array literals (this is actually three
                                   // int literals within an array literal)

If the literal represents some quantity, like a physical constant, it's better to give it a name instead of writing the same literal everywhere you need it. That way, when you are reading the source code, you know what the number means, which is usually more important than its value (which could change anyways).

const int maxUsers = 100;
const double gravitationalAcceleration = 9.8;

Generally, the only numeric literals I use (besides to initialize constants like above) are 0 or 1, and sometimes 2 if I'm skipping every other item in a loop. If the meaning of the number is more important than its actual value (it usually is), its better to name it.

share|improve this answer

A literal value is a value, but a value could also be stored in a variable. In the statement

string str = "string literal";

there's a string variable (str) and a string literal. After the statement is executed they both have the same value.

Be aware that in many languages the variable and the literal value don't necessarily even have to be the same type. For example:

int a = 1.0;

The literal value above is a floating point type. The value will be coerced by the compiler to fit into the int variable.

For another example, in the first line of C++ code above the type of a string literal isn't actually the library type string at all. To maintain backwards compatibility with C, string literals in C++ are char arrays.

share|improve this answer

Quick example:

int my_int_var = 723;

723 - This set of characters refers to a literal integer value.

my_int_var - This set of characters refers to a variable integer value.

share|improve this answer

A literal is when you put it in the code. So a string literal is

string s = "SomeText";

This is as opposed to building the string, or taking it in as a parameter.

share|improve this answer

In programming, a value written exactly as it's meant to be interpreted. In contrast, a variable is a name that can represent different values during the execution of the program. And a constant is a name that represents the same value throughout a program. But a literal is not a name -- it is the value itself.

A literal can be a number, a character, or a string. For example, in the expression,

x = 3

x is a variable, and 3 is a literal.

share|improve this answer

I have heard string literals used casually to refer to what the C# specification actually refers to as verbatim string literals. A regular string literal allows for the escaping of certain characters (prefixed by a ), like \t for a tab. A verbatim string literal is prepended by @ and processed verbatim, \ has no special meaning.

//regular
string regular = "\thello world";
//verbatim
string verbatim = @"C:\";
//the regular equivalent of this would be "C:\\"
share|improve this answer

generally when someone uses the word literal they mean the value is decipherable from the code (text) as shown in many of the examples in other posts, another common usage is for values that are converted to immediate values in assembly, these are values that are inserted directly into the machine instruction rather than requiring register loads

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.