Question 15
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <cstring>
#include <iostream>
#include <string>
int main() {
std::string a = "ab\0cd";
std::string b("ab\0cd", 5);
std::cout << a.size() << ' ' << b.size() << ' ' << std::strlen(b.c_str()) << '\n';
return 0;
}
- A 2 5 2 Correct
- B 5 5 5
- C 5 5 2
- D 2 2 2
Correct Answer: Option A
Constructing from a plain literal stops at the first '\0' (2 chars). With an explicit length all 5 bytes are kept – std::string can hold null characters – but strlen on c_str() still stops at the first one.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!