C multi line macro: do/while(0) vs scope block
http://docs.google.com/View?docid=ajbgz6fp3pjh_2dwwwwt#_38239340844832237
It is not about optimization.
The whole idea of using 'do/while' version
is to make a macro which will
expand into a regular statement, not into a
compound statement. This is
done in order to make the use of function-style
macros uniform with the
use of ordinary functions in all
contexts.
Consider the following code sketch
if
(<condition>)
foo(a);
else
bar(a);
where 'foo' and 'bar'
are ordinary functions. Now imagine that you'd
like to replace function 'foo'
with a macro of the above nature
if
(<condition>)
CALL_FUNCS(a);
else
bar(a);
Now, if your
macro is defined in accordance with the second approach
(just '{' and '}')
the code will no longer compile, because the 'true'
branch of 'i' is now
represented by a compound statement. And when you
put a ';' after this
compound statement, you finished the whole 'if'
statement, thus orphaning the
'else' branch (hence the compilation error).
One way to correct this
problem is to remember not to put ';' after
macro "invocations"
if
(<condition>)
CALL_FUNCS(a)
else
bar(a);
This will compile
and work as expected, but this is not uniform. The
more elegant solution is
to make sure that macro expand into a regular
statement, not into a compound
one. One way to achieve that is to define
the macro as follows
#define
CALL_FUNCS(x) \
do { \
func1(x); \
func2(x); \
func3(x); \
}
while (0)
Now this code
if
(<condition>)
CALL_FUNCS(a);
else
bar(a);
will compile
without any problems.
However, note the small but important difference
between my definition
of 'CALL_FUNCS' and the first version in your message.
I didn't put a
';' after '} while (0)'. Putting a ';' at the end of that
definition
would immediately defeat the entire point of using 'do/while' and
make
that macro pretty much equivalent to the compound-statement version.
相关文档:
给我们上ASP.NET的老师在讲解C#的时候,总是要询问我们C#和C/C++间的不同之处,或者一些细节问题,但是平时不怎么注意,还真把我们给问到了!!
一.不经过中间变量怎样交换两个数据 int a=25;b=10;
解法: a=a+b;//35
b=a-b;//25
  ......
在编程的过程中,文件的操作是一个经常用到的问题,在C++Builder中,可以使用多种方法对文件操作,下面我就按以下几个部分对此作详细介绍,就是:
1、基于C的文件操作;
2、基于C++的文件操作;
3、基于WINAPI的文件操作;
4、基于BCB库的文件操作;
5、特殊文件的操作。
壹、基于C的文件操作
在ANSI C中 ......
3: int main()
4: {
5: int i = 1,j;
6: union test{
7: int m;
8: ......
在Linux C编程中使用Unicode和UTF-8
目前各种Linux发行版都支持UTF-8编码,当前系统的语言和字符编码设置保存在一些环境变量中,可以通过locale命令查看:
$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US ......
本文旨在说明如何利用Flash和C语言制作BS模式下的实时数据动态曲线图,现在流行的实时数据曲线图,大都是采用了CS模式的开发语言,BS模式的虽有一些实例,比如google上的股市曲线图,但其实现的方式和流程在网上很少能见到。
其运行时的界面如下,数据实时更新,曲线图从左往右动态移动:
下面先讲在实现过程中的工作环境 ......