The difference between unsigned integer and signed integer, and the use of unsigned integer

Signed Integer:
Insert picture description here
Unsigned Integer:
Insert picture description here
Note:

  1. Unsigned data means quantity, only positive value
  2. unsigned unsigned identification does not change the byte size of the data type
  3. To print unsigned data, replace all the previous %d with %u. If you do not pay attention to the conversion in vs and output the unsigned type with %d, then the compiler will optimize it and press the unsigned type as a signed type. For output, the premise of optimization is not to write:
unsigned int a = -10u;

Insert picture description here
After the data is added u, if a negative sign is written in front, an error will be reported, because it is clear that this is an unsigned integer.
Insert picture description here
If you use %u to output a negative integer, garbled characters will appear
Insert picture description here
Insert picture description here

  1. Signed type is generally not written before signed
  2. When defining variables, the following d and u are generally omitted:
#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;
}

`Insert picture description here

Insert picture description here
Insert picture description here

#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;
}

Insert picture description here

Note: The return value of sizeof() is:

Insert picture description here
size_t is equivalent to unsigned int to
receive the return value of sizeof to use %u

Guess you like

Origin blog.csdn.net/m0_53157173/article/details/114096771