0x01 C language – write the first hello world

Time:2021-4-18

Learning every programming language starts with “Hello world!” which seems to be an unwritten rule in the programming world.

In this article, I will teach you to write a program that can output “Hello world!”.

Create solutions and projects in vs2019:

Run vs2019, select “create new project” – > “blank solution” – > “next step” – > fill in the project name, which is the name of the solution. I write “C”_ Learn “- > fill in the path, which can be understood as” work area “. It is suggested to create a new folder to store your various solutions – >” create “.

Next, in the “solution manager” on the right, right-click the solution – > “add” – > “new project” – > “empty project” – > “next step” – > fill in the project name. On one side is the name of your output program. Here I fill in “0x01helloworld” – > fill in the location. Generally, there is no need to change – > “create”.

At this point, the solution and project have been created. But we still can’t start to write the code, because we still need to do some configuration and add source code files:

In the right “solution manager”, right-click the project you just created – > “properties” – > “configuration properties” – > “advanced” – > set “character set” to “use multi byte character set” – > “general” – > “output directory” – > “Browse” – > create a new folder in your solution folder to store compiled programs – > “OK”.

Note: the purpose of modifying the output directory here is to keep the output directory consistent, because when you switch the options of “x86”, “x64”, “debug” and “release” in VS, the output directory will change, which will take up more space this time, and may lead to some unexpected problems. Of course, there’s no big problem if you don’t want to.

Then in the “solution manager” on the right, right-click the “source file” option – > “add” – > “new item” – > “C + + file (. CPP)” – > set the name. What I wrote is“ entry.cpp ”->Generally, there is no need to change the position – > “add”.

Now, we can really start writing programs.

Sample program:

1 #include //Include a header
 2 #include 
 3 
 4 //Entry of the program
 5 int main(int argc, char** argv)
 6 {
 7     printf("Hello world!");//Print a String on the screen
 8     system("pause");//Pause the program
 9     return 0;//Return a value to the system
10 }

After inputting these codes in the compiler, click the “compile” button, an executable file (. Exe) will be generated in the folder where the project is located. Double click to run it.

Note: in VS, you can directly press Ctrl + F5 to run.

For beginners, there must be a lot of things they don’t understand about this code. But let’s run the program first and see what it looks like. I’ll explain the code one by one.

Analysis:

1:

#include //Include a header
#include

What does this sentence mean? Literally, include means “include” in English. So, the function of this sentence is to tell the compiler to say a wordHeader fileIncluded in our code. The header file usually ends with (. H). In fact, it’s similar to our source code file (. C), and it’s all written with code. So this sentence can be said to be the code written by others for our own use. Let’s talk about the knowledge of include first, and then go into details when talking about precompiling.

So what is stdio. H? In fact, it is the header file we mentioned above. Stdio is the abbreviation of standard input and output, which means standard input and output. So, only by using it can we do input and output operations. In this case, a string of words is displayed on the screen.

Another windows. H provides some functions to be used in Windows system.

What do you mean by / /notes, that is / / and all the characters after it (but excluding the next line) will not affect the operation of the program. The purpose of comments is to improve the readability of the program. To put it bluntly, it is to let you and others understand your program as much as possible. There is another way to write notes in C language

1 #include /*Before me is the include sentence
2 I will not affect the program
3 After me is the program*/
4 int main(int argc, char **argv)
5 {...}

It means that comments (including / * and * /) are between / * and * /).

Note: in VS, after selecting the code, press Ctrl + K and then Ctrl + C to comment out the selected code; after selecting, press Ctrl + K and then Ctrl + u to comment out the selected code.

 2:

int main(int argc, char **argv)

Let’s not talk about this in detail. After talking about data types, let’s talk about argc and argv in detail. Now what we need to know is that this thing is calledMain function, is the entry of this program, that is, the place where a program starts. Therefore, this program will start from the first line of the main function.

3:

printf("Hello world!");//Print a String on the screen

Printf is a character output function, here it will be in bracketscharacter stringOutput to the screen.

be careful:In C language and C + +, a string must be enclosed in a pair of double quotation marks; only one character can be enclosed in a single quotation mark.

4:

system("pause");//Pause the program

The function of this sentence is to pause the program.

We know that after the program executes the output function, it will execute the next statement. If there is no such statement, then the following will be executed:

return 0;//Return a value to the system

This statement will return a value to the system, and tell the system: “I have finished running, can exit”, and then the system will shut down your program.

In this way, as soon as the string is displayed, the window is closed (strictly speaking, the program exits), and we have no time to see the output string. (computers run very fast.)

So, if we can stop the program temporarily, we will have time to see the output string.

Note: in some compilers, the program will pause before exiting. In those compilers, there is no need to pause the program artificially. However, to be on the safe side, I suggest you add this sentence.

 Tips:

If compilation failure is displayed, check the possible causes listed below:

1: Forget to type a semicolon;

2: Chinese characters are used;

3: It turns 0 into o;

4: There is no space in the space;

5: Brackets do not appear in pairs.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]