1.9 — Introduction to literals and operators

What are literals?

A literal is a fixed value that is inserted directly into the source code. Literals represent values that never change during program execution.

Integer literals

Integer literals are whole numbers written directly in your code:

#include <iostream>

int main() {
    std::cout << 5 << std::endl;        // 5 is an integer literal
    std::cout << -3 << std::endl;       // -3 is a negative integer literal
    std::cout << 0 << std::endl;        // 0 is an integer literal
    
    return 0;
}

Floating-point literals

Floating-point literals represent decimal numbers:

#include <iostream>

int main() {
    std::cout << 3.14159 << std::endl;  // 3.14159 is a floating-point literal
    std::cout << 0.5 << std::endl;      // 0.5 is a floating-point literal
    std::cout << -2.8 << std::endl;     // -2.8 is a negative floating-point literal
    
    return 0;
}

String literals

String literals are sequences of characters enclosed in double quotes:

#include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;  // "Hello World!" is a string literal
    std::cout << "C++ is fun" << std::endl;    // "C++ is fun" is a string literal
    
    return 0;
}

What are operators?

An operator is a symbol that tells the compiler to perform a specific operation. Operators work with values (called operands) to produce results.

Arithmetic operators

Arithmetic operators perform mathematical calculations:

#include <iostream>

int main() {
    std::cout << 2 + 3 << std::endl;    // + is addition operator, output: 5
    std::cout << 7 - 4 << std::endl;    // - is subtraction operator, output: 3
    std::cout << 3 * 4 << std::endl;    // * is multiplication operator, output: 12
    std::cout << 15 / 3 << std::endl;   // / is division operator, output: 5
    
    return 0;
}

Assignment operator

The assignment operator (=) assigns a value to a variable:

#include <iostream>

int main() {
    int x = 10;         // = assigns literal 10 to variable x
    int y = x;          // = assigns value of x to variable y
    
    std::cout << x << std::endl;    // output: 10
    std::cout << y << std::endl;    // output: 10
    
    return 0;
}

Combining literals, variables, and operators

You can combine literals, variables, and operators to create more complex expressions:

#include <iostream>

int main() {
    int a = 5;          // a gets literal value 5
    int b = 3;          // b gets literal value 3
    
    int sum = a + b;            // sum gets result of a + b (8)
    int product = a * 10;       // product gets result of a * 10 (50)
    int mixed = a + b * 2;      // mixed gets result of a + (b * 2) = 5 + 6 = 11
    
    std::cout << "sum: " << sum << std::endl;           // output: sum: 8
    std::cout << "product: " << product << std::endl;   // output: product: 50
    std::cout << "mixed: " << mixed << std::endl;       // output: mixed: 11
    
    return 0;
}

Output operator

The output operator (<<) sends values to std::cout:

#include <iostream>

int main() {
    int age = 25;
    
    std::cout << "I am ";           // string literal
    std::cout << age;               // variable value
    std::cout << " years old";      // string literal
    std::cout << std::endl;
    
    // Can chain multiple << operators:
    std::cout << "I am " << age << " years old" << std::endl;
    
    return 0;
}

Best practices

Use meaningful variable names with literals

// Good: Clear what the literal represents
int daysInWeek = 7;
double pi = 3.14159;
int maxScore = 100;

// Poor: Unclear what the literal means
int x = 7;
double y = 3.14159;
int z = 100;

Space around operators for readability

// Good: Easy to read
int result = a + b * c;
bool isEqual = (x == y);

// Poor: Hard to read
int result=a+b*c;
bool isEqual=(x==y);

Summary

  • Literals are fixed values written directly in code (like 5, 3.14, "hello")
  • Operators perform operations on values (like +, -, *, /, =)
  • Variables store values that can be used with operators
  • The output operator (<<) sends values to the console
  • Good formatting and meaningful names make code more readable

Understanding literals and operators is fundamental to writing C++ programs that can perform calculations and display results.