如何消除这个全局变量,沉默? [英] How to eliminate this global variable, silent?

查看:87
本文介绍了如何消除这个全局变量,沉默?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我控制是否打印消息时,我通常使用全局变量

" int silent"。当我设置-silent时在我的命令行中标记

参数,我在main.c中设置silent = 1.

我有很多函数可能会打印一些消息。


foo(...)

{

if(!silent)

printf(" Msg1 \\\
" ;);

}

foo2(...)

{

if(!silent)

printf(" Msg2 \ n");

}

等等...

是以上编码不好的做法?如何消除变量

沉默但达到同样的效果?

When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions that may print some messages.

foo(...)
{
if (!silent)
printf("Msg1\n");
}
foo2(...)
{
if (!silent)
printf("Msg2\n");
}
and so on...
Is the above bad coding practice? How to eliminate the variable
"silent" but achieve the same effect?

推荐答案

2008年4月11日星期五21:31:41 -0700(PDT),是********* @ gmail.com 写道:
On Fri, 11 Apr 2008 21:31:41 -0700 (PDT), is*********@gmail.com wrote:

>当我控制是否打印消息时,我通常使用全局变量
int silent。当我设置-silent时在我的命令行中标记
参数,我在main.c中设置了silent = 1.

我有很多函数可能会打印一些消息。

foo( ......)
{

if(!silent)

printf(" Msg1 \ n");
}

foo2(...)
{

if(!silent)

printf(" Msg2 \ n");
}

等......

以上编码错误的做法是什么?如何消除变量
沉默但达到同样的效果?
>When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions that may print some messages.

foo(...)
{
if (!silent)
printf("Msg1\n");
}
foo2(...)
{
if (!silent)
printf("Msg2\n");
}
and so on...
Is the above bad coding practice? How to eliminate the variable
"silent" but achieve the same effect?



如果你用于调试,那很好。

删除电子邮件的del

If you are using for debugging, it is fine.
Remove del for email


is*********@gmail.com 写道:

当我控制是否打印消息时,我通常使用全局变量

" int silent"。当我设置-silent时在我的命令行中标记

参数,我在main.c中设置silent = 1.


我有许多函数可能会打印一些消息。 />

foo(...)

{

if(!silent)

printf(" Msg1 \ n;

}


foo2(...)

{

if(!silent)

printf(" Msg2 \ n");

}


等等。 ..


以上是不良的编码习惯吗?
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions that may print some messages.

foo(...)
{
if (!silent)
printf("Msg1\n");
}
foo2(...)
{
if (!silent)
printf("Msg2\n");
}
and so on...
Is the above bad coding practice?



不,这对调试来说很常见。

No, it''s quite common for debugging.


如何消除变量无声"但达到同样的效果?
How to eliminate the variable "silent" but achieve the same effect?



为printf创建一个包装器并使用调试或生产链接

版本。如果你只想删除全局,请使用静态静态

并通过getSilent()函数公开它。


-

Ian Collins。

Create a wrapper for printf and link with either a debug or production
version. If you simply want to remove the global, make silent static
and expose it through a getSilent() function.

--
Ian Collins.


您好,

这是您的计划的修改。

----- ---------

foo()

{

#if!SILENT

printf (Msg1 \ n);

#endif


}


foo2()

{

#if!SILENT

printf(" Msg2 \ n");

#endif

}

main()

{

foo();

foo2() ;

}

--------------

命令:gcc -D SILENT foo.c
Hi,
Here is modification for your program.
--------------
foo()
{
#if !SILENT
printf("Msg1\n");
#endif

}

foo2()
{
#if !SILENT
printf("Msg2\n");
#endif
}
main()
{
foo();
foo2();
}
--------------
command: gcc -D SILENT foo.c


这篇关于如何消除这个全局变量,沉默?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