Overview
This chapter will delve into two foundational aspects of C++: literals and operators. These are the building blocks upon which you construct meaningful instructions for your computer.
What are Literals?
Literals in C++ represent fixed values that are directly embedded within your source code. They are the raw data that your program operates on. C++ supports various types of literals, each tailored to represent different kinds of data. Here are some common types of literals in C++:
Integer Literals
Integer literals are used to represent whole numbers. They can be positive or negative and are usually written without decimal points. For example:
int number = 42; // This is an integer literal
Floating-Point Literals
Floating-point literals represent real numbers with a decimal point. They can be written in various forms, such as:
float pi = 3.14159; // This is a floating-point literal
Character and String Literals
Character literals represent individual characters enclosed in single quotes, while string literals represent sequences of characters enclosed in double quotes. For example:
char grade = 'A'; // This is a character literal
std::string greeting = "Hello, World!"; // This is a string literal
Boolean Literals
Boolean literals represent the truth values true
and false
. They often used in conditional statements and expressions:
bool isRaining = true; // This is a boolean literal
Null Pointer Literal
The nullptr
literal represents a null pointer and is often used to indicate that a pointer does not point to any valid memory location:
int *ptr = nullptr; // This is a null pointer literal
What are Operators?
Operators in C++ are symbols that performs operations on one or more operands. They enable you to manipulate data and control the flow of your program. C++ provides a wide range of operators, classified into several categories:
Arithmetic Operators
Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, division, and Modulus(%). Here are some examples:
int a = 5, b = 2;
int sum = a + b; // Addition
int sub = a - b; // Subtration
int product = a * b; // Multiplication
int quotient = a / b; // Division
int mod = a % b; // Returns the remainer of the division.
Relational Operators
Relational operators compare two values and return boolean result (true or false). They are often used in conditional statements. It includes Equality(==), Inequality (!=), Greater Than (>), Less than (<), Greater Than or Equal To (>=), Less than or Equal To (<=).
int x = 6, int y = 7;
bool isEqual = (x == y); // Equality Check
bool isLess = (x < y); // Less than
bool isGreater = (x > y); // Greater than
Logical Operators
Logical operators combine boolean values and produce boolean results. They are frequently used to make decisions in your program. These are Logical AND (&&), Logical OR (||), Logical NOT (!).
bool sunny = true, warm = true;
bool isPerfectDay = sunny && warm; // Logical AND
bool isRainyDay = sunny || !warm; // Logical OR
bool isNotSunny = !sunny; // Logical NOT
Assignment Operators
Assignment operators assign a value to a variable. The most basic one is the =
operator.
int age = 23; // Assignment operator (=)
// Compound Assignment Operator
int compound = 1;
compound += 10; // compound now becomes 11;
Conditional (Ternary) Operator
The conditional operator (? :
) is a shorthand way to write simple if-else statements:
int marks = 80;
std::string result = (marks >= 80) ? "Good Marks" : "Bad Marks";