• 【C/C++】uin8_t uint16_t uint32_t uint64_t数据类型解析


    作者:柒号华仔
    个人主页:欢迎访问我的主页
    个人信条:星光不问赶路人,岁月不负有心人。
    个人方向:专注于5G领域,同时兼顾其他网络协议,编解码协议,C/C++,linux等,感兴趣的小伙伴可以关注我,一起交流。


    1.基本数据类型

    C语言中常用的基本数据类型有:char、short、int、long、float、double:

    类型定义大小取值范围小数点范围输出格式
    char字符型1byte-128- 127%c
    short短整型2byte-32768~32767%d
    int整型4byte-2 147 483 648~2 147 483 647%d
    long长整型32位4byte
    64位8byte
    -2 147 483 648~2 147 483 647%ld
    long long64位长整型8byte-9223372036854775808~
    9223372036854775807
    %lld
    float单精度浮点型4byte-3.4E+38 ~ 3.4E+386位%f
    double双精度浮点型8byte-1.7E-308~1.7E+30812位%lf

    2.无符号数据类型

    在上述整型数据类型前加上unsigned,则为无符号类型:

    类型定义大小取值范围小数点范围输出格式
    unsigned char无符号字符型1byte0~255%u
    unsigned short无符号短整型2byte0~65535%u
    unsigned int无符号整型4byte0~4294967295%u
    unsigned long无符号长整型32位4byte
    64位8byte
    0~4294967295%lu
    unsigned long long无符号64位长整型8byte0~18446744073709551615%Ilu

    3.整型数据别名

    为了代码编写和阅读方便,我们通常会将数据类型名称声明为简写方式,即uin8_t/uint16_t/uint32_t/uint64_t,或者UINT8/UINT16/UINT32/UINT64。因此uin*_t并不是新的数据类型,而是通过关键字typedef声明后的数据形式。

    typedef char        INT8;
    typedef short       INT16;
    typedef int         INT32;
    typedef long long   INT64;
    
    typedef unsigned char        UINT8;
    typedef unsigned short       UINT16;
    typedef unsigned int         UINT32;
    typedef unsigned long long   UINT64;
    
    typedef unsigned char        uint8_t;
    typedef unsigned short       uint16_t;
    typedef unsigned int         uint32_t;
    typedef unsigned long long   uint64_t;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4.代码验证

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef unsigned char        uint8_t;
    typedef unsigned short       uint16_t;
    typedef unsigned int        uint32_t;
    typedef unsigned long long   uint64_t;
    
    int main(void)
    {
        char cData = 'a';
        short sData = 1;
        int iData = 2;
        long lData = 3;
        long long llData = 4;
        uint8_t ucData = 5;
        uint16_t usData = 6;
        uint32_t uiData = 7;
        uint64_t ullData = 8;
    
        printf(" cData = %c, sizeof(char) = %lu\n", cData, sizeof(cData));
        printf(" sData = %d, sizeof(short) = %lu\n", sData, sizeof(sData));
        printf(" iData = %d, sizeof(int) = %lu\n", iData, sizeof(iData));
        printf(" lData = %ld, sizeof(long) = %lu\n", lData, sizeof(lData));
        printf(" llData = %lld, sizeof(long long) = %lu\n", llData, sizeof(llData));
        printf(" ucData = %u, sizeof(uint8_t) = %lu\n", ucData, sizeof(ucData));
        printf(" usData = %u, sizeof(uint16_t) = %lu\n", usData, sizeof(usData));
        printf(" uiData = %u, sizeof(uint32_t) = %lu\n", uiData, sizeof(uiData));
        printf(" ulData = %llu, sizeof(uint64_t) = %lu\n", ullData, sizeof(ullData));
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    运行结果:

    在这里插入图片描述

  • 相关阅读:
    华能 + Alluxio | 数字化浪潮下跨地域数据联邦访问与分析
    数据⼀致性模型有哪些?
    threejs中各种的对像详细介绍
    Orcad Schematic常用功能
    webserver(二)
    Linux网络编程学习笔记(TCP)
    [iOS]App Store Connect添加银行卡时的CNAPS代码查询
    HTML5期末大作业:基于HTML+CSS+JavaScript实现中国风文化传媒企业官网源码
    掌控变量(C语言变量初步详解)
    林沛满---一个面试建议
  • 原文地址:https://blog.csdn.net/NoBack7/article/details/125581353