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

Int mainargc,char * argv []) detailed explanation

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Argc is the total number of arguments on the command line

Argv [] is the argc parameter, where the 0th parameter is the full name of the program, and the subsequent parameter

The parameters entered by the user followed by the command line, such as:

Int main (int argc, char* argv [])

{

Int i

For (I = 0; itest)

At this point, the value of argc is 1, and the value of argv [0] is "test".

D:\ tc2 > test myarg1 myarg2

At this point, the value of argc is 3 argc [0] is "test", the value of argc [1] is "myarg1", and the value of argc [2] is "myarg2".

This thing is generally used to provide very important information for programs, such as data file names, and so on.

Such as: copy a.c b.txt

At this point, A.C and b.txt are the so-called "very important information". You can't copy these two files without specifying them.

When your program uses the parameters argc and argv, you can simply judge the value of argc to see if the program parameters meet the requirements

Char * envp []

This thing is relatively seldom used. It is used to get the environment variables of the system.

For example, under DOS, there is a PATH variable. When you type a command at the DOS prompt (of course, this command is not an internal command such as dir), DOS will first look for the command's execution file in the current directory. If it cannot be found, then go to the path defined by PATH to find it. If you find it, it will be executed. No Bad command or file name will be returned.

Type set at the DOS command prompt to view the environment variables for the system

Similarly, under UNIX or LINUX, there are system environment variables that are used more often than DOS. Such as the commonly used $PATH,$USER,$HOME and so on.

Envp holds all the environment variables. Its format is (under UNIX)

PATH=/usr/bin;/local/bin

HOME=/home/shuui

That is:

Environment variable name = value

It's probably the same under DOS.

Environment variables are generally used to provide additional information for programs. For example, you make a program that displays the contents of the text. You want to control the number of characters displayed on one line. You can define an environment variable yourself (under UNIX)

% setenv NUMBER = 10

Echo $NUMBER

ten

Then you can read this environment variable into the program. Then determine how many characters a line outputs based on its value. In this way, if you do not modify the environment variables, the number of characters displayed on a line will be different each time you execute the program.

Here is an example program

# include

Int main (int argc, char * argv [], char * envp [])

{

Int i

Printf ("You have inputed total% d argments\ n", argc)

For (iTun0; iargtest this is a test programe of main ()'s argments

You have inputed total 9 argments

Arg0: d:\ TC\ NONAME.EXE

Arg1: this

Arg2: is

Arg3: a

Arg4: test

Arg5: programe

Arg6: of

Arg7: main () 's

Arg8: argments

The follow is envp:

TMP=C:\ WINDOWS\ TEMP

TEMP=C:\ WINDOWS\ TEMP

PROMPT=$p$g

Winbootdir=C:\ WINDOWS

PATH=C:\ WINDOWS;C:\ WINDOWS\ COMMAND

COMSPEC=C:\ WINDOWS\ COMMAND.COM

SBPCI=C:\ SBPCI

Windir=C:\ WINDOWS

BLASTER=A220 I7 D1 H7 P330 T6

CMDLINE=noname this is a test programe of main ()'s argments

-

Command line parameters. Argc is the number of parameters, argv [] is the parameter, argv [0] is the file name, and argv [1] is the first parameter.

If your exe file name is: myprog.exe, then

Myprog 12 22 32

Then argv [0] = "myprog", argv [1] = "12", argv [2] = "22".

Exit () is the return code when the program exits. It can be received by other programs to determine whether to exit normally. For example, exit (- 1) thinks it exits abnormally.

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

Servers

Wechat

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

12
Report