LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Find the Error · Question 1

Error: an auto variable without an initialiser Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    auto total;
    total = 42;
    std::cout << total << '\n';
    return 0;
}
  • A 42 is too large
  • B auto deduces the type from the initialiser, so a declaration without one has no type; write auto total = 42; or int total; Correct
  • C auto needs a header
  • D total must be const
Correct Answer: Option B

auto is compile-time deduction, not a dynamic type.

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