cgg之数据类型
所有例子都在64为操作系统
Linux 2.6.30 x86_64 x86_64 x86_64 GNU/Linux
1.1整数
在stdint.h中定义一些看上去更明确的整数类型
#ifndef __int8_t_defined
# define __int8_t_defined
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
# if __WORDSIZE ==
typedef long int int64_t;
# else
__extension__
typedef long long int int64_t;
# endif
#endif /* Unsigned. */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
#ifndef __uint32_t_defined
typedef unsigned int uint32_t;
# define __uint32_t_defined
#endif
#if __WORDSIZE == 64
typedef unsigned long int uint64_t;
#else
__extension__
typedef unsigned long long int uint64_t;
#endif
还有各种整数类型的大小限制:
/* Minimum of signed integral types. */
# define INT8_MIN (-)
# define INT16_MIN (--)
# define INT32_MIN (--)
# define INT64_MIN (-__INT64_C()-)
/* Maximum of signed integral types. */
# define INT8_MAX ()
# define INT16_MAX ()
# define INT32_MAX ()
# define INT64_MAX (__INT64_C())
字符常量默认是一个int整数,但编译器可以自行决定将其解释为char或者int
char c = 'a';
printf("%c,size(char)=%d,size('a')=%d;\n",c,sizeof(c),sizeof('a'));
输出结果为:
a,size(char)=,size('a')=;
可以看出sizeof(c)和sizeof('a')的大小不同,后者为int类型,前者为char
指针是个有特殊用途的整数,在stdint.h中同样给出其类型定义:
/* Types for `void *' pointers. */
#if __WORDSIZE == 64
# ifndef __intptr_t_defined
typedef long int intptr_t;
# define __intptr_t_defined
# endif
typedef unsigned long int uintptr_t;
#else
# ifndef __intptr_t_defined
typedef int intptr_t;
# define __intptr_t_defined
# endif
typedef unsigned int uintptr_t;
#endif
在64位系统中:
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(long));
输出结果为:
4
8
stdint.h中定义了一些辅助宏:
# define INT8_C(c) c
# define INT16_C(c) c
# define INT32_C(c) c
# if __WORDSIZE ==
# define INT64_C(c) c ## L
# else
# define INT64_C(c) c ## LL
# endif
对于宏定义中"##"表示什么意思呢?
http://blog.csdn.net/dotphoenix/article/details/4345174
表示把左和右结合在一起,作为一个符号,如下举个例子:
[root@typhoeus79 ]# more test.c
#include <stdio.h>
#include <stdint.h> # define TEST(c) c ## UL int main()
{
printf("%d\n",sizeof());
printf("%d\n",sizeof(TEST())); //20默认是属于int类似,使用TEST宏之后将其转换为UL,无符号长整型 return ;
}
[root@typhoeus79 ]# ./test
1.2浮点数
c提供不同精度的浮点:
*float:32位4字节浮点数,精确度为6
*double:64位8字节浮点数,精确度为15
*long double:80位10字节浮点数,精确度为19位
测试结果:
printf("%d\n",sizeof(long double));
printf("%d\n",sizeof(double));
printf("%d\n",sizeof(float));
输出:
16
8
4
对于long double为啥不是10个字节呢??
对应的文件为bits/mathdef.h
# if __WORDSIZE == || (defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == )
/* The x86-64 architecture computes values with the precission of the
used type. Similarly for -m32 -mfpmath=sse. */
typedef float float_t; /* `float' expressions are evaluated as `float'. */
typedef double double_t; /* `double' expressions are evaluated
as `double'. */
# else
/* The ix87 FPUs evaluate all values in the 80 bit floating-point format
which is also available for the user as `long double'. Therefore we
define: */
typedef long double float_t; /* `float' expressions are evaluated as
`long double'. */
typedef long double double_t; /* `double' expressions are evaluated as
`long double'. */
# endif
c99支持复数,在complex.h中有定义
double complex comp = 1.0 +3.0 * I;
printf("%f\n",creal(comp));
printf("%f\n",cimag(comp));
输出:
1.000000
3.000000
1.3枚举
例子
enum color {black,red=,green};
enum color b = black;
enum color r = red;
enum color g = green;
printf("black=%d\n",b);
printf("red=%d\n",r);
printf("green=%d\n",g);
输出结果为:
black=
red=
green=
默认是递增,从0开始,若中间有重新设置,如例子中red=5,后面的还是继续加1
枚举成员的值是可以相同的,这样有什么用呢??
enum color {black,red=,green=};
enum color b = black;
enum color r = red;
enum color g = green;
printf("black=%d\n",b);
printf("red=%d\n",r);
printf("green=%d\n",g);
输出为:
black=
red=
green=
通常省略枚举小标签用来代替宏定义常量
[root@typhoeus79 ]# more test_enum.c
#include <stdio.h> int main()
{
enum {BLACK=,RED=,GREEN,YELLOW=}; printf("%d\n",BLACK);
printf("%d\n",RED);
printf("%d\n",GREEN);
printf("%d\n",YELLOW);
}
输出:
[root@typhoeus79 ]# ./test_enum
可以作为定义一些常量使用。
cgg之数据类型的更多相关文章
- JavaScript 中的数据类型
Javascript中的数据类型有以下几种情况: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Function,Date,Ar ...
- JS 判断数据类型的三种方法
说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...
- Python高手之路【二】python基本数据类型
一:数字 int int(整型): 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值 ...
- UniqueIdentifier 数据类型 和 GUID 生成函数
UniqueIdentifier 数据类型用于存储GUID的值,占用16Byte. SQL Server将UniqueIdentifier存储为16字节的二进制数值,Binary(16),按照特定的格 ...
- SQL Server常见数据类型介绍
数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 1.整数类型 int 存储范围是-2,147,483,648到2,147,483 ...
- 由js apply与call方法想到的js数据类型(原始类型和引用类型)
原文地址:由js apply与call方法想到的js数据类型(原始类型和引用类型) js的call方法与apply方法的区别在于第二个参数的不同,他们都有2个参数,第一个为对象(即需要用对象a继承b, ...
- python 数据类型 ----字典
字典由一对key:value 组成的 python中常用且重量级的数据类型 1. key , keys, values 字典由一对key:value 组成的 python中常用且重量级的数据类型 1. ...
- SQL数据类型
1.Character 字符串: 数据类型 描述 存储 char(n) 固定长度的字符串.最多8,000个字符. n varchar(n) 可变长度的字符串.最多8,000个字符. varchar ...
- 跟着老男孩教育学Python开发【第二篇】:Python基本数据类型
运算符 设定:a=10,b=20 . 算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**3 ...
随机推荐
- HDU1285 确定比赛名次
有N个比赛队(<=N<=),编号依次为1,,,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结 ...
- C#无限级分类递归显示示例
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RoleDemo20150305 ...
- 从字符串获得MAC地址的方法
今日有感于编程水平下降,特意练习一下,根据MAC地址字符串,获取MAC地址的2种方法. #include <stdio.h> void func1(char *str){ unsigned ...
- ajax和jsonp使用总结
前言:ajax和jsonp可以与后台通信,获取数据和信息,但是又不用刷新整个页面,实现页面的局部刷新. 一.ajax 定义:一种发送http请求与后台进行异步通讯的技术. 原理:实例化xmlhttp对 ...
- JAVA基础-反射
一.反射的介绍 JAVA反射机制是在运行状态中,能够获取任意一个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法.这种动态获取的以及动态调用对象的方法的功能称为java语言的反射机制. ...
- (转)JVM内存组成及分配
转自:http://www.cnblogs.com/redcreen/archive/2011/05/04/2036387.html java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 ...
- 【ASP.NET MVC 学习笔记】- 15 Unobtrusive Ajax
本文参考:http://www.cnblogs.com/willick/p/3418517.html 1.Unobtrusive Ajax允许我们通过 MVC 的 Help mothod 来定义 Aj ...
- Ubuntu/deppin 系统安装Nginx
Ubuntu/deppin 系统安装Nginx 添加密钥 打开Nginx官网 ,并找到如下位置: 在桌面新建"nginx_signing.key"文件,文件内容为请点击图1中的标注 ...
- tomcat启动报错Several ports (8080, 8009) required by Tomcat v6.0
tomcat启动报错 如下图: 问题:8080.8009端口已经被占用. 解决办法: 1.在命令提示符下,输入netstat -aon | findstr 8080 2.继续输入taskkill -F ...
- Android Studio常见问题解决
1.Error:Execution failed for task ':XXXX:processDebugManifest'. > Manifest merger failed with mul ...