C++ Online Compiler
Example: String Declaration and Output
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// String Declaration and Output #include
#include
// Required for std::string using namespace std; int main() { // Step 1: Declare an empty string string greeting; // Step 2: Initialize a string using direct assignment string name = "Alice"; // Step 3: Initialize with another string string message = "Hello, " + name + "!"; // Step 4: Initialize using constructor (can specify length and char) string stars(10, '*'); // Creates a string with 10 asterisks // Step 5: Output strings cout << "Greeting: " << greeting << endl; // Will be empty cout << "Name: " << name << endl; cout << "Message: " << message << endl; cout << "Stars: " << stars << endl; return 0; }
Output
Clear
ADVERTISEMENTS