Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

c - So what does "return 0" actually mean?

I'm pretty proficient in PHP, but I've started dabbling with C. I've seen the code

return 0;

at the end of functions that don't return a value. This isn't used in PHP, because if a function is doesn't have a return, a value NULL is automatically returned.

All I'm asking is, in simple English, what does the return 0 actually do? Is it like PHP, where it returns its argument as the value of the function call? Is it just good practice?

I know this question has been asked many times before, but I'm asking it from the point of view of a PHP developer. The answers google throws up haven't been that concise.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is it like php, where it returns its argument as the value of the function call? Is it just good practise?

Yes, PHP and many other languages borrowed the return keyword from 'C'. And in all the languages, the return keyword has the same function - to return from the function. Anything that follows return keyword is the value that is returned to the caller.

Is it a good practise? Yes and No. Not all functions should return a value. And quite a few in the standard library even, do not return any value. Hence their return type is void.

But main function should return 0(also EXIT_SUCCESS) to identify that the program has executed successfully. And -1 otherwise (also EXIT_FAILURE)

EDIT: (Thanks to @KeithThompson):

EXIT_FAILURE is implementation defined. 1 is a common value of EXIT_FAILURE but the whole point is, you need not know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...