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 215 views
215 views asked May 8, 2021 by avibootz
1 answer 159 views
2 answers 218 views
1 answer 268 views
1 answer 269 views
269 views asked May 8, 2021 by avibootz
1 answer 329 views
1 answer 204 views
...