Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of main function and Command Line parameters in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the C language main function and command line parameter instance analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this C language main function and command line parameter instance analysis article will have something to gain, let's take a look.

I. the concept of main function

The main function in C language is called the main function.

A C program is executed from the main function.

Second, the essence of main function.

Main function is a function called by the operating system

The operating system always takes the main function as the beginning of the application.

The operating system takes the return value of the main function as the exit status of the program.

Let's look at the return value of the main function:

A.c:

# include int main () {printf ("iTunm A!\ n"); return 0;}

B.c:

# include int main () {printf ("Ichimm B!\ n"); return 99;}

Compile code from the VS012 command line under windows. If you are not familiar with command line compilation, please look at the "command line", otherwise skip this section

Take B.C as an example. After compiling through the echo% ERRORLEVEL% command, you can see that the return value is 99.

Let's do another experiment. First, compile A, and then type B.exe & & A.exe. It is found that only Ichimm Bread is printed, because the operating system returns 99Magin99 after the end of the B.exe run. In the operating system's view, B.exe is not running and exiting normally, so the operating system will not run A.exe.

If you enter A.exe & & B.exe, the result is as follows. This is because the A.exe normally returns 0 to the operating system after the end of the run, so the B.exe will be executed.

Command Lin

Take the following code as an example

Test.h:

# ifndef _ TEST_H_ # define _ TEST_H_ # include class Test {public: Test (const char* s) {printf ("% s\ n", s);}; # endif

Test.cpp:

# include "test.h" Test T4 ("T4"); int main () {Test T5 ("T5");}

T1.cpp:

# include "test.h" Test T1 ("T1")

T2.cpp:

# include "test.h" Test T2 ("T2")

T3.cpp:

# include "test.h" Test T3 ("T3")

The first step is to open VS2012 and select tools-> Visual Studio Command prompt

The second step is to enter the folder where you need to compile the utility cd/d. (note that you need to enter / d to change the drive letter)

The file I want to compile is in the C:\ Users\ HuZeQiu\ Desktop\ demo folder.

Enter cd/d C:\ Users\ HuZeQiu\ Desktop\ demo and press enter as follows to go to the destination folder

The third step is to enter the cltest.cpp t2.cpp t1.cpp t3.cpp-otest.exe compiler. (the cl command is used to compile the program) press enter to start the compilation and generate the test.exe executable file, as follows:

Step 4: run test.exe, type test.exe directly, and you can see the running result.

The compiled folder is as follows:

Third, the parameters of main function

You can pass parameters to the main function when the program is executed

Common uses of gcc compilers:

Let's take a look at an example of the parameters of the main function:

# include int main (int argc, char* argv [], char* env []) {int iTuno; printf ("= Begin argv =\ n"); for (iTuno; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report