简体   繁体   中英

using void with printf function

#include <stdio.h>

char char1;     /* first character */
char char2;     /* second character */
char char3;     /* third character */

main()
{

  char1 = 'A';
  char2 = 'B';
  char3 = 'C';
  (void)printf("%c%c%c reversed is %c%c%c\n",
        char1, char2, char3,
        char3, char2, char1);
  return (0);
}

Why we use void with the printf function? what are use of void with the printf function?

printf returns a value which most people don't use most of the time. Some tools (eg 'lint') warn about this unused return value, and a common way of suppressing this warning is to add the (void) cast.

It does nothing in terms of execution, it's just a way of telling your tools that you know that you're happy to ignore the return value.

That looks like very old C code.

The (void) cast before printf is used to signify that you're ignoring printf 's return value. It's not necessary.

(void)foo() means that we ignore the return value of the call to foo (in this case - printf ).

Depends on the compiler and the warning level set, ignoring the return value will trigger a warning. Sometimes people use "treat warnings as errors" option of the compiler, and then in order for the code to compile, the return value of called functions must be either used or explicitly ignored, as in this case.

This is not required in a usual setting, only if the settings are very strict.

I would say it is the coder's effort to remind anyone reading the code that the return value from printf is being ignored. It's not at all necessary for any technical reason.

This is type casting the return value of Printf to nothing. This could be use to get rid of a compiler warning or it could simply make the reader of the code know that the writer knew that the return was never going to be used.

main() ?

which data type is?

you need select the data type:

int main() {
   return 0
}

printf is void funcion...

Code correctly:

#include <stdio.h>

char char1;     /* first character */
char char2;     /* second character */
char char3;     /* third character */

int main() {
    char1 = 'A';
    char2 = 'B';
    char3 = 'C';
    printf("%c%c%c reversed is %c%c%c\n",
    char1, char2, char3,
    char3, char2, char1);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM