LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 18

The "s" string literal and concatenation Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 18 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>

using namespace std::string_literals;

int main() {
    auto a = "Hi"s + " there";
    auto b = "Hi" " there";
    std::cout << a << '|' << b << '|' << a.size() << '|' << sizeof(b) << '\n';
    return 0;
}
  • A Hi there|Hi there|8|8 Correct
  • B Hi there|Hi there|8|9
  • C Hi there|Hi|8|8
  • D Hi there|Hi there|9|9
Correct Answer: Option A

"Hi"s is a std::string, so + works. Adjacent literals are joined by the compiler; but b is declared with auto from an array, so it decays to const char*, and sizeof(b) is the pointer size, 8.

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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links