The C language provides a rich set of data types that allow us to effectively handle various data.
C Language Data Types
├── Basic Types
│ ├── Integer
│ ├── Floating Point
│ └── Character
├── Enumeration Type
├── Void Type
├── Derived Types
│ ├── Pointer Type
│ ├── Array Type
│ ├── Structure Type
│ └── Union Type
└── Function Type
1. Integer Data Types
| Data Type | Bytes Used | Value Range | Format Specifier |
| char | 1 | –2⁷~2⁷-1 | %c or %d |
| unsigned char | 1 | 0~2⁸ | %c or %u |
| short | 2 | –2¹⁵~2¹⁵-1 | %hd |
| unsigned short | 2 | 0~2¹⁶ | %hu |
| int | 4 | –2³¹~2³¹-1 | %d |
| unsigned int | 4 | 0~2³² | %u |
| long | 4/8 | Depends on whether 4 bytes or 8 bytes are used | %ld |
| unsigned long | 4/8 | Depends on whether 4 bytes or 8 bytes are used | %lu |
| long long | 8 | –2⁶³~2⁶³-1 | %lld |
| unsigned long long | 8 | 0~2⁶⁴ | %llu |
You can use the sizeof function to check how many bytes a certain type occupies.
#include <stdio.h>
int main(int argc, char *argv[]) {
char ch = 61; // You can use either the variable name ch or the type name char in sizeof
// Both ways are valid
printf("ch size = %d\n", sizeof(char));
printf("char size = %d\n", sizeof(char));
printf("unsigned char size = %d\n", sizeof(unsigned char));
printf("short size = %d\n", sizeof(short));
printf("unsigned short size = %d\n", sizeof(unsigned short));
printf("int size = %d\n", sizeof(int));
printf("unsigned int size = %d\n", sizeof(unsigned int));
printf("long size = %d\n", sizeof(long));
printf("unsigned long size = %d\n", sizeof(unsigned long));
printf("long long size = %d\n", sizeof(long long));
printf("unsigned long long size = %d\n", sizeof(unsigned long long));
return 0;
}
Output:
ch size = 1
char size = 1
unsigned char size = 1
short size = 2
unsigned short size = 2
int size = 4
unsigned int size = 4
long size = 8
unsigned long size = 8
long long size = 8
unsigned long long size = 8
2. Floating Point Data Types
| Data Type | Bytes Used | Precision | Format Specifier |
| float | 4 | About 6-7 decimal places | %f |
| double | 8 | About 15-16 decimal places | %lf |
| long double | 10/16 | About 18-19 decimal places | %Lf |
Generally, using the float type is sufficient.
3. Character Data Types
char ch = 'A'; // Single character
char newline = '\n'; // Escape character
char null_char = '\0'; // Null character, string terminator
char ch2 = 61; // Can also be directly assigned a number
Correspondence between numbers and characters:
Dec Hex Char │ Dec Hex Char
──────────────────────────────────────┼─────────────────────
0 00 NUL '\0' (null character) │ 64 40 @
1 01 SOH (start of heading) │ 65 41 A
2 02 STX (start of text) │ 66 42 B
3 03 ETX (end of text) │ 67 43 C
4 04 EOT (end of transmission) │ 68 44 D
5 05 ENQ (enquiry) │ 69 45 E
6 06 ACK (acknowledge) │ 70 46 F
7 07 BEL '\a' (bell) │ 71 47 G
8 08 BS '\b' (backspace) │ 72 48 H
9 09 HT '\t' (horizontal tab) │ 73 49 I
10 0A LF '\n' (new line) │ 74 4A J
11 0B VT '\v' (vertical tab) │ 75 4B K
12 0C FF '\f' (form feed) │ 76 4C L
13 0D CR '\r' (carriage ret) │ 77 4D M
14 0E SO (shift out) │ 78 4E N
15 0F SI (shift in) │ 79 4F O
16 10 DLE (data link escape) │ 80 50 P
17 11 DC1 (device control 1) │ 81 51 Q
18 12 DC2 (device control 2) │ 82 52 R
19 13 DC3 (device control 3) │ 83 53 S
20 14 DC4 (device control 4) │ 84 54 T
21 15 NAK (negative ack.) │ 85 55 U
22 16 SYN (synchronous idle) │ 86 56 V
23 17 ETB (end of trans. blk) │ 87 57 W
24 18 CAN (cancel) │ 88 58 X
25 19 EM (end of medium) │ 89 59 Y
26 1A SUB (substitute) │ 90 5A Z
27 1B ESC (escape) │ 91 5B [
28 1C FS (file separator) │ 92 5C \ '\'
29 1D GS (group separator) │ 93 5D ]
30 1E RS (record separator) │ 94 5E ^
31 1F US (unit separator) │ 95 5F _
32 20 SPACE │ 96 60 `
33 21 ! │ 97 61 a
34 22 " │ 98 62 b
35 23 # │ 99 63 c
36 24 $ │ 100 64 d
37 25 % │ 101 65 e
38 26 & │ 102 66 f
39 27 ' │ 103 67 g
40 28 ( │ 104 68 h
41 29 ) │ 105 69 i
42 2A * │ 106 6A j
43 2B + │ 107 6B k
44 2C , │ 108 6C l
45 2D - │ 109 6D m
46 2E . │ 110 6E n
47 2F / │ 111 6F o
48 30 0 │ 112 70 p
49 31 1 │ 113 71 q
50 32 2 │ 114 72 r
51 33 3 │ 115 73 s
52 34 4 │ 116 74 t
53 35 5 │ 117 75 u
54 36 6 │ 118 76 v
55 37 7 │ 119 77 w
56 38 8 │ 120 78 x
57 39 9 │ 121 79 y
58 3A : │ 122 7A z
59 3B ; │ 123 7B {
60 3C < │ 124 7C |
61 3D = │ 125 7D }
62 3E > │ 126 7E ~
63 3F ? │ 127 7F DEL
#include <stdio.h>
int main(int argc, char *argv[]) {
char ch = 'A';
printf("ch = %d, %c\n", ch, ch);
return 0;
}
Output:
ch = 65, A
4. Enumeration Data Types
Enumeration types are an important data type in C language, used to define a set of named integer constants.
Basic Syntax:
enum EnumName {
Identifier1,
Identifier2,
// …
IdentifierN
};
#include<stdio.h>
// Define enumeration type, default values start from 0
enum Week{
MONDAY, // 0
TUESDAY, // 1
WEDNESDAY, // 2
THURSDAY, // 3
FRIDAY, // 4
SATURDAY, // 5
SUNDAY // 6
};
// Custom enumeration values can be defined
enum Color{
RED = 5,
GREEN, // If not specified, it will be the previous value + 1, which here is 6
BLUE = 10
};
intmain(int argc, char *argv[]){
// Declare enumeration variables
enum Color favorite_color = GREEN;
enum Week today = WEDNESDAY;
printf(“Favorite color: %d\n”, favorite_color); // Output: 6
printf(“Today is: %d\n”, today); // Output: 2
return0;
}
Favorite color: 6
Today is: 2