c语言跨平台的实用技巧

来源:互联网 发布:python scatter 气泡 编辑:程序博客网 时间:2024/04/25 14:21

c语言在发展的历程中经历了c89和c99两个标准,为了实现c语言的跨平台的优点,就是在windows和Linux以及Unix上都可以执行,就需要进行一些跨平台的技巧了,但是不但c语言的标准多种多样,并且c语言的编译器也有很多种,gcc,lcc,vc++等,比如:对于int型有的编译器赋予4个字节,有的赋予2个字节。再次我给大家介绍一个好的技巧,就是inttypes.h头文件,这个头文件一般在编译器里边都有,#include<inttypes.h>以后就可以直接根据字节数目来定义变量而不是通过其他的.

以下是这个头文件在gcc编译器的代码,其他的类似

  1. bash-3.00$ vi int_types.h   
  2. "int_types.h" [Read only] 176 lines, 4367 characters   
  3. /* 
  4.  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved. 
  5.  * Use is subject to license terms. 
  6.  */  
  7.   
  8. #ifndef _SYS_INT_TYPES_H  
  9. #define _SYS_INT_TYPES_H  
  10.   
  11. #pragma ident   "@(#)int_types.h        1.10    04/09/28 SMI"  
  12.   
  13. /* 
  14.  * This file, <sys/int_types.h>, is part of the Sun Microsystems implementation 
  15.  * of <inttypes.h> defined in the ISO C standard, ISO/IEC 9899:1999 
  16.  * Programming language - C. 
  17.  * 
  18.  * Programs/Modules should not directly include this file.  Access to the 
  19.  * types defined in this file should be through the inclusion of one of the 
  20.  * following files: 
  21.  * 
  22.  *      <sys/types.h>           Provides only the "_t" types defined in this 
  23.  *                              file which is a subset of the contents of 
  24.  *                              <inttypes.h>.  (This can be appropriate for 
  25.  *                              all programs/modules except those claiming 
  26.  *                              ANSI-C conformance.) 
  27.  * 
  28.  *      <sys/inttypes.h>        Provides the Kernel and Driver appropriate 
  29.  *                              components of <inttypes.h>. 
  30.  * 
  31.  *      <inttypes.h>            For use by applications. 
  32.  * 
  33.  * See these files for more details. 
  34.  */  
  35.   
  36. #include <sys/feature_tests.h>  
  37.   
  38. #ifdef __cplusplus  
  39. extern "C" {  
  40. #endif  
  41.   
  42. /* 
  43.  * Basic / Extended integer types 
  44.  * 
  45.  * The following defines the basic fixed-size integer types. 
  46.  * 
  47.  * Implementations are free to typedef them to Standard C integer types or 
  48.  * extensions that they support. If an implementation does not support one 
  49.  * of the particular integer data types below, then it should not define the 
  50.  * typedefs and macros corresponding to that data type.  Note that int8_t 
  51.  * is not defined in -Xs mode on ISAs for which the ABI specifies "char" 
  52.  * as an unsigned entity because there is no way to define an eight bit 
  53.  * signed integral. 
  54.  */  
  55. #if defined(_CHAR_IS_SIGNED)  
  56. typedef char                    int8_t;  
  57. #else  
  58. #if defined(__STDC__)  
  59. typedef signed char             int8_t;  
  60. #endif  
  61. #endif  
  62. typedef short                   int16_t;  
  63. typedef int                     int32_t;  
  64. #ifdef  _LP64  
  65. #define _INT64_TYPE  
  66. typedef long                    int64_t;  
  67. #else   /* _ILP32 */  
  68. #if defined(_LONGLONG_TYPE)  
  69. #define _INT64_TYPE  
  70. typedef long long               int64_t;  
  71. #endif  
  72. #endif  
  73.   
  74. typedef unsigned char           uint8_t;  
  75. typedef unsigned short          uint16_t;  
  76. typedef unsigned int            uint32_t;  
  77. #ifdef  _LP64  
  78. typedef unsigned long           uint64_t;  
  79. #else   /* _ILP32 */  
  80. #if defined(_LONGLONG_TYPE)  
  81. typedef unsigned long long      uint64_t;  
  82. #endif  
  83. #endif  
  84.   
  85. /* 
  86.  * intmax_t and uintmax_t are to be the longest (in number of bits) signed 
  87.  * and unsigned integer types supported by the implementation. 
  88.  */  
  89. #if defined(_INT64_TYPE)  
  90. typedef int64_t                 intmax_t;  
  91. typedef uint64_t                uintmax_t;  
  92. #else  
  93. typedef int32_t                 intmax_t;  
  94. typedef uint32_t                uintmax_t;  
  95. #endif  
  96.   
  97. /* 
  98.  * intptr_t and uintptr_t are signed and unsigned integer types large enough 
  99.  * to hold any data pointer; that is, data pointers can be assigned into or 
  100.  * from these integer types without losing precision. 
  101.  */  
  102. #if defined(_LP64) || defined(_I32LPx)  
  103. typedef long                    intptr_t;  
  104. typedef unsigned long           uintptr_t;  
  105. #else  
  106. typedef int                     intptr_t;  
  107. typedef unsigned int            uintptr_t;  
  108. #endif  
  109.   
  110. /* 
  111.  * The following define the fastest integer types that can hold the 
  112.  * specified number of bits. 
  113.  */  
  114. #if defined(_CHAR_IS_SIGNED)  
  115. typedef char                    int_fast8_t;  
  116. #else  
  117. #if defined(__STDC__)  
  118. typedef signed char             int_fast8_t;  
  119. #endif  
  120. #endif  
  121. typedef int                     int_fast16_t;  
  122. typedef int                     int_fast32_t;  
  123. #ifdef  _LP64  
  124. typedef long                    int_fast64_t;  
  125. #else   /* _ILP32 */  
  126. #if defined(_LONGLONG_TYPE)  
  127. typedef long long               int_fast64_t;  
  128. #endif  
  129. #endif  
  130.   
  131. typedef unsigned char           uint_fast8_t;  
  132. typedef unsigned int            uint_fast16_t;  
  133. typedef unsigned int            uint_fast32_t;  
  134. #ifdef  _LP64  
  135. typedef unsigned long           uint_fast64_t;  
  136. #else   /* _ILP32 */  
  137. #if defined(_LONGLONG_TYPE)  
  138. typedef unsigned long long      uint_fast64_t;  
  139. #endif  
  140. #endif  
  141.   
  142. /* 
  143.  * The following define the smallest integer types that can hold the 
  144.  * specified number of bits. 
  145.  */  
  146. #if defined(_CHAR_IS_SIGNED)  
  147. typedef char                    int_least8_t;  
  148. #else  
  149. #if defined(__STDC__)  
  150. typedef signed char             int_least8_t;  
  151. #endif  
  152. #endif  
  153. typedef short                   int_least16_t;  
  154. typedef int                     int_least32_t;  
  155. #ifdef  _LP64  
  156. typedef long                    int_least64_t;  
  157. #else   /* _ILP32 */  
  158. #if defined(_LONGLONG_TYPE)  
  159. typedef long long               int_least64_t;  
  160. #endif  
  161. #endif  
  162.   
  163. typedef unsigned char           uint_least8_t;  
  164. typedef unsigned short          uint_least16_t;  
  165. typedef unsigned int            uint_least32_t;  
  166. #ifdef  _LP64  
  167. typedef unsigned long           uint_least64_t;  
  168. #else   /* _ILP32 */  
  169. #if defined(_LONGLONG_TYPE)  
  170. typedef unsigned long long      uint_least64_t;  
  171. #endif  
  172. #endif  
  173.   
  174. #ifdef __cplusplus  
  175. }  
  176. #endif  
  177.   
  178. #endif /* _SYS_INT_TYPES_H */  

0 0