所有例子都在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之数据类型的更多相关文章

  1. JavaScript 中的数据类型

    Javascript中的数据类型有以下几种情况: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Function,Date,Ar ...

  2. JS 判断数据类型的三种方法

    说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...

  3. Python高手之路【二】python基本数据类型

    一:数字 int int(整型): 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值 ...

  4. UniqueIdentifier 数据类型 和 GUID 生成函数

    UniqueIdentifier 数据类型用于存储GUID的值,占用16Byte. SQL Server将UniqueIdentifier存储为16字节的二进制数值,Binary(16),按照特定的格 ...

  5. SQL Server常见数据类型介绍

    数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 1.整数类型 int 存储范围是-2,147,483,648到2,147,483 ...

  6. 由js apply与call方法想到的js数据类型(原始类型和引用类型)

    原文地址:由js apply与call方法想到的js数据类型(原始类型和引用类型) js的call方法与apply方法的区别在于第二个参数的不同,他们都有2个参数,第一个为对象(即需要用对象a继承b, ...

  7. python 数据类型 ----字典

    字典由一对key:value 组成的 python中常用且重量级的数据类型 1. key , keys, values 字典由一对key:value 组成的 python中常用且重量级的数据类型 1. ...

  8. SQL数据类型

    1.Character 字符串: 数据类型 描述 存储 char(n) 固定长度的字符串.最多8,000个字符. n varchar(n) 可变长度的字符串.最多8,000个字符.   varchar ...

  9. 跟着老男孩教育学Python开发【第二篇】:Python基本数据类型

    运算符 设定:a=10,b=20 . 算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**3 ...

随机推荐

  1. C#直接发送打印机命令到打印机及ZPL常用打印命令 - 条码打印机

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  2. wampserver里面出现403错误的问题解决方法

    今天再装wampserver的时候,不能进入localhost和phpmyadmin,提示403错误,我自己是win10系统,已通过以下方法解决了: 1.第一个问题,就是wampserver没有切换到 ...

  3. 深入浅出AQS之组件概览

    之前分析了AQS中的独占锁,共享锁,条件队列三大模块,现在从结构上来看看AQS各个组件的情况. 原文地址:http://www.jianshu.com/p/49b86f9cd7ab 深入浅出AQS之独 ...

  4. iptables使用实践

    1.iptables 本质上是一组规则,报文从端口接收到之后,按照规则的顺序进行匹配,一旦匹配上则执行动作,后续就不再匹配. 2.为了体现出优先级,iptable分为4个表,5个链,如下: 优先级顺序 ...

  5. Android之View绘制流程源码分析

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 对于稍有自定义View经验的安卓开发者来说,onMeasure,onLayout,onDraw这三个方法都不会陌生,起码多少都有所接触吧. 在安卓中 ...

  6. Ubuntu安装Java8和Java9

    前言 系统:Ubuntu 16.04 软件: Java8, Java9 Tips: Java 9 的代码由于提供了新特性,所以有些代码并不支持向后兼容.也就是说,用 Java 9 写的代码,有可能在 ...

  7. 关于string.h中字符串的操作

     string.h中字符操作的函数 注意:**对字符数组的多次操作需要进行赋初值.或者善于用memset()函数进行清空数组的操作.**     否则容易出现错误. string.h文件中函数的用法加 ...

  8. Xamarin.Forms 开发IOS、Android、UWP应用

    C#语言特点,简单.快速.高效.本次我们通过C#以及Xaml来做移动开发. 1.开发工具visual studio 2015或visual studio 2017.当然visual studio 20 ...

  9. Ubuntu15.04 网站服务器环境搭建,php/html/css等学习环境搭建教程

    ---恢复内容开始--- 本文部分参考自:http://www.cnblogs.com/emouse/archive/2013/06/07/3124009.html 原文中存在少量错误,已改正. 首先 ...

  10. 测试中出现ERROR StatusLogger No log4j2 configuration file

    概述 在hibernate框架搭建完成用log4j2进行测试时,总是出现ERROR StatusLogger No log4j2 configuration file found. Using def ...