编写一段代码,确定一个变量是有符号数还是无符号数

#include #define ISUNSIGNED(a) (a >= 0) && (~a >= 0) // 如果要考虑整形提升的话 #define ISUNSIGNED_EX(type, a) ((type)a >= (type)0) && ((type)~a >= (type)0) int main() { int a = 0; unsigned char b = 0; int c = 1; int d = -1; printf("%d /n", ISUNSIGNED(a)); printf("%d /n", ISUNSIGNED(b)); printf("%d /n", ISUNSIGNED(c)); printf("%d /n", ISUNSIGNED(d)); printf("---------/n"); printf("%d /n", ISUNSIGNED_EX(short int, a)); printf("%d /n", ISUNSIGNED_EX(unsigned char, b)); printf("%d /n", ISUNSIGNED_EX(int, c)); printf("%d /n", ISUNSIGNED_EX(int, d)); return 0; } 输出: 0 0 0 0 --------- 0 1 0 0 Press any key to continue  

 

引用至:求教一个面试题。http://topic.csdn.net/u/20090618/18/b892dbeb-2e2a-43cf-91bd-94ce661ad104.html?18292

你可能感兴趣的:(Forum,Question)