abs int 宏定义_C - 跨平台宏定义

1 #ifndef _H_INCLUDED2 #define _H_INCLUDED

3

4 #include

5 #include

6 #include

7 #include

8 #include

9 #include

10 #include

11 #include

12

13 #define DEBUG

14 #define USE_WINDOWS

15

16 /*********************************************/

17 #if defined(USE_WINDOWS)

18 #define DIR_SEPERATOR ‘\\‘

19 #else

20 #define DIR_SEPERATOR ‘/‘

21 #endif

22 /*********************************************/

23

24 /*********************************************/

25 #if LONG_MAX >> 31 > 0

26 #define WORD_BYTES 8 /* 64-bit */

27 #elif INT_MAX >> 15 > 0

28 #define WORD_BYTES 4 /* 32-bit */

29 #else

30 #define WORD_BYTES 2 /* 16-bit */

31 #endif

32 /*********************************************/

33

34 /*********************************************/

35 #if WORD_BYTES == 8

36

37 typedef signed longintx;38 typedef unsigned longuintx;39 typedef signed intint32;40 typedef unsigned intuint32;41

42 #elif WORD_BYTES == 4

43

44 typedef signed intintx;45 typedef unsigned intuintx;46 typedef signed intint32;47 typedef unsigned intuint32;48

49 #else

50

51 typedef signed intintx;52 typedef unsigned intuintx;53 typedef signed longint32;54 typedef unsigned longuint32;55

56 #endif /* WORD_BYTES */

57 /*********************************************/

58

59 /*********************************************/

60 #define REAL_TYPE_FLOAT 1

61 #define REAL_TYPE_DOUBLE 2

62 #define REAL_TYPE_LONGDOUBLE 3

63 /*********************************************/

64

65 /*********************************************/

66 #ifndef REAL_TYPE67

68 #if WORD_BYTES == 8

69 #define REAL_BYTES 8

70 #define REAL_TYPE REAL_TYPE_DOUBLE

71 #else

72 #define REAL_BYTES 4

73 #define REAL_TYPE REAL_TYPE_FLOAT

74 #endif /* WORD_BYTES */

75

76 #endif /* REAL_TYPE */

77 /*********************************************/

78

79 /*********************************************/

80 #if REAL_TYPE == REAL_TYPE_FLOAT

81

82 typedef floatreal;83 #define REAL_FORMAT "%.7g"

84 #define real_limit(n) FLT_##n

85 #define math_op(op) op##f

86 #define math_s2r(s,p) (strtof((s),(p)))

87

88 #elif REAL_TYPE == REAL_TYPE_DOUBLE

89

90 typedef doublereal;91 #define REAL_FORMAT "%.14g"

92 #define real_limit(n) DBL_##n

93 #define math_op(op) op##d

94 #define math_s2r(s,p) (strtod((s),(p)))

95

96 #elif REAL_TYPE == REAL_TYPE_LONGDOUBLE

97

98 #define REAL_FORMAT "%.19g"

99 #define real_limit(n) LDBL_##n

100 #define math_op(op) op##l

101 #define math_s2r(s,p) (strtold((s),(p)))

102

103 #else

104

105 #error "numeric float type not defined"

106

107 #endif

108 /*********************************************/

109

110 /*********************************************/

111 typedef signed shortint16;112 typedef unsigned shortuint16;113 typedef signed charint8;114 typedef unsigned charuint8;115 typedef unsigned char byte;116 typedef unsigned charboolean;117 typedef void *Pointer;118 typedef char *Chars;119 typedef FILE *File;120 /*********************************************/

121

122 /*********************************************/

123 #define bitsof(t) (sizeof(t)<<3)

124 #define cast_type(t, x) ((t)(x))

125 #define cast_intx(x) ((intx)(x))

126 #define cast_uintx(x) ((uintx)(x))

127 #define cast_int32(x) ((int32)(x))

128 #define cast_uint32(x) ((uint32)(x))

129 #define cast_int16(x) ((int16)(x))

130 #define cast_uint16(x) ((uint16)(x))

