Basic data types in C
- 24-07-2022
- Toanngo92
- 0 Comments
In the previous article, we learned how variables store values in computers and the syntax for declaring variables, in this article we will discuss data types in C language, recalling the knowledge of In the previous lesson, we understand that when declaring a data type for a variable to store, the computer will automatically allocate us a memory location ( memory address ) with a specified size ( memory size ) just enough to store the data type we want. And because the C programming language requires strict syntax, you cannot assign a value of this data type to a variable of another data type, once you declare any data type for the variable, you can only assign a value. value that data type for the variable.
Mục lục
Keywords (keywords) in variable declaration C
If you do not understand the concept of keywords, you can re-read the article C program structure, list of keywords (keywords) in C language to understand the concept again. Here, we talk about the keywords to determine the data type of the variable when declaring the variable. When declaring a variable, it is necessary to make sure that the variable name is not allowed to have the same keyword (the variable name can contain keywords but cannot be identical, for example:
int int; // lỗi int intNum; // không lỗi
List of basic data types in C
Common types of data stored in variables include:
- Numeric data
- Integer. Example (6 or -5)
- Real number. Example (5.20 or 7.234)
- Positive numbers
- Negative numbers
- Text data (e.g. "abcde" , "I'm trying")
- Logical data (true/false)
When data is stored in variables of different data types, they require different amounts of memory. The amount of memory assigned to a variable depends on the data type of the variable (similar to other programming languages). Syntax in C when using data types:
data_type variable_name //kiểudữliệu tên_biến data_type function_name(){ // do something here // kiểudữliệu tên_hàm }
In C language, there are 5 basic data types:
int – integer data type, used to represent integers in the wild (abbreviated interger ), occupies 2 bytes of memory. (Example 5 or 100 or 10000)
float, double – real number data type, representing decimal quantities as in math, float type occupies 4 bytes and can represent 6 numbers after decimal (eg 5.999999), and double type takes 8 bytes memory area, representing 10 digits after decimal
char – character data type, occupies 1 byte of memory representing single characters ( 'A','a','B','1','2' …). Note the character '1' is different from the integer 1
void – data type used for function declaration (not used for variable declaration), used to declare a function that does not return a value. Will be mentioned in the concept of functions.
Derived data type
A modifier used to combine with basic numeric data types to meet different situations (reduce memory size, increase memory size, increase variable representation), help make the program more optimized, save system memory, or represent numbers larger than the normal data type variables can represent. (Will be mentioned in the example below)
sign – data type with sign
unsigned – unsigned data type
long – allocates twice the memory size of the data type
short – allocates half the memory size of the data type
Declaration syntax:
sign int a; // khai báo biến integer a có dấu unsign int b; // khai báo biến interer b không dấu short float c; // khai báo biến float nhưng cấp phát 1 nửa kích thước vùng nhớ => 2 bytes unsign long double d; // khai báo biến double nhưng cấp phát gấp đôi kích thước vùng nhớ => 16 bytes và biểu diễn số thực không dấu
So why does each data type occupy a different amount of memory, to better understand this problem, we understand roughly as follows, in terms of computers, computers are designed from electronic components, including electronic components. consists of circuit boards and uses current to transmit data, and it only interprets 0.1 as the equivalent of switching current in electronic equipment. So, to represent any data type, the computer eventually converts it to binary. Therefore, every number, every character or string of characters when put into machine language to execute it will also convert to the value 0 and 1. If you want to learn more about the representation, read more articles write Integer representation in binary and binary operations .
For example, the number 31 when represented in binary will be 11111, with data type int , it will be allocated 2 bytes, equivalent to 16 bits and store the following sequence of numbers in RAM memory: 00000000011111 (the leading zero). first represent the quantity sign to determine whether the integer is positive or negative)
(000011111)₂ = (0 × 2⁸) + (0 × 2⁷) + (0 × 2⁶) + (0 × 2⁵) + (1 × 2⁴) + (1 × 2³) + (1 × 2²) + (1 × 2¹) + (1 × 2⁰) = (31)₁₀
Thus, with a regular int, it can only represent the largest number 32767 (0111111111111111) , and the smallest is -32767( 0111111111111111)
As for the character ( char ), it will be allocated 1 byte equivalent to 8bits, which is a character set in the ASCII encoding (American Information Interchange Code Standard), each of these characters can be completely represented in the form numbers and we can match directly in the code table. For example, for the character 'A', the decimal ascii code is 65 , store the following sequence in RAM: 01000001.
Similar to float and double , but these two data types are quite complicated to represent, so I don't mention it here, and we see, the more complex the data type, the more memory it takes to memory. store it.
Example table describing variable microdata
Datatypes | Device size (bit units) | Performance range |
---|---|---|
char | 8 | -128 -> 127 (ASCII code) |
unsigned | 8 | 0 -> 255 |
signed char | 8 | -128 -> 127 (ASCII code) |
int | 16 | -32,678 -> 32.767 |
unsigned int | 16 | 0 -> 65.535 |
signed int | 16 | -32,678 -> 32.767 |
short int | 8 | -128 -> 127 |
long int | 32 | -2,147,483,648 -> 2,147,483,647 |
Basic variable declaration example:
/* khai bao thu vien chuan */ #include<stdio.h> #include<stdlib.> /* end khai bao thu vien chuan */ int main() { // khai bao bien so nguyen int songuyen; // gan gia tri cho so nguyen bang 5 songuyen = 5; // khai bao va gan luon gia tri cho so nguyen int songuyencach2 = 6; // khai bao bien c la ki tu va gan bang ky tu 'A'; char c = 'A'; // khai bao bien so thuc va gan gia tri bang 5.1 float sothuc = 5.1; }
About sizeof() function
The sizeof function is a built-in function of the c programming language, used to measure the size of the data type, the return value of the function will be an integer, representing the size of the data type that we check. check.
The sizeof function syntax:
#include stdio.h #include stdlib.h int main(){ int a; a = sizeof(int); printf("%d",a); return 0; } // kết quả in ra trả về 2 với hệ 16 bit, 4 với hệ 32 và 64bit
Introduction to the printf() function
The printf function is a function in the input output system of the C language, this is a classic function, often used to print data from variables to the screen through the format specifier . We will talk more deeply in the article input, output. In this article, I will give an example of how to use the basic printf function and formatters so that everyone can understand the problem.
The printf() function syntax:
printf("%d",a); // in biến a dưới dạng só nguyên (decimal) printf("%f",b) // in biến dưới dạng số thực (float) printf("%c",c) // in biến dưới dạng ký tự (character)
Example of a combination of variable declaration and printf() function to print variable results to the screen
#include<stdio.h> #include<stdio.h> int main() { // khai bao bien so nguyen int songuyen; // gan gia tri cho so nguyen bang 5 songuyen = 5; // khai bao va gan luon gia tri cho so nguyen int songuyencach2 = 6; // khai bao bien kytu la ki tu va gan bang ky tu 'A'; char kytu = 'A'; // khai bao bien so thuc va gan gia tri bang 5.1 float sothuc = 5.1; printf("%d,%d,%c,%f",songuyen,songuyencach2,kytu,sothuc); }