After the last lesson, you now have a running C language program, related code, and development environment.
Perhaps you will have a little doubt as to why the establishment of main.cpp rather than main.c
In addition, what is printf? What is main? #include是什么?
Well, don't worry, I'll tell you all of these questions slowly.
Through the content of the last lesson, we know that the code needs a tool for programming programs.
in the Windows below, the tool vs provided; Linux below, the tool is called GCC .
This tool is what we'll refer to later, something called a compiler. Because it translates the source code into a executable file, it is called a compiler.
To illustrate the following, we first set up a header file:
Right-click the project and select add → new Item
Select the header file, file name input public.h
Then add a new line of code, as shown below (based on the class code):
#include <stdio.h>
#include "Public.h"
int Main (intargc,char* *argv,Char ** Env )
{
printf("Hello world!\n");
return0;
}
Let's take a look at the code below. Any c or C + + source code, basic will have two parts: header file Reference description and function implementation
here #include <stdio.h> is the header file Reference Description section. It tells the compiler that I will use some of the functions declared in the other files.
#include There are two ways, one is mentioned above, the other is to use double quotation marks as in the following code
#include "Public.h"
The difference between the two is that the former tells the compiler that the file I refer to is the header file of the library file (the detailed concept of the library file will say, here you can simply understand the basic functions provided by the operating system, to simplify software development.) ), start with the system header file, the latter tells the compiler that this header is my own definition, please look up the current directory first.
The compiler will then look for the header files according to your instructions.
int Main (intargc,char* *argv,Char ** Env )
This line of code tells the compiler that I have a function here called Main
It has three parameters argc argv and the Env , whose return value is an integer
One of the special highlights here is that Main function, which can only be implemented once in a program!
Because this function has a special function, it is the starting point of all code, the entry function of the program.
That is, when the program runs, the function is run first, not the other functions.
So this function can only be implemented once, otherwise there are multiple implementations, which is equivalent to a plurality of portals, you let the program from which to enter it?
After all, what about the other entrances from one in? So simply define a rule where a program can define only one entry. So everyone is good.
Maybe you have a textbook or something, and it's Main the function is given in this way:
int main (INTARGC)
int main (intargc,char* argv[])
int main (intargc,char*argv[],char*env[])
It's even like that.
int main ()
and such a
void Main ()
Well, don't get excited, they're all right, and I'm right.
As I said above, this function just tells the compiler that my portal is here
As to what kind of entrance this entrance is, compiling doesn't care too much.
but pay attention!! Although you can even define the main functionlike this:int main (void*param)
But when the system calls your portal, it will follownumber of parameters at startup (argc) The list of parameters at startup (argv) The list of environment variables at startup (Env) to pass parameters in order. From what you can seeparamat run time, the value that the system passes in is1. Indicates that only one parameter was used at startup. Other parameters you can not, but you cannot change the parameters given to you by the system.
so we define Main the function is best according to everyone's agreement
And more interestingly, although you may not want these three parameters, the system will still give you a complete disregard for your needs.
For example, I define as follows Main function
From the actual debugging situation, the system still passes the argv (that is, the startup parameter list) and the env (running environment list) to me. I just need some special tricks to see this happen.
So, when you define the main function, it's best to use my method:
int Main (intargc,char* *argv,Char ** Env )
Since you can not give up, in order to control the program, it is best to take-after all, whether you take or not to take, it is where, relative to the uncontrolled situation, in the hands of the heart more practical.
Well, here's a summary:
This section mainly studies the following points:
1 The composition of the source code has two parts: header file and function implementation
2 How to create a custom header file
3 access to the knowledge of the main function.
So now you're basically the first step to getting Started with C (or the C + + language).
Watch the 1th! No matter how complex or simple the code you touch, you can't leave this box (or it's either a useless program or a bad program).
For the rest of the main function, I'll go deeper in the next chapter and subsequent chapters, so don't forget it.
Zerglurker's C language Tutorial 002