在C语言中,sign函数通常用来返回一个数的符号,即判断一个数是正数、负数还是零。其原型定义为:
int sign(int x);
参数x为要判断符号的数,函数返回值为:
示例代码如下:
#include <stdio.h>
int sign(int x){
if(x > 0){
return 1;
}else if(x == 0){
return 0;
}else{
return -1;
}
}
int main(){
int num = 10;
printf("The sign of %d is: %d\n", num, sign(num));
num = -5;
printf("The sign of %d is: %d\n", num, sign(num));
num = 0;
printf("The sign of %d is: %d\n", num, sign(num));
return 0;
}
输出结果为:
The sign of 10 is: 1
The sign of -5 is: -1
The sign of 0 is: 0