Why do you need to add "(void)" before the function name when calling a function in some cases

 

We know that when defining a function , the "void" added to the function name means that the function has no return value. But what is the effect of adding "(void)" to the function name when calling it?

The most obvious point is that the program does not care what the return value is after calling the function , such as the function strcpy, we directly use the form of "strcpy(des_str, src_str);" to call. strcpy() prototype declaration: char *strcpy(char* dest, const char *src); . "(void)strcpy(des_str,src_str);" is really rare! Many times we don't care what kind of value the function returns at all.

The reason is that this way of writing is not aimed at people, nor at the compiler, but at the static code detection tool , which uses the function return value as a detection criterion. In some large companies, more emphasis is placed on code specification, if this item needs to be checked during code static detection. In this case, it is necessary to use the form of "(void)" before the called function (name) to tell the static code detection tool that the return value of the function is not not processed, but that it does not need to be processed ( The return value of the function), there is no need to perform this check on the code here. In fact, this is the same reason we use "#pragma warning (disable: XXXX)" in our code.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568916&siteId=291194637