原文: https://www.programiz.com/c-programming/examples/negative-positive-zero
要理解此示例,您应该了解以下 C 编程主题:
该程序从用户那里获取一个号码,并检查该号码是positive
还是negative
或zero
。
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
} else
printf("You entered a positive number.");
return 0;
}
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num < 0.0)
printf("You entered a negative number.");
else if (num > 0.0)
printf("You entered a positive number.");
else
printf("You entered 0.");
return 0;
}
输出 1
Enter a number: 12.3
You entered a positive number.
输出 2
Enter a number: 0
You entered 0.