Super detailed explanation of data types in C language

Posted by MerlinJR on Sat, 08 Jan 2022 03:44:07 +0100

Super detailed explanation of data types in C language

1, Integer (int, short, long, long)

1. Signed integer
Signed integer data types usually include int, short, long and long. Because they are signed types, they should be preceded by signed, but they are usually omitted. That is to say, typing int directly in the code means that they are signed types.

(1) int type
The size of the data type is 4 bytes, and the range of values that can be represented is
-2 ^ (32-1) – 2 ^ (32-1) - 1 (i.e. - 2147483648 ~ 2147483647)
The print type is% d, using the format int name = value;

(2) short type
The size of the data type is 2 bytes, and the range of values that can be represented is
-2 ^ (16-1) – 2 (16-1) - 1 (i.e. - 32768 ~ 32767)
The print type is% hd, and the format used is short name = value;

(3) long type
The size of the data type is 4 bytes, and the range of values that can be represented is
-2 ^ (32-1) – 2 ^ (32-1) - 1 (i.e. - 2147483648 ~ 2147483647)
The print type is% ld, using the format int name = value;

(4) long long type
The size of the data type is 8 bytes, and the range of values that can be represented is
-2 ^ (63) ~ 2 ^ (63) - 1 (this number is large enough)
The print type is% lld, and the format is long. Long name = value;

2. Unsigned integer
Unsigned numbers are represented by unsigned. They only represent the amount of data and have no direction (no positive or negative). The highest bit of an unsigned number is not a sign bit, but a part of the number. An unsigned number cannot be negative.

(1) unsigned int type
The size of the data type is 4 bytes, and the range of values that can be represented is
0 – 2 ^ (32) - 1 (i.e. 0 ~ 4294967295)
The print type is% u, and the format used is unsigned int name = value;

(2) unsigned short type
The size of the data type is 2 bytes, and the range of values that can be represented is
0 ~ 2 ^ 8 - 1 (i.e. 0 ~ 65535)
The print type is% hu, the format used is unsigned short, name = value;

(3) unsigned long type
The size of the data type is 4 bytes, and the range of values that can be represented is
0 – 2 ^ (32) - 1 (i.e. 0 ~ 4294967295)
The print type is% lu, and the format used is unsigned long name = value;

(4) Unsigned long type
The size of the data type is 8 bytes, and the range of values that can be represented is
0~2^63-1
The print type is% llu, and the format used is unsigned long long name = value;

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main0401(void)
{
	size_t var = 10;
	printf("var = %u\n", var);

	unsigned int a = 10u;  // Abbreviated as unsigned int a = 10;
	unsigned short b = 20u;// Abbreviated as unsigned short b = 20;
	unsigned long c = 30Lu; 
	unsigned long long d = 40LLu;

	printf("unsigned int Type data value:%u\n", a);
	printf("unsigned short Type data value:%hu\n", b);
	printf("unsigned long Type data value:%lu\n", c);
	printf("unsigned long long Type data value:%llu\n", d);

	system("pause");

	return EXIT_SUCCESS;
}

As a result, note the return value symbol

2, Character type (char)

Character variables are used to store a single character, which is represented by char in C language. Each character variable will occupy 1 byte. When assigning a value to a character variable, you need to enclose the characters with a pair of single quotation marks (') in English half width format. In fact, the character variable does not put the character itself into the memory unit of the variable, but puts the ASCII code corresponding to the character into the storage unit of the variable. Char is essentially a 1-byte integer.

The format matching character (print format) of char is:% c

The numerical representation range is:
Signed: - 2 ^ (8-1) – 2 (8-1) - 1 (i.e. - 128 ~ 127)
Unsigned: 0 ~ 2 ^ 8 - 1 (i.e. 0 ~ 255)

The commonly used Classl codes are:
'A': 65

'a': 97 (case difference 32)

'0': 48

'\n':10

'\0': 0

Demo character type:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
	char ch = 'A';  // 65

	//printf("1 ch = %c\n", ch);
	printf("1 ch = %d\n", ch);

	ch = 'm'; //

	//printf("2 ch = %c\n", ch);
	printf("2 ch = %d\n", ch);

	//ch = 97;
	ch = 'a';	// 97

	//printf("3 ch = %c\n", ch);
	printf("3 ch = %d\n", ch);

	system("pause");
	return EXIT_SUCCESS;
}

Operation results:

Verify whether the difference between upper and lower case Classl codes is 32:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
	char ch = 'M';
	char var = '5';

	printf("ch = %c\n", ch + 32);
	printf("var = %c\n", var + 4);

	printf("'\\n\'The value of is=%d\n", '\n');

	system("pause");
	return EXIT_SUCCESS;
}

The result is:

3, Floating point type (float, double)

(1) Single precision floating point (float)
The size of a single precision floating point is 4 bytes
float v1 = 4.345;
unsigned float v1 = 4.345; Unsigned float data
The format matching character is:% f, and 6 decimal places are reserved by default.

(2) Double (double)
The size of a double precision floating-point type is 8 bytes
double v2 = 5.678;
unsigned double v2 = 5.678; Unsigned double data

printf("n = %08.3f\n", n);
The meaning of output is: 8 digits (including decimal point) are displayed, and less than 8 digits are filled with 0. And keep 3 decimal places. Round the fourth digit.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
	float m = 3.145;
	double n = 4.566545;

	printf("m = %08.2f\n", m);
	printf("n = %08.3lf\n", n);

	system("pause");
	return EXIT_SUCCESS;
}

The result is:

Topics: C