Pertanyaan

Tipe data manakah yang tepat untuk: menyimpan string seperti "Hello, World' dalam C++ char Sint float string

Solusi

Terverifikasi Ahli
4.6 (219 Suara)
Aakriti master ยท Tutor selama 5 tahun

Jawaban

The correct data type for storing a string like "Hello, World!" in C++ is **string**. Here's why:* **char:** A `char` variable can store a single character, like 'H', 'e', 'l', 'l', 'o', etc. It's not suitable for storing an entire string.* **int:** An `int` variable is used to store whole numbers (integers). It's not designed for text data.* **float:** A `float` variable is used to store decimal numbers. It's not designed for text data.* **string:** The `string` data type in C++ is specifically designed to store sequences of characters, making it the perfect choice for storing strings like "Hello, World!".**Example:**```c++#include #include int main() { std::string greeting = "Hello, World!"; std::cout << greeting << std::endl; return 0;}```This code will output:```Hello, World!```