In C++, the string
data type is used to store text values, which consist of multiple characters. While char
holds a single character, string
allows working with complete words or sentences.
Declaring a String Variable
A string value must be enclosed in double quotes (" "
).
Example
C++
x
using namespace std;
int main() {
string bikeBrand = "Yamaha"; // Storing a text value
cout << "Bike Brand: " << bikeBrand;
return 0;
}
Using the <string> Library
The string
type is not built-in, but it behaves like one. To use string
, the <string>
library must be included in the code.
Example
C++
using namespace std;
int main() {
string jacketBrand = "Alpinestars"; // Creating a string variable
cout << "Rider's jacket brand: " << jacketBrand;
return 0;
}
Key Points About Strings in C++
- Strings are enclosed in double quotes (
" "
). - Unlike
char
, astring
stores multiple characters instead of just one. - The
<string>
library must be included to use string operations.