完整示例代码如下:
- #include <stdio.h>
- #include <math.h>
- int main() {
- double number = 16.0;
-
- if (number >= 0) {
- double result = sqrt(number);
- printf("The square root of %.2f is %.2f\n", number, result);
- } else {
- printf("Error: The input must be non-negative.\n");
- }
- return 0;
- }
这段代码首先包含了必要的头文件,然后声明了一个变量 number 并赋予一个正数值,接着安全地调用了 sqrt() 函数,最后打印出了结果。
|