c 解決無法列印uint8 t 型別變數的問題

2022-09-29 06:33:09 字數 3146 閱讀 9826

將uint8_t 轉化為unsigned 型別

使用一元運算子+(和- 運算子對應)

測試**如下

#include

#include

#include

int main()

執行結果如下

可見使用+運算子的原理也是進行型別轉換(把uint8_t 轉為 int)

補充知識:c 語言printf列印各種資料型別的方法(u8/s8/u16/s16.../u64/double/float)(全)

首先必須知道u8,s8等資料型別的定義:

typedef signed char s8;

typedef unsigned char u8;

typedef signed short s16;

typedef unsigned short u16;

typedef signed int s32;

typedef unsigned int u32;

typedef signed long long s64;

typedef unsigned long long u64;

與體系結構相關的,定義在include/linux/type.**件中:

/* bsd */

typedef unsign程式設計客棧ed char u_char;

typedef unsigned short u_short;

typedef unsigned int u_int;

typedef unsigned long u_long;

/* sysv */

typedef unsigned char unchar;

typedef unsigned short ushort;

typedef unsigned int uint;

typedef unsigned long ulong;

#ifndef __bit_types_defined__

#define __bit_types_defined__

typedef __u8 u_int8_t;

typedef __s8 int8_t;

typedef __u16 u_int16_t;

typedef __s16 int16_t;

typedef __u32 u_int32_t;

typedef __s32 int32_t;

#endif /* !(__bit_types_defined__) */

typedef __u8 uint8_t;

typedef __u16 uint16_t;

typedef __u32 uint32_t;

#if defined(__gnuc__)

typedef __u64 uint64_t;

typedef __u64 u_int64_t;

typedef __s64 int64_t;

對於各種資料型別的列印方式總結如下如下:

資料型別

列印格式

u8%d

s8%d

u16%d or %hu

s16%d or %hd

u32%u

s32%d

u64%llu

s64%lld

int%d

unsigned int

%ushort int

%d or %hd

long

%ldunsigned long

%lulong long

%lld

unsigned long long

%llu

char

%cchar *

%sbool (#define stdbool.h)

%dunsigned int/int------>十六進製制

%0xunsigned long/long---->十六進製制

%0lx

long long/unsigned long long ----->十六進製制

%0llx

unsigned int/int------>八進位制

%0ounsigned long/long---->八進位制

%0lo

long long/unsigned long long ----->八進位制

%0llo

float

%fdouble

www.cppcns.com %f or %lf

科學技術型別(必須轉化為double型別)

%e限制輸出字段寬度

%x.yf (x:整數長度,y:小數點長度)

待解問題,在linux kernel裡面也有使用bool來定義變數,檢視code,定義如下:

typedef _bool bool;

但是並沒有真正找到具體定義在何處,待解。

下面是stdbool.h的source code:

程式設計客棧

#define _stdbool_h

#ifndef __cplusplus

#define bool _bool

#define true 1

#define false 0

#else /* __cplusplus */

/* supporti _bool in c++ is a gcc extension. */

#define _bool bool

#if __cplusplus < 201103l

/* defining these macros in c++98 is a gcc exten程式設計客棧sion. */

#define bool bool

#define false false

#define true true

#endif

#endif /* __cplusplus */

/* signal that all the definitions are present. */

#define __bool_true_false_are_defined 1

#endif /* stdbool.h */

也大致解釋了linux kernel bool type了。

本文標題: c++ 解決無法列印uint8_t 型別變數的問題

本文位址:

解決無法刪除檔案 rm rf

使用lnmp或者wdcp建立的站點後刪除站點目錄無法刪除,甚至已經加了root許可權.解決辦法 執行 chattr i home wwwroot yoursite user.ini 路徑不同 你的檔名 yoursite 在進行刪除 chattr命令 有時候你發現用root許可權都不能修改某個檔案,大...

ubuntu 解決「無法獲得鎖

ubuntu系統的命令視窗下用apt get install安裝軟體的時會出現以下錯誤 無法獲得鎖 var lib dpkg lock open 11 資源暫時不可用 無法鎖定管理目錄 var lib dpkg 是否有其他程序正占用它?最可能的原因為 apt get程序並沒有結束而導致的!此時在命令...

laragon解決無法使用Memcached的問題

本地開發環境使用的是laragon整合環境,因為業務需要,所以要同時使用 memcached 和 redis 如何配置redis在上回已經說過了,這回說一下如何配置 memcached 有幾個問題需要知道 1.實際上 laragon是支出兩個快取同時執行的 為什麼出現報錯是因為缺少 dll檔案 2....