简体   繁体   English

使用 void 与 printf function

[英]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?为什么我们在 printf function 中使用 void? what are use of void with the printf function? printf function 的 void 有什么用?

printf returns a value which most people don't use most of the time. printf 返回一个大多数人大部分时间不使用的值。 Some tools (eg 'lint') warn about this unused return value, and a common way of suppressing this warning is to add the (void) cast.一些工具(例如'lint')会警告这个未使用的返回值,抑制这个警告的常用方法是添加 (void) 强制转换。

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.这看起来像是非常古老的C代码。

The (void) cast before printf is used to signify that you're ignoring printf 's return value. printf之前的(void)转换用于表示您忽略printf的返回值。 It's not necessary.这不是必需的。

(void)foo() means that we ignore the return value of the call to foo (in this case - printf ). (void)foo()意味着我们忽略对foo的调用的返回值(在本例中 - 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.我想说这是编码人员的努力,以提醒任何阅读代码的人printf的返回值被忽略。 It's not at all necessary for any technical reason.出于任何技术原因,这根本没有必要。

This is type casting the return value of Printf to nothing.这是将 Printf 的返回值强制转换为空的类型。 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() ? main()

which data type is?哪种数据类型是?

you need select the data type:你需要 select 数据类型:

int main() {
   return 0
}

printf is void funcion... printf是无效函数...

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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