ProgrammingModerate

n <= 10^5 values, each up to 10^9, must be summed. Which declaration is correct for the total?

  1. A. long long total = 0;
  2. B. int total = 0;
  3. C. short total = 0;
  4. D. unsigned int total = 0; with a cast on every addend

Correct answer: A. long long total = 0;

The sum can reach 10^14, far above INT_MAX, so the total needs at least a 64-bit type. unsigned int is also too small, and wrapping would give a wrong answer.

More C Programming questions

Exams that ask this topic