"A Basic C Language Introductory Course"-(3) Easy to understand the first C language program

1. Learning objectives

  1. Understand the general structure of C language code
  2. Understand the concept of functions
  3. Understand how to use the printf function
  4. Understand the concept of header files
  5. Understand how to use the system function

table of Contents

The first article: (1) Get rid of the misunderstanding of learning. The
second article: ( 2) C language is not so difficult. Simple development will show you the process

2. A brief understanding of the basic structure of C language programs

In the study of the previous section, I copied a section of HelloWorld code and ran the program. Next, learn the structure of C language code under normal circumstances through the code in the previous section.

2.1 Understanding the C language code writing area
As shown in the figure below, the area indicated by the purple box is the work area, and we write code in this area. But for beginners, this method is too free to write. The most suitable way for beginners is to specify a local area and write basic code in that area.
Insert picture description here

In the previous section, we modify the printf("")content within double quotes, in order to achieve a custom display content at runtime. Let's check printf("")the area where this is located int main(){}within the curly braces ({ }) in the code. We call the content of the code in the main function within the curly braces.

Here we have a new term "function". The concept of "function" will be explained next. Now we put aside the function and know that the code of printf is written in a pair of curly braces. Then we stipulate here that the next program will be written in this pair of curly braces.

In the above code, the code inside the curly braces uses a semicolon (;) as the end sign after each sentence. The semicolon functions in the code the same as writing articles in Chinese, indicating the end of this sentence. When writing the code In general, use a semicolon to indicate the end. It means that there are many different situations at the end, which we will explain in detail in later courses.

Note : When writing code, all key symbols and punctuation need to be entered in English.

Three, understand the use of functions

The unfamiliar term "function" appeared in the last small point. In programming, a general function is not a function in exponential learning, but refers to a function, a method that can help us achieve a certain result.

3.1 Understanding the concept of function
In programming, a function can be understood as a "tool" that can be used directly to complete a task. Functions can be implemented by themselves, which will be explained in later courses.

The function we can use an example to illustrate very well: before the washing machine appeared, our laundry can generally be divided into washing water, soap jade and other detergents, beating or scrubbing, rinsing, and twisting dry; through the above The steps to complete the washing of clothes. When the washing machine appeared, the washing machine automatically completed the processes of fetching water, putting soap jade and other detergents, beating or scrubbing, rinsing, and wringing; when we wash clothes, we don’t need such a tedious process, just This task can be accomplished by putting the clothes in the washing machine.

The same is true for functions in C language programming. Functions represent a method or function. When we need to use the function, we can complete the task by writing the name of the function and passing some required content.

3.2 Understanding how to use the printf function
In our previous section, we modified the content in the printf("") double quotation marks, and the content is displayed when the program is running. Knowing from the running results, printf is most likely the key code used to display the content of the program at runtime. If you have this idea, congratulations, you have some thinking about writing programs. The function of printf("") is to display the content in the double quotation marks in parentheses when the program is running. When writing the program, if you fill in the double quotation marks, what value will be output if you don’t specify characters. .

printf is a function that displays some values ​​while the program is running. The values ​​we fill in are called parameters. The general way to use a function is to add a parenthesis to the function name. If you need to pass in a string value as a parameter, then use double quotation marks when passing the string. (Later courses will learn multiple types as different manifestations of parameters)

In the C language, the functions that can be used directly are called system functions, such as printf. These system functions are equivalent to some tools and are classified in some "toolboxes".

3.3 Understanding the concept of header files
At the end of section 3.2, we learned that system functions are classified in some "toolboxes", and these toolboxes are header files. As shown in the figure below, the header file is the two lines of code at the top of the code.
Insert picture description here

As shown in the purple box above, the position is the import code of the header file. How to introduce? Let me tell you next.
The imported code is as follows:

#include<stdio.h>
#include<stdlib.h>

In the above code, stdio and stdlib are the header file names, among which the .h of stdio.h is the suffix of the file, and h indicates that this file is a header file. We can understand that this .h is the mark of the current file, indicating the type.
#include<> indicates which header file to include. The code acts like a hand. You tell it what toolbox to take, and it will help you go to the designated place to get the toolbox. So where is this designated place? The system defaults this location to the include directory under the devc software installation location.
Insert picture description here
This is the role of the environment. If you write a C language program without a supported library, then using the printf function will have no effect, unless you manually implement the entire process yourself, which is very tedious and vague for novices.
Of course, we need to pay attention when importing header files. It is useless to import at will. Suppose I want to import a header file named qqq. #include goes to the current directory to find the file and finds that it cannot be found. An error will occur at this time prompt.
Insert picture description here
And it will prompt an error, telling you that the file was not found in this directory.
Insert picture description here

