dushaoshuai / dushaoshuai.github.io

https://www.shuai.host
0 stars 0 forks source link

C 数据类型 #2

Open dushaoshuai opened 2 years ago

dushaoshuai commented 2 years ago

基本数据类型

type storage (bytes) format specifier (base 10, 8, 16) constant suffix
short 2 %hd %ho %hx
unsigned short 2 %hu
int 4 %d %o %x
unsigned 4 %u
long 8 %ld %lo %lx l (lowercase L) or L
unsigned long 8 %lu ul, LU ...
long long 8 %lld %llo %llx ll or LL
unsigned long long 8 %llu ull, LLu ...

进制

base constant prefix example
8 0 020
10 16
16 0x or 0X 0x10 or 0X10
// bases.c -- prints 16 in decimal, octal, and hex

#include <stdio.h>

int main(void) {
    int x = 16;

    printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
    printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x);
    printf("dec = %d; octal = %#o; hex = %#X\n", x, x, x);

    return 0;
}

// Output:
// dec = 16; octal = 20; hex = 10
// dec = 16; octal = 020; hex = 0x10
// dec = 16; octal = 020; hex = 0X10