LearnFrenzy
Sign in Take a free test

C Library Functions · Predict the Output · Question 5

memset(a, 1, sizeof a) on an int array Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>
#include <string.h>

int main(void) {
    int a[3];
    memset(a, 1, sizeof a);
    printf("%d\n", a[0]);
    memset(a, 0, sizeof a);
    printf("%d\n", a[2]);
    return 0;
}
  • A 16843009 0 Correct
  • B 1 0
  • C 111 0
  • D 16843009 16843009
Correct Answer: Option A

memset sets every byte, so each 4-byte int becomes 0x01010101 = 16843009. Only 0 (and −1, all bits set) work as int fill values.

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