有符号整型:
无符号整型:
注意:
- 无符号数据表示数量,只有正值
- unsigned无符号标识不会改变数据类型的字节大小
- 无符号型数据打印要将之前的%d,全部替换成%u,如果在vs中没有注意转换,将无符号型用%d输出,那么编译器会做优化,将无符号型按有符号型进行输出,优化的前提是不写成:
unsigned int a = -10u;
在数据后面加了u,如果前面写了负号就会报错,因为明确了这是一个无符号整型
如果用%u输出一个负号整型,会出现乱码
- 有符号型前面一般不写signed
- 在定义变量时,一般会省去后面的d和u:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test()
{
unsigned int a = 10u; //简写成unsigned int a=10;
//注意:这里不能用hu
unsigned short a1 = 10u; //简写成unsigned short a1=10;
unsigned long a2 = 10lu; //简写成unsigned long a2=10;
unsigned long long a3 = 10llu; //简写成unsigned long long a3=10;
printf("int a=%u\n", a);
//这里打印short短整型要用hu
printf("short a1=%hu\n", a1);
printf("long a2=%lu\n", a2);
printf("long long a3=%llu\n", a3);
}
int main()
{
test();
return 0;
}
`
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test()
{
int a = 10; //简写成int a=10;
//注意:这里不能用hu
short a1 = 10; //简写成 short a1=10;
long a2 = 10l; //简写成 long a2=10;
long long a3 = 10ll; //简写成 long long a3=10;
printf("int a=%d\n", a);
//这里打印short短整型要用hd
printf("short a1=%hd\n", a1);
printf("long a2=%ld\n", a2);
printf("long long a3=%lld\n", a3);
}
int main()
{
test();
return 0;
}
注意:sizeof()的返回值是:
size_t 等价于unsigned int
接收sizeof的返回值要用%u