All types in C language are as follows:
1. Integer Types (int, short, long, long long)
1.1 Signed Integer
The signed integer data types typically include four types: int, short, long, and long long. Since they are signed types, they are prefixed with signed, although this is usually omitted. Therefore, when you write int in the code, it represents a signed type.
(1) int Type
The size of the data type is 4 bytes, and the range of values it can represent is
-2^(32-1) – 2^(32-1)-1 (i.e., -2147483648 ~ 2147483647)
The print format is %d, and the usage format is int name = value;
(2) short Type
The size of the data type is 2 bytes, and the range of values it can represent is
-2^(16-1) – 2^(16-1)-1 (i.e., -32768 ~ 32767)
The print format is %hd, and the usage format is short name = value;
(3) long Type
The size of the data type is 4 bytes, and the range of values it can represent is
-2^(32-1) – 2^(32-1)-1 (i.e., -2147483648 ~ 2147483647)
The print format is %ld, and the usage format is long name = value;
(4) long long Type
The size of the data type is 8 bytes, and the range of values it can represent is
-2^(63) ~ 2^(63)-1 (this number is sufficiently large)
The print format is %lld, and the usage format is long long name = value;
1.2 Unsigned Integer
Unsigned numbers are represented with unsigned, indicating only the quantity of data without direction (no positive or negative, and the highest bit of an unsigned number is not a sign bit but part of the number itself; unsigned numbers cannot be negative).
(1) unsigned int Type
The size of the data type is 4 bytes, and the range of values it can represent is
0 – 2^(32)-1 (i.e., 0 ~ 4294967295)
The print format is %u, and the usage format is unsigned int name = value;
(2) unsigned short Type
The size of the data type is 2 bytes, and the range of values it can represent is
0 ~ 2^16 -1 (i.e., 0 ~ 65535)
The print format is %hu, and the usage format is unsigned short name = value;
(3) unsigned long Type
The size of the data type is 4 bytes, and the range of values it can represent is
0 – 2^(32)-1 (i.e., 0 ~ 4294967295)
The print format is %lu, and the usage format is unsigned long name = value;
(4) unsigned long long Type
The size of the data type is 8 bytes, and the range of values it can represent is
0 ~ 2^63-1
The print format is %llu, and the usage format 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; // Shortened to unsigned int a = 10;
unsigned short b = 20u; // Shortened to 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;
}
Results, note the return value symbol
2. Character Type (char)
Character type variables are used to store a single character, represented by char in C language, where each character variable occupies 1 byte. When assigning a value to a character variable, it must be enclosed in a pair of single quotes (‘ ‘). Character variables do not actually store the character itself in the variable’s memory unit, but rather store the ASCII code corresponding to that character in the storage unit. The essence of char is a 1-byte sized integer.
The format specifier for char is: %c
The range of values is:
Signed: -2^(8-1) – 2^(8-1)-1 (i.e., -128 ~ 127)
Unsigned: 0 ~ 2^8 -1 (i.e., 0 ~ 255)
Common ASCII codes are:
‘A’: 65
‘a’: 97 (the difference between upper and lower case is 32)
‘0’: 48
‘\n’: 10
‘\0’: 0
Demonstration of 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;
}
Running results:
Verify the difference in ASCII codes between upper and lower case 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’ value = %d\n”, ‘\n’);
system(“pause”);
return EXIT_SUCCESS;
}
Result:
3. Floating-point Types (float, double)
(1) Single Precision Floating-point Type (float)
The size of single precision floating-point type is 4 bytes.
float v1 = 4.345;
unsigned float v1 = 4.345; unsigned float data
The format specifier is: %f, defaulting to 6 decimal places.
(2) Double Precision Floating-point Type (double)
The size of 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 output means: display 8 digits (including the decimal point), padding with 0 if less than 8 digits, and keeping 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;
}
Result: