Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีกำหนด string constants ใน C++

เพื่อกำหนด string constant ใน C++ คุณต้อง include header <string> แล้วสร้าง string constant โดยใช้ class นี้และคีย์เวิร์ด const

ตัวอย่าง

#include <iostream>
#include <string>
int main() {
    const std::string MY_STRING = "Hello World!";
    std::cout << MY_STRING;
    return 0;
}

ผลลัพธ์

Hello World!

โปรดทราบว่าหากพยายามกำหนดค่าใหม่ให้ตัวแปรนี้ จะเกิด error