01_基本数据类型

基本数据类型

1、什么是数据类型?

数据类型可以理解为固定内存大小的别名
数据类型是创建变量的模子

2、什么是变量

变量是一段(具体)连续存储空间的别名
程序通过变量申请并命名存储空间
通过变量名可以使用存储空间

3、代码练习

(1)类型与变量的关系

#include <stdio.h>

int main()
{
    char c = 0;
    short s = 0;
    int i = 0;

    printf("sizeof(char) = %d, sizeof(c) = %d\n", sizeof(char), sizeof(c));
    printf("sizeof(short) = %d, sizeof(s) = %d\n", sizeof(short), sizeof(s));
    printf("sizeof(int) = %d, sizeof(i) = %d\n", sizeof(int), sizeof(i));

    return 0;
}

输出结果为:

(2)自定义类型与创建变量

#include <stdio.h>

typedef int INT32;
typedef unsigned char BYTE;
typedef struct _tag_ts
{
    BYTE b1;
    BYTE b2;
    short s;
    INT32 i;
} TS;

int main()
{
    INT32 i32;
    BYTE b;
    TS ts;

    printf("%d, %d\n", sizeof(INT32), sizeof(i32));
    printf("%d, %d\n", sizeof(BYTE), sizeof(b));
    printf("%d, %d\n", sizeof(TS), sizeof(ts));

    return 0;
}

输出结果为:

4、小结

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





猜你喜欢

转载自www.cnblogs.com/chen-ace/p/9839692.html
今日推荐