LearnFrenzy
Sign in Take a free test

C Library Functions · Find the Error · Question 1

Error: calling sqrt() without math.h Answer, explanation & discussion

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

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    double side = 49;
    printf("%.1f\n", sqrt(side));
    return 0;
}
  • A side must be an int
  • B sqrt is used without its declaration – <math.h> is not included Correct
  • C %.1f cannot print a double
  • D There is no bug
Correct Answer: Option B

Since C99 an undeclared function cannot be called. Before that the compiler assumed it returned int, which gave garbage for double results.

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