学校C语言课的PPT,关于 Command line arguments的,讲的很粗,例题直接看不懂。
直接上图片了,因为有老师的标注,截图比较完整。
我的疑问在于,
1.虚线框内的代码有什么作用,老师说为了避免String = NULL?并不理解。
2.为什么用fprintf并且括号内是stderr?如果没有特殊作用,那我就理解为老师只是为了演示课堂内容了……
3.这里为什么exit()和return混用,各有什么目的?
新入门,求助各位前辈,谢谢!
附上图的原题:
天蓬老师2017-04-17 13:11:50
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.
ringa_lee2017-04-17 13:11:50
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.
天蓬老师2017-04-17 13:11:50
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.