LearnFrenzy
Sign in Take a free test

Bitwise Operators · Find the Error · Question 8

Bug: using ^ to square a number in C Answer, explanation & discussion

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

The program should print the area of a square with side 5. What does it print, and why?

main.c
#include <stdio.h>

int main(void) {
    int side = 5;
    int area = side ^ 2;
    printf("%d\n", area);
    return 0;
}
  • A 25 – ^ means power
  • B 7 – ^ is bitwise XOR, not exponentiation: 101 ^ 010 = 111 Correct
  • C 10 – ^ means multiply
  • D It does not compile
Correct Answer: Option B

C has no power operator. ^ is XOR: 5 (101) XOR 2 (010) = 7 (111). Write side * side, or pow() from <math.h> for floating-point powers.

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