Preface
What should I do if I still cannot meet my needs after learning variables, for example, I want to output characters. So, this article mainly introduces data types from language.
Why data type
This problem is actually very easy to understand. For example, if I declare a character variable, I am unlikely to use it to do the four arithmetic (+, -, *, /). Therefore, when we use different data types, we need computers to process them in different ways.
When declaring variables, we apply for space from the memory. How does the system tell the memory how much space we need?
Basic data types
Numerical type: including:
integer {
integer (int), short integer (short), long integer (long) }
floating point {
single precision (float), double precision (double) } .
**Character type**: char
Practice hands
Different data types occupy different space in the memory. When we declare variables, we must specify the data type so that the system knows how much memory we have in the memory.
#include<stdio.h>
int main()
{
int m,n=2; //声明整型
float f1 = 1.4,f2; //声明单精度浮点型
double fl1 = 2.0,fl2; //声明双精度浮点
long l = 10; //声明长整型
char ch;
printf("依次输入一个整数,小数,小数,以空格隔开\n");
scanf("&d,%f,%lf",&m,&f2,&fl2);
getchar();
scanf("%c",&ch);
printf("整数:%d,单精度:%f,双精度:%lf,字符型:%c",m,f2,fl2,ch);
}
Focus
The above example contains the input and output of various basic data types, excluding the long type (you can check it if you are interested).
Use single-precision floating point for input. %f
Double precision: use both for %lf
output %f
. There are other formatted output such as %m.nf
m represents the width of the output, n represents the precision of the output.
There are two ways to enter the string ch = getchar()
and scanf("%c",&ch)
careful may find why there is a getchar statement before entering ch it? This is because when a character is input, a space also represents a character. Therefore, this sentence is to buffer the space entered before we enter the character.
Write at the end
In order to take care of Xiaobai, some details are not explained. Those who are interested can check the information by themselves, which can deepen the impression. This series of articles only contains basic knowledge, and in order to save time, there are relatively few examples given. Therefore, I hope that interested students can get started. In view of the current popularity of win10, the editor codeBlocks is recommended