Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using void with printf function

Tags:

c

#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?

like image 928
user843378 Avatar asked Jul 13 '11 19:07

user843378


2 Answers

printf returns a value which most people don't use most of the time. Some tools (e.g. '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.

like image 165
Will Dean Avatar answered Nov 11 '22 09:11

Will Dean


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.

like image 36
Mat Avatar answered Nov 11 '22 10:11

Mat