General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Datatypes

Literals

In computer programming, a literal is a notation for representing a fixed value in source code. Literals directly express specific data types' values within the code, such as numbers, characters, strings, or boolean values. Unlike variables and constants, which are named entities used to store data values that can be retrieved and manipulated, literals are the values themselves as written in the code.

Types of Literals:

  1. Integer Literals: Represent whole numbers without fractions, such as 42, -1, 0.

  2. Floating-Point Literals: Represent real numbers, which can include fractional parts, written with a decimal point or in exponential notation, such as 3.14, -0.01, 2.5e2.

  3. Character Literals: Represent single characters, typically enclosed in single quotes, such as 'a', '1', '@'.

  4. String Literals: Represent sequences of characters enclosed in double quotes (in most languages), such as "Hello, world!".

  5. Boolean Literals: Represent truth values, usually written as true and false.

  6. Null Literal: Represents the absence of a value or a null reference, usually written as null in many languages (or None in Python, nil in some other languages).

Characteristics of Literals:

  • Immutability: The value of a literal does not change; it is a fixed part of the code.

  • Direct Representation: Literals represent values directly in the code. When the program runs, these literals are interpreted as the values they represent.

  • Type Inference: In some languages, the type of a literal can be inferred from its notation. For example, 42 is clearly an integer, while 3.14 is a floating-point number.

Examples:

# Integer literal
num = 10

# Floating-point literal
pi = 3.14159

# Character literal (in languages like C or Java)
char c = 'A';

# String literal
greeting = "Hello, world!"

# Boolean literal
is_valid = true

# Null literal
reference = null

Usage:

Literals are used throughout programming to initialize variables, pass arguments to functions, and specify conditions in control structures. They are foundational to expressing basic values and structures directly in code, making them an indispensable part of programming syntax.

PreviousStructsNextData structures

Last updated 1 year ago

Was this helpful?