LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Predict the Output · Question 9

Three ways to declare "hello" – sizes Answer, explanation & discussion

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

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

main.c
#include <stdio.h>

int main(void) {
    char a[] = "hello";
    char b[10] = "hello";
    const char *c = "hello";
    printf("%zu %zu %zu\n", sizeof a, sizeof b, sizeof c);
    return 0;
}
  • A 6 10 8 Correct
  • B 5 10 5
  • C 6 6 6
  • D 6 10 6
Correct Answer: Option A

a gets exactly 6 bytes from the string, b the 10 bytes declared, and c is a pointer (8 bytes). The declaration, not the string, decides the size.

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