Expressions and Literals in C++

Topic 9 of 13

Welcome to your C++ journey! These notes summarize the core concepts of C++ statements, expressions, and literals, providing a clear foundation for both absolute beginners and those looking to clarify their understanding of basic syntax. Our goal is not just to print things, but to understand the underlying depth of the code.

Expression

Definition:

An expression is a portion of code that, after being evaluated, provides some output.

Explanation:

More generally, an expression is defined as a combination of values (or literals) and operators, for instance 3*5-3. The compiler looks at an expression, determines the operation, evaluates it, and then gives you the result.

Literals (The Constants)

A literal is a specific value within C++ programming that remains constant and will not change throughout the entire execution of the program. In other words, a literal is a constant value.

Difference between literals constants and constant variables:

 Literals

 

  Constant Variable
A value expressed directly in the source code. Definition A variable whose value cannot be modified after it is declared and initialized.
They are the "raw data" itself. Nature They are named variables that hold a fixed value, making code more readable and maintainable.
Literals do not have an address in memory. Memory: Constants are actual variables that reside in memory, so you can take their address.
10, 3.14, 'A', "Hello", true. Examples const int MAX_USERS =100; or const double PI = 3.14159;

 

Types of Literals Explained

We can categorize literals based on how they are defined in the code:

String Literals

A string literal is a constant value made up of a number of characters.

Example Code (in cout)

Definition

Behaviour

"76"

A series of characters enclosed in double quotes. The compiler prints the contents exactly as written. This value will remain constant.

"123"

Even though it looks like a number, it is considered a string literal because it is inside double quotes. It is composed of multiple characters.

 

Key Point: Anything inside double quotes, even a single character like "1", is considered a String Literal.

Character Literals

A character literal is a single character.

Example Code (in cout) Definition Behaviour
'A' A single character enclosed in single quotes. This is a Character Literal because it is a single character. It will not change throughout the code.
'1' A single digit enclosed in single quotes. This is also considered a Character Literal.

Integer Literals

An integer literal is a whole number.

Example Code (in cout) Definition Behavior
123 A numeric value written without quotes. The compiler treats this as an integer value. It is an Integer Literal because it remains constant throughout the code.

Floating Point Literals

A floating point literal is a numeric value that includes a decimal point.

Example Code (in cout) Definition Behavior
5.6 A numeric value containing a decimal point. This is a Floating Point Literal. It remains constant.

 

Expressions in Action (Combining Literals and Operators)

While a single literal can act as an expression (since it evaluates to itself), expressions become powerful when combining multiple values and operators.

Expression Example Evaluation Behaviour Result
4 * 5 The compiler sees the multiplication operator (*) and evaluates the two Integer Literals (4 and 5). The answer will be the integer value 20.
5 * 5.6 An Integer Literal (5) is multiplied by a Floating Point Literal (5.6). The compiler can multiply these values, typically resulting in a Floating Point number.

The Role of ASCII Code in Character Arithmetic

When you perform arithmetic operations between an integer and a character, the character is temporarily converted into its corresponding ASCII integer value for the calculation.

Expression Example Evaluation Behavior Result

1 * 'A'

The integer 1 is multiplied by the character 'A'. The compiler takes the ASCII code of 'A' (e.g., 97) and performs the multiplication. If the ASCII code for 'A' is 97, the result would be 97.

'A' + 'B'

Both characters are treated as Character Literals. The compiler takes the ASCII code for 'A' and the ASCII code for 'B', and adds them together. The sum of their ASCII codes.

1 + '2'

The integer 1 is added to the character '2'. Since '2' is in single quotes, it is treated as a character literal, not an integer value. The compiler uses the ASCII code for the character '2' (which is 50). The result would be 51 (1 + 50).

 

Note: If you are unsure about ASCII codes, feel free to comment and request a detailed lecture on the topic.

 

Writing Efficient cout Statements

Instead of writing cout repeatedly for every output, you can chain multiple outputs together in a single line using the insertion operator (<<).

// Instead of this:

// cout << "76" << endl;

// cout << 123;

 

// You can do this:

cout << "76" << 123 << endl;

Remember to place the insertion operator (<<) before each new value or operator, including special commands like endl (to move to a new line).

Example of Expression and Literals:

# include <iostream>
using namespace std;
int main(){
    cout<<"7Scribes"<<end; //string literal
    cout<<"123 n"; //123 is a constant sequence of characters (string literals)
    cout<<123<<'n'; //123 is a integer value (integer literals)
    cout<<2+2.5<<endl; // integer + float value (The result will be a floating-point literal, but it will evaluate as expression) 
    cout<<'A'<<endl; //character literal
    cout<<'a'+2<<endl; // it will take the ASCII value of 'a' and add 2 to it. (Expression)
    cout<<4*5<<end; //Expression
    return 0;
}

Output:

7Scribes

123 // It appears as a string ("123").

123 //integer value

4.5

A

97

20

Try It Yourself!

To solidify your understanding, try these challenges on your system:

  1. Chaining Literals: Write a single cout statement that prints your name (String Literal), your age (Integer Literal), and your favorite letter (Character Literal). Use endl to ensure a clean display.
  2. ASCII Practice: What is the output of cout << 'B' + 'C';? (Hint: The compiler will add their respective ASCII codes).
  3. The Multiplication Table Challenge (Homework): Create a program to print the 2x table up to 2 x 5 = 10. The output must appear exactly as: 2 * 5 = 10. You must use only the concepts covered so far.

Output:

2 * 1 = 2

2 * 2 = 4

2 * 3 = 6

2 * 4 = 8

2 * 5 =10

Hint: Think about how to print the static text (2 * 5 = ) separately from the calculated result (10).

4. What will be the output of the following code?

# include <iostream>
using namespace std;
int main(){
    cout<<"Welcome";
    cout<<" Learn Literals and Expresions with 7Scribes"<<endl;
    cout<<2.3+'r';
    cout<<"n"<<'a'*'b'<<endl;
    cout<<'2'+'3';
    reutrn 0; 
}

Hint: Identify every types of literals, evaluate it, and don't forgot ASCII codes.

What's your biggest C++ fear—variables, loops, or complex data types? Let us know in the comments! Don't forget to practice these examples. Happy Coding😀