c语言跨平台的实用技巧
2013-12-24 20:09
239 查看
c语言在发展的历程中经历了c89和c99两个标准,为了实现c语言的跨平台的优点,就是在windows和Linux以及Unix上都可以执行,就需要进行一些跨平台的技巧了,但是不但c语言的标准多种多样,并且c语言的编译器也有很多种,gcc,lcc,vc++等,比如:对于int型有的编译器赋予4个字节,有的赋予2个字节。再次我给大家介绍一个好的技巧,就是inttypes.h头文件,这个头文件一般在编译器里边都有,#include<inttypes.h>以后就可以直接根据字节数目来定义变量而不是通过其他的.
以下是这个头文件在gcc编译器的代码,其他的类似
bash-3.00$ vi int_types.h
"int_types.h" [Read only] 176 lines, 4367 characters
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _SYS_INT_TYPES_H
#define _SYS_INT_TYPES_H
#pragma ident "@(#)int_types.h 1.10 04/09/28 SMI"
/*
* This file, <sys/int_types.h>, is part of the Sun Microsystems implementation
* of <inttypes.h> defined in the ISO C standard, ISO/IEC 9899:1999
* Programming language - C.
*
* Programs/Modules should not directly include this file. Access to the
* types defined in this file should be through the inclusion of one of the
* following files:
*
* <sys/types.h> Provides only the "_t" types defined in this
* file which is a subset of the contents of
* <inttypes.h>. (This can be appropriate for
* all programs/modules except those claiming
* ANSI-C conformance.)
*
* <sys/inttypes.h> Provides the Kernel and Driver appropriate
* components of <inttypes.h>.
*
* <inttypes.h> For use by applications.
*
* See these files for more details.
*/
#include <sys/feature_tests.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Basic / Extended integer types
*
* The following defines the basic fixed-size integer types.
*
* Implementations are free to typedef them to Standard C integer types or
* extensions that they support. If an implementation does not support one
* of the particular integer data types below, then it should not define the
* typedefs and macros corresponding to that data type. Note that int8_t
* is not defined in -Xs mode on ISAs for which the ABI specifies "char"
* as an unsigned entity because there is no way to define an eight bit
* signed integral.
*/
#if defined(_CHAR_IS_SIGNED)
typedef char int8_t;
#else
#if defined(__STDC__)
typedef signed char int8_t;
#endif
#endif
typedef short int16_t;
typedef int int32_t;
#ifdef _LP64
#define _INT64_TYPE
typedef long int64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
#define _INT64_TYPE
typedef long long int64_t;
#endif
#endif
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
#ifdef _LP64
typedef unsigned long uint64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef unsigned long long uint64_t;
#endif
#endif
/*
* intmax_t and uintmax_t are to be the longest (in number of bits) signed
* and unsigned integer types supported by the implementation.
*/
#if defined(_INT64_TYPE)
typedef int64_t intmax_t;
typedef uint64_t uintmax_t;
#else
typedef int32_t intmax_t;
typedef uint32_t uintmax_t;
#endif
/*
* intptr_t and uintptr_t are signed and unsigned integer types large enough
* to hold any data pointer; that is, data pointers can be assigned into or
* from these integer types without losing precision.
*/
#if defined(_LP64) || defined(_I32LPx)
typedef long intptr_t;
typedef unsigned long uintptr_t;
#else
typedef int intptr_t;
typedef unsigned int uintptr_t;
#endif
/*
* The following define the fastest integer types that can hold the
* specified number of bits.
*/
#if defined(_CHAR_IS_SIGNED)
typedef char int_fast8_t;
#else
#if defined(__STDC__)
typedef signed char int_fast8_t;
#endif
#endif
typedef int int_fast16_t;
typedef int int_fast32_t;
#ifdef _LP64
typedef long int_fast64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef long long int_fast64_t;
#endif
#endif
typedef unsigned char uint_fast8_t;
typedef unsigned int uint_fast16_t;
typedef unsigned int uint_fast32_t;
#ifdef _LP64
typedef unsigned long uint_fast64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef unsigned long long uint_fast64_t;
#endif
#endif
/*
* The following define the smallest integer types that can hold the
* specified number of bits.
*/
#if defined(_CHAR_IS_SIGNED)
typedef char int_least8_t;
#else
#if defined(__STDC__)
typedef signed char int_least8_t;
#endif
#endif
typedef short int_least16_t;
typedef int int_least32_t;
#ifdef _LP64
typedef long int_least64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef long long int_least64_t;
#endif
#endif
typedef unsigned char uint_least8_t;
typedef unsigned short uint_least16_t;
typedef unsigned int uint_least32_t;
#ifdef _LP64
typedef unsigned long uint_least64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef unsigned long long uint_least64_t;
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif /* _SYS_INT_TYPES_H */
以下是这个头文件在gcc编译器的代码,其他的类似
bash-3.00$ vi int_types.h
"int_types.h" [Read only] 176 lines, 4367 characters
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _SYS_INT_TYPES_H
#define _SYS_INT_TYPES_H
#pragma ident "@(#)int_types.h 1.10 04/09/28 SMI"
/*
* This file, <sys/int_types.h>, is part of the Sun Microsystems implementation
* of <inttypes.h> defined in the ISO C standard, ISO/IEC 9899:1999
* Programming language - C.
*
* Programs/Modules should not directly include this file. Access to the
* types defined in this file should be through the inclusion of one of the
* following files:
*
* <sys/types.h> Provides only the "_t" types defined in this
* file which is a subset of the contents of
* <inttypes.h>. (This can be appropriate for
* all programs/modules except those claiming
* ANSI-C conformance.)
*
* <sys/inttypes.h> Provides the Kernel and Driver appropriate
* components of <inttypes.h>.
*
* <inttypes.h> For use by applications.
*
* See these files for more details.
*/
#include <sys/feature_tests.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Basic / Extended integer types
*
* The following defines the basic fixed-size integer types.
*
* Implementations are free to typedef them to Standard C integer types or
* extensions that they support. If an implementation does not support one
* of the particular integer data types below, then it should not define the
* typedefs and macros corresponding to that data type. Note that int8_t
* is not defined in -Xs mode on ISAs for which the ABI specifies "char"
* as an unsigned entity because there is no way to define an eight bit
* signed integral.
*/
#if defined(_CHAR_IS_SIGNED)
typedef char int8_t;
#else
#if defined(__STDC__)
typedef signed char int8_t;
#endif
#endif
typedef short int16_t;
typedef int int32_t;
#ifdef _LP64
#define _INT64_TYPE
typedef long int64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
#define _INT64_TYPE
typedef long long int64_t;
#endif
#endif
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
#ifdef _LP64
typedef unsigned long uint64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef unsigned long long uint64_t;
#endif
#endif
/*
* intmax_t and uintmax_t are to be the longest (in number of bits) signed
* and unsigned integer types supported by the implementation.
*/
#if defined(_INT64_TYPE)
typedef int64_t intmax_t;
typedef uint64_t uintmax_t;
#else
typedef int32_t intmax_t;
typedef uint32_t uintmax_t;
#endif
/*
* intptr_t and uintptr_t are signed and unsigned integer types large enough
* to hold any data pointer; that is, data pointers can be assigned into or
* from these integer types without losing precision.
*/
#if defined(_LP64) || defined(_I32LPx)
typedef long intptr_t;
typedef unsigned long uintptr_t;
#else
typedef int intptr_t;
typedef unsigned int uintptr_t;
#endif
/*
* The following define the fastest integer types that can hold the
* specified number of bits.
*/
#if defined(_CHAR_IS_SIGNED)
typedef char int_fast8_t;
#else
#if defined(__STDC__)
typedef signed char int_fast8_t;
#endif
#endif
typedef int int_fast16_t;
typedef int int_fast32_t;
#ifdef _LP64
typedef long int_fast64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef long long int_fast64_t;
#endif
#endif
typedef unsigned char uint_fast8_t;
typedef unsigned int uint_fast16_t;
typedef unsigned int uint_fast32_t;
#ifdef _LP64
typedef unsigned long uint_fast64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef unsigned long long uint_fast64_t;
#endif
#endif
/*
* The following define the smallest integer types that can hold the
* specified number of bits.
*/
#if defined(_CHAR_IS_SIGNED)
typedef char int_least8_t;
#else
#if defined(__STDC__)
typedef signed char int_least8_t;
#endif
#endif
typedef short int_least16_t;
typedef int int_least32_t;
#ifdef _LP64
typedef long int_least64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef long long int_least64_t;
#endif
#endif
typedef unsigned char uint_least8_t;
typedef unsigned short uint_least16_t;
typedef unsigned int uint_least32_t;
#ifdef _LP64
typedef unsigned long uint_least64_t;
#else /* _ILP32 */
#if defined(_LONGLONG_TYPE)
typedef unsigned long long uint_least64_t;
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif /* _SYS_INT_TYPES_H */
相关文章推荐
- C语言已死(连载1)——趣味、通俗、实用的计算机达人成长之路之C语言高级技巧篇
- c语言-位运算简介及实用技巧(一):基础篇
- 一波C语言字符数组实用技巧集锦
- C语言已死(连载1)——趣味、通俗、实用的计算机达人成长之路之C语言高级技巧篇
- c语言-位运算简介及实用技巧(二):进阶篇(1)
- 这样学习C语言最有效(连载2)——趣味、通俗、实用的计算机达人成长之路之C语言高级技巧篇
- 实用C语言技巧
- 读《C语言高级实用编程技巧》备忘
- 实用C语言技巧
- Source Insight 的实用技巧
- Windows Vista 实用技巧八则
- 20个超实用的JavaScript技巧及最佳实践
- 20个超实用的JavaScript技巧及最佳实践
- PHP自动加载实用技巧
- AndroidStudio实用技巧总结
- CodeSmith实用技巧(八):生成的代码输出到文件中
- Unity3d的iOS调试实用技巧
- excel 表格实用技巧大全
- js实用调试技巧
- 45个实用的JavaScript技巧、窍门和最佳实践