1. Learning Objectives

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

directory

Is C really hard? If you don’t see this picture, you can easily learn C by breaking it into parts. (4) Basic data types and variables of the language (5) Variables, constants and operations of the C language (6) Easy to understand the logical operations of the C language (7) : (11) C language pointer is originally like this: (11) C language custom function is really very simple twelfth chapter: (12) the original structure is such a thing :(13) socket server prepared

C language novice 100 error solutions

recommended

I am participating in 1024 activity, welcome everyone to like, favorites, comments on my dry goods articlesAn article to guide you from 0 to 1 to know how to build a website and complete the COMPILATION of CMS system

Welcome everyone to pay attention to the public account, the public account every 1024 and 1024 times will be lucky draw a mechanical keyboard + 2 IT books oh ~

Two, a simple understanding of the basic structure of C language program

In the previous section, you copied a piece of HelloWorld code and ran the program. Next, use the code in the previous section to learn the structure of C code in general.

2.1 Understand the coding area of C language

As shown in the figure below, the area indicated by the purple box is the workspace, where we write codes. However, this is too free for beginners to write, and the best way for beginners is to specify a local area in which to write the basic code.

In the previous section, we modified the content inside the printf(“”) double quotes to customize what the program displays at run time. Printf (“”) is inside the curly braces ({}) inside the int main(){} code. Inside this curly brace we call the content of the code inside main.

We have a new term, function, which we’ll talk about in a little bit, but let’s get rid of the function and see that printf is written inside a pair of curly braces. So we’re specifying that the next program will be written in this pair of curly braces.

In the above code, the code in curly braces ends with a semicolon (;) at the end of each sentence. As an end sign, a semicolon is used to indicate the end of a sentence in code, just as it is used to indicate the end of a sentence in Chinese. There are a lot of different cases of ending, and we’ll go into more detail later in the course.

Note: when writing code, all key punctuation marks need to be entered in The English state.

Three, understand the use of functions

In the last dot, the unfamiliar term “function” appeared. In programming, a function does not refer to a mathematical function. It refers to a function, a method that helps us achieve a result.

3.1 Understanding the Concept of Function In programming, a function can be understood as a “tool” that can be directly used to complete a task. Functions can be implemented on their own, as will be explained later in the course.

Function we can use a good example to illustrate: before the emergence of washing machines, our washing clothes can be divided into water, soap, such as detergent, beating or rubbing, rinsing water, twist dry; Go through the above steps to complete the laundry. When the washing machine appears, the washing machine automatically completed the water, such as the soap, beating or rubbing, rinsing water, twisting dry these processes; When we wash clothes, we don’t need such a tedious process, just need to put the clothes in the washing machine to complete the task.

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

In our previous section, we modified the contents of the printf(“”) double quotes to display when the program runs. From the results of the run, printf is most likely the key code for the program to display its content at run time. If you have this idea, congratulations, you have some programming mind. The purpose of printf(“”) is to display the contents of the double quotation marks inside the parentheses when the program is running. When you write a program, it will output whatever value you put inside the double quotation marks without specifying the character.

Printf is a function that displays values while the program is running. The values we fill in are called parameters. A function name is usually followed by parentheses. If you want to pass in a string value as an argument, use double quotation marks to pass in the string. (Later lessons will look at different representations of multiple types as parameters)

In C, the functions that can be used directly are called system functions, such as printf. These system functions are equivalent to some tools, and are stored in some “toolbox”.

3.3 Understand the concept of header files

At the end of Section 3.2, we learned that system function categories are stored in toolkits, which are header files. As shown below, the header file is the two lines in the header position of the code.

The position indicated by the purple box above is the lead-in code for the header file. How to introduce? Here’s what I’ll tell you. The introduction code is as follows:

#include<stdio.h>
#include<stdlib.h>
Copy the code

In the above code, stdio and stdlib are the header file names, where. H of stdio.h is the suffix of the file, and h indicates that the file is a header file. We can understand that.h is the current file flag, the type.

