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 digits15 + 27
gets calculated to give us 42
"15 + 27"
is treated as literal text to displayThis 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.
Getting our computer set up so we can create and build C++ programs. Then, creating our very first application