Originally I didn’t plan to answer, but after reading vczh’s answer upstairs, I don’t agree with it, so I’ll express my opinion. Please point out any mistakes.
Because this program needs to pass in 2 parameters, the next 2 if语句 are used to read these 2 parameters. The first parameter of all programs when running is the name of the executable file of the program itself, so in order to ensure that the program passes in 2 parameters, it is necessary to determine whether the value of argc is equal to 3. If you don’t judge in advance, wrong data may be read in the next two if statements (that is, the wild pointer data mentioned by vczh)
It is a good habit to output normal information and error information to "standard output" and "error output" respectively, so that users can easily distinguish between normal output and error output, such as Redirect to different log file
For a program with only one thread, using exit and return in main has the same effect, both of which end the program. But if it is not in the main function, or the program has multiple threads opened, then return cannot achieve the purpose of exiting the program. So it is a good habit to exit the program using exit. First, it looks clear. exit You can tell at a glance that it is used to exit; second, it can ensure that the program can be exited correctly under any circumstances, such as when multi-threading is turned on or it is not in main as I mentioned earlier. situation in the function.
The last point is the biggest difference between my views and vczh’s. In my opinion, when your intention is to actively and definitely exit the program, use exit instead of return. In other cases, use return.
I think your teacher’s program is well written. The reason why the last sentence is return is because the program has been executed to the end and it’s time for the main method to “return”. The purpose of this code is to emphasize that main方法正常结束了, not 我要在这里退出程序, so return (normal return) is used here instead of exit (the emphasis is 我要让程序退出) - although the two have the same effect.
The following answers are excerpted from: What is the difference between return and exit? Thank you
What is the difference between return and exit?
exit() is a function that ends a process. It will delete the memory space used by the process and return error information to the parent process. The wait system call in the parent process will receive this return information.
return returns the function value, which is the keyword
In the main function we usually use return (0); to return a value.
But this is limited to non-void situations, that is, void main().
exit() is usually used in a subroutine to terminate the program. After use, the program automatically ends and jumps to the operating system.
But when exit is used in main, the value returned is valid regardless of whether main is defined as void, and exit does not need to consider the type. exit(1) is equivalent to return (1)
exit(0); //Normal exit Non-0 means abnormal exit Numbers 0, 1, -1 will be written into the environment variable ERRORLEVEL, and other programs can use this to determine the program end status. Generally, 0 is normal push, other numbers are abnormal, and the corresponding errors can be specified by yourself.
1. What is the function of the code in the dotted box? The teacher said it is to avoid String = NULL? Don't understand.
is to check the command line input. In fact, it is not String = NULL, but you will read a wild pointer from argv.
2. Why use fprintf and stderr in parentheses? If there is no special function, then I understand that the teacher just wants to demonstrate the class content...
stderr is used to output error information. This is useful when other programs start your program and then read your output without displaying it on the screen. In fact, this is an early method of exchanging data across programs and has long been outdated.
3. Why are exit() and return used interchangeably here, and what are their respective purposes?
From a practical point of view, return can also be used. Exit is a crude method of ending a process and is not recommended.
Originally I didn’t plan to answer, but after reading
vczh
’s answer upstairs, I don’t agree with it, so I’ll express my opinion. Please point out any mistakes.Because this program needs to pass in 2 parameters, the next 2
if语句
are used to read these 2 parameters. The first parameter of all programs when running is the name of the executable file of the program itself, so in order to ensure that the program passes in 2 parameters, it is necessary to determine whether the value ofargc
is equal to 3. If you don’t judge in advance, wrong data may be read in the next two if statements (that is, the wild pointer data mentioned byvczh
)It is a good habit to output normal information and error information to "standard output" and "error output" respectively, so that users can easily distinguish between normal output and error output, such as Redirect to different log file
For a program with only one thread, using
exit
andreturn
in main has the same effect, both of which end the program. But if it is not in the main function, or the program has multiple threads opened, thenreturn
cannot achieve the purpose of exiting the program. So it is a good habit to exit the program usingexit
. First, it looks clear.exit
You can tell at a glance that it is used to exit; second, it can ensure that the program can be exited correctly under any circumstances, such as when multi-threading is turned on or it is not in main as I mentioned earlier. situation in the function.The last point is the biggest difference between my views and
vczh
’s. In my opinion, when your intention is to actively and definitely exit the program, useexit
instead ofreturn
. In other cases, usereturn
.I think your teacher’s program is well written. The reason why the last sentence is
return
is because the program has been executed to the end and it’s time for the main method to “return”. The purpose of this code is to emphasize thatmain方法正常结束了
, not我要在这里退出程序
, soreturn
(normal return) is used here instead ofexit
(the emphasis is我要让程序退出
) - although the two have the same effect.The following answers are excerpted from:
What is the difference between return and exit? Thank you
What is the difference between return and exit?
exit() is a function
that ends a process. It will delete the memory space used by the process and return error information to the parent process. The wait system call in the parent process will receive this return information.
return returns the function value, which is the keyword
In the main function we usually use return (0); to return a value.
But this is limited to non-void situations, that is, void main().
exit() is usually used in a subroutine to terminate the program. After use, the program automatically ends and jumps to the operating system.
But when exit is used in main, the value returned is valid regardless of whether main is defined as void, and exit does not need to consider the type. exit(1) is equivalent to return (1)
exit(0); //Normal exit
Non-0 means abnormal exit
Numbers 0, 1, -1 will be written into the environment variable ERRORLEVEL, and other programs can use this to determine the program end status.
Generally, 0 is normal push, other numbers are abnormal, and the corresponding errors can be specified by yourself.
is to check the command line input. In fact, it is not String = NULL, but you will read a wild pointer from argv.
stderr is used to output error information. This is useful when other programs start your program and then read your output without displaying it on the screen. In fact, this is an early method of exchanging data across programs and has long been outdated.
From a practical point of view, return can also be used. Exit is a crude method of ending a process and is not recommended.