Setting up a C++ Development Environment

Why Text Needs Quotes

Why do we need to use double quotes for text but not for numbers?

3D art showing a blacksmith character

In C++, we use quotes to help the compiler understand exactly what we mean. When we write a number like 42, the compiler knows this is a numeric value that can be used in calculations.

But when we write text, we need a way to tell the compiler "this is just text - don't try to interpret it as anything else". Let's look at an example:

#include <iostream>
using namespace std;

int main() {
  // number - no quotes needed
  cout << 42 << "\n";

  // text - needs quotes
  cout << "42" << "\n";

  // math expression - calculates to 42
  cout << 15 + 27 << "\n";

  // text - just prints exactly what we wrote
  cout << "15 + 27" << "\n";
}
42
42
42
15 + 27

Without quotes, the compiler treats things differently:

  • 42 is a number we can do math with
  • "42" is just text that happens to contain digits
  • 15 + 27 gets calculated to give us 42
  • "15 + 27" is treated as literal text to display

This becomes really important when we start doing calculations. For example:

#include <iostream>
using namespace std;

int main() {
  // works fine - does the math
  cout << 42 + 8 << "\n";

  // this will cause some weird output!
  cout << "42" + 8 << "\n"; 
}
50
Σ£≥╦

The quotes tell C++ "this is text" (technically called a string literal), while no quotes means "this is a value we can do math with". This distinction becomes even more important as we learn about variables and different types of data in future lessons.

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved