How to declare a constant string in C++

3 Answers

0 votes
#include <iostream>
#include <string>

int main() {
    const std::string str = "ABC"; 
    
    std::cout << str << std::endl;          
}



/*
run:

ABC

*/

 



answered Jun 19, 2025 by avibootz
0 votes
#include <iostream>

int main() {
    const char str[] = "ABC"; 
    
    std::cout << str << std::endl;          
}



/*
run:

ABC

*/

 



answered Jun 19, 2025 by avibootz
0 votes
#include <iostream>
#include <string>

int main() {
    constexpr char str[] = "ABC"; 
    
    std::cout << str << std::endl;      
}



/*
run:

ABC

*/

 



answered Jun 19, 2025 by avibootz

Related questions

1 answer 131 views
131 views asked May 8, 2021 by avibootz
1 answer 68 views
2 answers 91 views
1 answer 167 views
1 answer 187 views
187 views asked May 8, 2021 by avibootz
1 answer 214 views
1 answer 77 views
...