#include<> specifies which header file to include. This code acts like a hand: you tell it what toolbox to get, and it goes to the right place to get it for you. So where is this designated place? The default location is the include directory under the devC software installation location.



This is where the environment comes in. If you’re writing a C program and don’t have a supported library then using printf won’t do you any good unless you implement the whole process manually, which can be tedious and fuzzy for beginners.

If I want to import a header file named QQQ, #include will look for it in the current directory and find it is not there. This will cause an error message.



You will be prompted with an error telling you that the file was not found in that directory.

So the question is, now let’s review why did we introduce headers?

As stated earlier, functions exist in header files, so there is a logic that “using a function requires importing that function”. The function printf we are using exists in the stdio header file, so in the header we include the tool to use printf. Don’t believe it? So let’s delete the header file first, and then click Compile to see if we get an error. The compile button is as follows:

After deletion, the code is as follows:

#include<stdlib.h>
void main(a){
	printf("Hello world!);
	system ("pause");
}
Copy the code

Result after compilation:

Warning: Printf is not used properly. (Because the direct translation will have some technical terms, so I changed the expression.) As the current C language standard changes over time, so there is a warning. In the older standard, there will be a direct error, which is not the current warning. For good practice we’ll introduce #include

with:

#include<stdio.h>
#include<stdlib.h>
void main(a){
	printf("Hello world!);
	system ("pause");
}
Copy the code

No warning appears when compiling again.

In the previous section, we learned about the introduction and methods of the header file. In the next section, we learned about another function, system. The system function is used to execute system commands. This system refers to the DOS system, which is simply understood as the little black box that appears when the program is running.

Why does the system call the DOS little black box command? What is this command? What does it do? We first look at the system in the code is system (“pause”); This line of code stops the program at this point and waits for a key to be pressed to continue. So why do it? Because if you don’t add this piece of code, everything you’re writing will just flash by.

We can do an experiment and delete the system line, resulting in the following code:

#include<stdio.h>
#include<stdlib.h>
void main(a){
	printf("Hello world!);
}
Copy the code

Then click the compile run program button.



Then there is a black box, and there is no flash of the phenomenon.



Oh dear, did I turn over the car? Not really.

This will stop because we run the program through the devc software, which will automatically add stop. What if I don’t run the program from DevC? As we learned in the previous section, when compiled and run, an executable program is generated that can be run by double-clicking.



Let’s go to the file we set up earlier, find the file and double-click to run. At this time there will be a flash phenomenon. So, class, you still have to add a stop code.

#include<stdio.h>
#include<stdlib.h>
void main(a){
	printf("Hello world!);
	system ("pause");
}
Copy the code

Next, explain system (“”); Function usage. The system function is not just a pause function, it can use the DOS system command, but need to pass the command. Because learning DOS system commands will increase unnecessary learning time, we use C language learning mainly, here only need to know that a pause will be executed. The pause command is a string of characters called a string. A function that does not need to pass in parentheses system() is written as system(“pause”) if the function is paused by passing in a semicolon. . Note that punctuation marks must be entered in English.

Four, the main

Void main is still unexplained in the helloWord program. Because the content involves too much other content, I will not explain in detail here, just need to understand that main is the entrance of THE C language program.

When we write a program, we have to give the computer a starting point to tell it where to start our program, and that’s main. It’s like a school, there’s a gate, you take the letter of admission and you see a gate, and you know this is the entrance to the school. The same goes for computers. This main is an entry point that we specify in C.

IT original animation, learning materials, original tutorials, please pay attention to the public number.

Five, the summary

Through the above description and explanation, we know the following contents:

  1. Beginners start by writing code inside curly braces after main
  2. Understand the concept of a function as an implementation of a function
  3. Understand the use of printf function, in the function to pass the value can be displayed in the program run
  4. We know the location of the header file and the function stored in the header file
  5. Stystem functions are functions that call DOS system commands
  6. See that main is used to represent code entry

My blog is synchronized to tencent cloud + community, invite everyone to come together: cloud.tencent.com/developer/s…