In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "in the C language when the function is successfully executed return value is 1 or 0", in daily operation, I believe many people in the C language when the function is successfully executed return value is 1 or 0 on the question there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation method, hope to answer "in the C language when the function is successfully executed return value is 1 or 0" doubts helpful! Next, please follow the small series to learn together!
directory
1. Return value of C language function
2."Industry hidden rules"
There is only one possibility for function success, but there are many possibilities for function failure.
4. There is also a kind of "industry hidden rule" among C language programmers.
5 and finally
The content shared today is a discussion about whether the function executes successfully and returns 0 or 1 ~
Basically, no one will put a large piece of C language code into the main() function, it is better to divide the code into different functional modules as much as possible according to the principle of high reuse rate and low coupling, and package them into functions. C code is composed of many different combinations, so functions can be complex, and different inputs often produce different output results.
Different inputs often produce different outputs.
1. Return value of C language function
C language functions can express output results by returning values, for example, the return value of log() function will return different values according to different inputs.
For example, if we define a function myopen() to open a file, then this function can either successfully open the file or fail to open the file. In this case, we can distinguish between "success" and "failure" by returning a value.
Of course, myopen() can fail for many reasons, but that's not the focus of this article. What this article is more concerned with is what value means "success" and what value means "failure." According to C language syntax, 0 means false, non-zero (usually 1) means true, so does the function also use 0 return value to mean "failure" and 1 return value to mean "success"?
2."Industry hidden rules"
C language functions can of course use a return value of 0 for "failure" and a return value of 1 for "success." In fact, what kind of return value C language functions use to indicate success or failure is only an artificial convention, and the caller of the function can abide by this "convention."
C language also has "industry hidden rules"
However, for general functions, most C programmers often use a return value of 0 to indicate success and a non-zero value to indicate failure. Therefore, if we want the functions we define to be used happily by most people, it is better not to do the opposite, but to follow the "hidden rules of the industry."
On second thought, C functions use a return value of 0 to indicate success for a reason. More general C language functions do not necessarily have only two possible return values (success/failure), it may also return a return value corresponding to the cause of the error, in short, the function success has only one possibility, the function failure has many possibilities.
There is only one possibility for function success, but there are many possibilities for function failure.
Real numbers are either 0 or non-0, so it is better to think of 0 as a special "unique" number, using 0 as a "unique" return value to indicate a unique "success," and multiple non-zero return values to indicate failures for multiple reasons.
Of course, we can also say that "real numbers are either 1 or not 1," but this is obviously not an "unspoken rule of the industry."
For example, we can specify that myopen() returns-1 if it fails because the file or directory does not exist, and-2 if it fails because the permissions are insufficient. - 1 and-2 are both "non-zero," and success is the opposite of failure, i.e.,"non-zero." The myopen() function uses a return value of 0 to indicate that success is justified.
4. There is also a kind of "industry hidden rule" among C language programmers.
Of course, there is also a "hidden rule of the industry" among C programmers. If the function defined is a Boolean function, i.e. the return value is explicitly used like the bool keyword definition, or the function name is similar to is_true(), then obviously the C syntax should be followed, with a "true" value indicating success and a "false" value indicating failure.
if( is_true() ) printf("true\n");
Look at the above two lines of C code. Obviously, Boolean functions that follow C syntax are easier for programmers to write Boolean judgment class code.
5 and finally
This paper mainly discusses the problem of function return value in C programming. It can be seen that what kind of return value is used to indicate success and what kind of return value is used to indicate failure. In fact, it is only an artificial convention.
However, if we want the code we write to be used happily by most of our peers, it is best to follow the "unspoken rules of the industry."
Of course, if you want our C code to be more widely applicable, you can use the EXIT_SUCCESS and EXIT_FAILURE macros predefined in the standard header file.
At this point, the study of "whether the return value is 1 or 0 when the function is successfully executed in C language" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.