What does the word literal mean?

What does the word โ€œliteralโ€ mean when used in a context such as literal strings and literal meanings?

What is the difference between literal value and value?

+57
c # glossary
Jan 27 '09 at 20:38
source share
11 answers

A literal is "any entry for representing a value in the source code" ( wikipedia )

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

Examples:

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

Some things that are not literals:

  • std::cout (identifier)
  • foo = 0; (instruction)
  • 1+2 (expression)
+79
Jan 27 '09 at 20:46
source share

A literal is a value that has been hardcoded directly in 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 may have special syntax, so you know what type of literal:

 //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 using the code where they are used, because it will definitely not change.

+61
Jan 27 '09 at 20:42
source share

A literal is an explicit value assignment, for example

 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 indicated, the assignment itself has nothing to do with the definition of a literal, I used assignment in my example, but the literal can also be passed to a method, etc.

+14
Jan 27 '09 at 20:41
source share

In programming, a value written exactly as it should be interpreted. In contrast, a variable is a name that can represent different values โ€‹โ€‹during program execution. And a constant is a name that represents the same value in the entire program. But the literal is not a name - it is the meaning 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.

+11
Sep 16 '14 at 6:37
source share

A literal is when you include a value in the source code (as opposed to referencing a variable or 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 a literal represents a certain quantity as a physical constant, it is better to give it a name instead of writing the same literal wherever you need it. That way, when you read the source code, you know what a number means, which is usually more important than its value (which can change anyway).

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

As a rule, the only numeric literals that I use (other than initializing constants as above) are 0 or 1, and sometimes 2, if I skip every other element in the loop. If the value of a number is more important than its actual value (it usually is), it is better to name it.

+9
Jan 27 '09 at 20:58
source share

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

 string str = "string literal"; 

there is a string variable (str) and a string literal. After the statement is executed, both values โ€‹โ€‹have the same value.

Remember that in many languages, a variable and a literal value need not be the same. For example:

 int a = 1.0; 

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

In another example, in the first line of C ++ code above, the type of a string literal is not a type of the string library at all. To maintain backward compatibility with C, C ++ string literals are char arrays.

+6
Jan 27 '09 at 20:47
source share

Quick example:

int my_int_var = 723;

723 - This character set refers to an integer literal value.

my_int_var - This character set refers to the integer value of the variable .

+4
Jan 27 '09 at 20:43
source share

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

 string s = "SomeText"; 

This is in contrast to building a string or accepting it as a parameter.

+3
Jan 27 '09 at 20:42
source share

Usually, when someone uses the word literal, they mean that the value is decrypted from the code (text), as shown in many examples in other posts.

Another common use is a value that translates to immediate values โ€‹โ€‹in an assembly. These are values โ€‹โ€‹that are inserted directly into the command station, and do not require register downloads.

+2
Jan 27 '09 at 21:03
source share

I heard that string literals are used randomly to refer to the fact that the C # specification is actually referenced as shorthand literals. A valid string literal allows certain characters to be escaped (prefixed with a), for example, \ t for a tab. A literal string literal is added by @ and processed verbatim, \ does not really matter.

 //regular string regular = "\thello world"; //verbatim string verbatim = @"C:\"; //the regular equivalent of this would be "C:\\" 
+1
Jan 27 '09 at 20:54
source share

A literal is a "raw data representation."

+1
Dec 25 '16 at 11:36
source share



All Articles