131 #define cast_int8(x) ((int8)(x))

132 #define cast_uint8(x) ((uint8)(x))

133 #define cast_byte(x) ((byte)(x))

134 #define cast_boolean(x) ((boolean)(!!(x)))

135 #define cast_void(x) ((void)(x))

136 #define cast_Chars(x) ((Chars)(x))

137 #define cast_Pointer(x) ((Pointer)(x))

138 #define struct_new(t) ((t)calloc(1, sizeof_type(t)))

139 #define struct_alloc(t) ((t)malloc(sizeof_type(t)))

140 #define array_size(a) (sizeof(a)/sizeof(*(a)))

141 #define math_max(x, y) ((x) > (y) ? (x) : (y))

142 #define math_min(x, y) ((x) < (y) ? (x) : (y))

143 #define math_swap(x, y) ((x) ^= (y) ^= (x) ^= (y))

144 #define math_abs(x) ((x) < 0 ? -(x) : (x))

145 #define math_sign(x) ((x) < 0 ? -1 : ((x) ? 1 : 0))

146 #define math_r2i(r, i) ((i = (r)) == (r))

147 #define math_pow2(n) (1 << (n))

148 /*********************************************/

149

150 /*********************************************/

151 #ifdef DEBUG152 #define debug_assert(c) ((c)||printf("debug>> assertion failed: %s, #%d, "#c"\n",__FILE__,__LINE__))

153 #else

154 #define debug_assert(c) ((void)0)

155 #endif

156 /*********************************************/

157

158 #endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C语言中,可以使用`<limits.h>`头文件中定义来获取整型的最大值。 对于带符号整型`int`,可以使用`INT_MAX`来获取其最大值,它的值为2147483647(2的31次方-1)。对于无符号整型`unsigned int`,可以使用`UINT_MAX`来获取其最大值,它的值为4294967295(2的32次方-1)。 以下是一个示例程序,演示如何使用`INT_MAX`和`UINT_MAX`: ```c #include <stdio.h> #include <limits.h> int main() { printf("The maximum value of signed int is: %d\n", INT_MAX); printf("The maximum value of unsigned int is: %u\n", UINT_MAX); return 0; } ``` 输出结果: ``` The maximum value of signed int is: 2147483647 The maximum value of unsigned int is: 4294967295 ``` ### 回答2: C语言中关于整型最大值的定义是通过limits.h头文件中的定义来实现的。整型最大值定义INT_MAX,它表示了int类型能够表示的最大正整数值。在32位系统中,INT_MAX的值是2147483647。这个定义可以在程序中使用,用于与变量进行比较或进行其他操作,以确定int类型的取值范围。整型最大值的定义是C语言中非常有用的一个,它提供了一种简洁方便的方式来表示int类型的取值范围,使得程序员能够更轻松地处理整型数据。通过整型最大值定义,程序员可以避免直接使用具体的数值,增加了代码的可读性和可维护性。此外,整型最大值定义还可以用于平台编程,因为不同的操作系统上int类型的取值范围可能有所不同,使用定义可以保证在不同操作系统上的一致性。总之,C语言中的整型最大值定义是一项非常重要和方便的特性,它提供了一种简单灵活的方式来表示int类型的最大取值范围,方便程序员进行相关操作和判断。 ### 回答3: 在C语言中,用定义来表示整型的最大值,可以使用INT_MAX定义。该定义在头文件<limits.h>中定义,表示整型的最大值。该定义会根据具体的编译器和系统平台而不同,一般情况下被定义为2147483647。这个值代表了32位有符号整型的最大值。在不同平台上,INT_MAX的值可能会有所变化,但通常都是2^31-1。通过使用INT_MAX定义,可以在程序中直接使用该值,而不必手动输入。这样做可以提高代码的可读性和可移植性,并且使得代码易于维护和修改。使用INT_MAX定义,可以确保整型数值在不同平台上都能正确地表示为最大值,而不受编译器和系统的限制。因此,INT_MAX定义在C语言中被广泛使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值