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 do we use void with printf function? what is the use of void with printf function?

+4
source share
6 answers

printf returns a value that most people do not use most of the time. Some tools (such as "lint") warn of this unused return value, and a general way to suppress this warning is to add a (void) cast.

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

+11
source

It looks like very old C code.

The (void) listing before printf used to indicate that you are ignoring the return value of printf . It's not needed.

+2
source

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

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

This is not required in a normal setup, only if the settings are very strict.

+2
source

I would say that it is the efforts of the encoder to remind everyone who reads the code that the return value from printf ignored. This is not necessary for any technical reason.

0
source

This is the type that converts the return value of Printf to nothing. This can be useful to get rid of the compiler warning, or simply make the code reader know that the writer knew that the return would never be used.

0
source

main() ?

what type of data?

you need to choose the data type:

 int main() { return 0 } 

printf is an invalid function ...

The code is correct:

 #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; } 
-2
source

All Articles