在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