So the question is, now we review why we need to introduce the header file?
It was said before that the function exists in the header file, so there is a logic "using a function requires introducing the function". The function printf we use exists in the stdio header file, so we use include in the header to introduce it, and printf can be used after the introduction. Do not believe? Then we first introduce and delete the header file, and then click Compile to see if there will be an error. The compile button is as follows:
Insert picture description here

The code after deletion is as follows:

#include<stdlib.h>
void main(){
    
    
	printf("你好 世界!");
	system ("pause");
}

The result after compilation:
Insert picture description here

A warning is prompted in the prompt window below, indicating that the current system function printf is directly used improperly. (Because there will be some professional terminology directly translated, so I changed the expression) As the current C language standard is changing over time, it has led to warnings. In the older standard, errors will appear directly. Not the warning now. For good habits, we add the introduction #include<stdio.h>:

#include<stdio.h>
#include<stdlib.h>
void main(){
    
    
	printf("你好 世界!");
	system ("pause");
}

Compile again without warning.
Insert picture description here

3.4 Understanding the use of the system function In the
previous section, we learned about the role and method of the introduction of header files, and then we will learn about another function system. The system function is used to execute system commands. This system refers to the DOS system, and the simple understanding is the small black box that appears when the program is running.

Why does the system call this DOS black box command here? What is this order? what's the effect?
We first look at the system in the code. The purpose of system ("pause");this code is to stop when the program runs to this point and wait for the next key to continue running. So why do you do this? Because without adding this piece of code, the program we are writing will flash by.

We can do an experiment and delete the line of system code. The final code is as follows:

#include<stdio.h>
#include<stdlib.h>
void main(){
    
    
	printf("你好 世界!");
}

Then click the button to compile and run the program.
Insert picture description here
Then a black frame appeared, and there was no flashing phenomenon.
Insert picture description here
Oh, did I roll over? Actually not.
It will stop here because we have passed the devc software to run the program, then it will be automatically added and stopped. What if I don’t run the program from the devc software? As we learned in the previous section, when compiled and run, an executable program will be generated, which can be run directly by double-clicking.
Insert picture description here
Let's go to the save location of the previously set file, find the file and double-click to run. At this time there will be a flash phenomenon. So students, still have to add stop code.

#include<stdio.h>
#include<stdlib.h>
void main(){
    
    
	printf("你好 世界!");
	system ("pause");
}

Then explain the system ("");usage of the function. The system function is not only a pause function, it can use DOS system commands, but the command needs to be passed. Since learning DOS system commands will increase unnecessary learning time, we focus on C language learning. Here we only need to know that a pause will be passed in and the pause will be executed. The pause command is a string of characters, called a string. A function does not need to pass in parameters in the form of the function name plus parentheses system(), but here you need to pass in a command pause to achieve pause, then write it as system ("pause"), and the code needs to use a semicolon to indicate the end, then it is finally written system ("pause");. Note that punctuation must be entered under the English input method.

Four, main

In the helloWord program, void main is not explained. Because the content involves too many other content, it will not be explained in detail here, just understand that main is the entry point of the C language program.

When we write a program, we must give the computer an initial entry point to the computer and tell it where our program starts. This is main. Just like a school, there is a gate, you take the admission notice and see a gate, know that this is the entrance to the school. The same is true for computers. This main is an entry we stipulated in the C language.

Please follow the official account for IT original animation, learning materials, and original tutorials.
Insert picture description here

Five, summary

Through the above description and explanation, we have learned the following points:

  1. Beginners start to write code first to write code from the curly braces after main
  2. Understand the concept of function as the realization of a function
  3. Understand the use of the printf function, pass in the value in the function to display it when the program is running
  4. Know the location of the header file and its functions are stored in the header file
  5. Understand that the stystem function is a function to call DOS system commands
  6. Understand the role of main is to represent the code entry

Guess you like

Origin blog.csdn.net/A757291228/article/details/108894723