ArticleslgStudy

science

Integer literal

Integer literal is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Integer literal rather than just read about it. In short: In computer science, an integer literal is a kind of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the value 16, which is represented by 10 in hexadecimal (indicated by the 0x prefix).

Key takeaways

  • Integer literal belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Integer literal to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Integer literal from memory before moving on to harder problems.

Reference excerpt

In computer science, an integer literal is a kind of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the value 16, which is represented by 10 in hexadecimal (indicated by the 0x prefix). By contrast, in x = cos(0), the expression cos(0) evaluates to 1 (as the cosine of 0), but the value 1 is not literally included in the source code. More simply, in x = 2 + 2, the expression 2 + 2 evaluates to 4, but the value 4 is not literally included. Further, in x = "1" the "1" is a string literal, not an integer literal, because it is in quotes. The value of the string is 1, which happens to be an integer string, but this is semantic analysis of the string literal – at the syntactic level "1" is simply a string, no different from "foo".

Parsing Recognizing a string (sequence of characters in the source code) as an integer literal is part of the lexical analysis (lexing) phase, while evaluating the literal to its value is part of the semantic analysis phase. Within the lexer and phrase grammar, the token class is often denoted integer, with the lowercase indicating a lexical-level token class, as opposed to phrase-level production rule (such as ListOfIntegers). Once a string has been lexed (tokenized) as an integer literal, its value cannot be determined syntactically (it is just an integer), and evaluation of its value becomes a semantic question. Integer literals are generally lexed with regular expressions, as in Python.

Evaluation As with other literals, integer literals are generally evaluated at compile time, as part of the semantic analysis phase. In some cases this semantic analysis is done in the lexer, immediately on recognition of an integer literal, while in other cases this is deferred until the parsing stage, or until after the parse tree has been completely constructed. For example, on recognizing the string 0x10 the lexer could immediately evaluate this to 16 and store that (a token of type integer and value 16), or defer evaluation and instead record a token of type integer and value 0x10. Once literals have been evaluated, further semantic analysis in the form of constant folding is possible, meaning that literal expressions involving literal values can be evaluated at the compile phase. For example, in the statement x = 2 + 2 after the literals have been evaluated and the expression 2 + 2 has been parsed, it can then be evaluated to 4, though the value 4 does not itself appear as a literal.

Affixes Integer literals frequently have prefixes indicating base, and less frequently suffixes indicating type. For example, in C++ 0x10ULL indicates the value 16 (because hexadecimal) as an unsigned long long integer. Common prefixes include:

0x or 0X for hexadecimal (base 16). 0, 0o* or 0O* for octal (base 8). *: Added in C2Y. 0b or 0B for binary (base 2). Added in C23 & C++14. Common suffixes include:

l or L for long integer. ll or LL for long long integer. u or U for unsigned integer. These affixes are somewhat similar to sigils, though sigils attach to identifiers (names), not literals.

Digit separators In some languages, integer literals may contain digit separators to allow digit grouping into more legible forms. If this is available, it can usually be done for floating point literals as well. This is particularly useful for bit fields and makes it easier to see the size of large numbers (such as a million) at a glance by subitizing rather than counting digits. It is also useful for numbers that are typically grouped, such as credit card number or social security numbers. Very long numbers can be further grouped by doubling up separators. Typically decimal numbers (base-10) are grouped in three digit groups (representing one of 1000 possible values), binary numbers (base-2) in four digit groups (one nibble, representing one of 16 possible values), and hexadecimal numbers (base-16) in two digit groups (each digit is one nibble, so two digits are one byte, representing one of 256 possible values). Numbers from other systems (such as id numbers) are grouped following whatever convention is in use.

Examples In Ada, C# (from version 7.0), D, Eiffel, Go (from version 1.13), Haskell (from GHC version 8.6.1), Java (from version 7), Julia, Perl, Python (from version 3.6), Ruby, Rust and Swift, integer literals and float literals can be separated with an underscore (_). There can be some restrictions on placement; for example, in Java they cannot appear at the start or end of the literal, nor next to a decimal point. While the period, comma, and (thin) spaces are used in normal writing for digit separation, these conflict with their existing use in programming languages as radix point, list separator (and in C/C++, the comma operator), and token separator. Examples include:

In C++14 (2014) and the next version of C as of 2022, C23, the apostrophe character may be used to separate digits arbitrarily in numeric literals. The underscore was initially proposed, with an initial proposal in 1993, and again for C++11, following other languages. However, this caused conflict with user-defined literals, so the apostrophe was proposed instead, as an "upper comma" (which is used in some other contexts).

Notes

References

Worked examples

Example 1 — a first encounter with Integer literal

Start with the simplest possible case. Write down what Integer literal claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Integer literal before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Integer literal ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Integer literal

In research
Integer literal appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Integer literal in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Integer literal is common in secondary-school and first-year university syllabi. It links to neighbouring topics Integers, Source code, so understanding it makes those chapters shorter.
In everyday life
Look for Integer literal outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Integer literal” →

How to study Integer literal in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Integer literal means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Integer literal out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Integer literal in simple terms?

In computer science, an integer literal is a kind of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer li…

Why does Integer literal matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Integer literal?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Integer literal.

Tags

  • Integers
  • Source code

Keep exploring