Variables of C language entry notes

After understanding Hello World, our joy will not stop at outputting Hello World. If we want to achieve more advanced functions, then we have to learn more new knowledge. This time introduce the variables in C language.

Computer memory

Before talking about variables, let's learn the basics of computers. Don't focus on it, just understand it, so I'll just cut it short here.

Modern computers include a central processing unit (CPU), memory, input and output devices. We only understand the operating memory (RAM) in the memory. The computer's processor is mainly used for calculations, but the calculations are stored in the memory. The memory can be divided into an area to store specific data. If we want to obtain this area, then we have to give this area a name.

variable

We first apply for a variable in C language

#inclued<stdio.h>

int main()
{
    int a;   //声明int型变量
    a = 10;   //给a赋值
    printf("%d",a);   //输出变量a
    return 0;
}

In the above program, we declare a intvariable a, the value of 10 assigned to the variable a, the final output variable a. We have already understood the output function, and now we need to understand more.

int a; This statement means that I have applied for an area in the memory to store an int-type data, where a represents this area.

a = 10; This sentence is to store the number 10 in this area called a

printf("%d\n",a);But there may be many students here who have been stumped. printfWhy is this Hello Worlddifferent from that of? In fact, it is still it, but the output variable should use its own format. %It is a conversion specifier, which indicates the type of data to be output. %d means to output an int variable. \nIs the escape character, which corresponds to the carriage return character on the keyboard.

scanf formatted input function

After studying variables, we found that we still need to write the output content into the variable in advance. Can we change the output content while the program is running? Of course it is possible. Variables can replace the data they store. We just need to tell the program to change to that value, which requires the program to be able to read the user's input. At this time, the scanffunction will be used . Let's look at an example first.

#include<stdio.h>

int main()
{
    int a;
    scanf("%d",&a);
    printf("我刚刚输入了%d\n",a);
}

scanfFunctions and printfare included in the header file stdio.h, which is why we included this header file at the beginning of the program. scanfThe format of the function is printfsimilar. The only difficulty is &a, what does this &mean? & Is the address character , and its function is to get the actual address of the area a. For example, if your home is in Beijing, we can call it Beijing, the capital, or xxx home, but these all represent this address. The actual address is the east longitude and north latitude of the earth. The same is true for variables, whose actual address in memory we need &to get through .

The article is only for learning and communication. Students who have errors or have questions can leave a message.

Guess you like

Origin blog.csdn.net/weixin_36382492/article/details/80631640