易截截图软件、单文件、免安装、纯绿色、仅160KB

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.


相关文档:

C/C++ 文件读写操作总结(2)

五、文件定位
  和C的文件操作方式不同的是,C++ I/O系统管理两个与一个文件相联系的指针。一个是读指针,它说明输入操作在文件中的位置;另一个是写指针,它下次写操作的位置。每次执行输入或输出时,相应的指针自动变化。所以,C++的文件定位分为读位置和写位置的定位,对应的成员函数是 seekg()和 seekp(),seekg()是 ......

UVa Online Judge Volume C 题目和解答索引

UVa Online Judge - Volume C 题目和解答索引。前面为原题链接,后面为我的解答链接。
返回总目录
10003 - Cutting Sticks
Dynamic Programming
Solution
10004 - Bicoloring
Graph: BFS
Solution
10006 - Carmichael Numbers
Number Theory: Modulus
Solution
10010 - Where's Waldorf?
String
Solution ......

对C/C++可变参数表的深层探索


引言
  C/C++语言有一个不同于其它语言的特性,即其支持可变参数,典型的函数如printf、scanf等可以接受数量不定的参数。如:
printf ( "I love you" ); 
printf ( "%d", a );
printf ( "%d,%d", a, b );
  第一、二、三个printf分别接受1、2、3个参数,让我们看看printf函数的原型:
int printf ( const ......

ANSI C读书笔记系列之字符集篇 第四章 字节序

"endian"这个词出自<<格列佛游记>>,小人国的内战就源于吃鸡蛋时是究竟从大头(Big-Endian)敲开还是从小头(Little-Endian)敲开.我们一般将endian翻译成"字节序",将big endian和little endian称作"大端"和"小端".
在计算机科学领域中,字节序是指存放多字节数据的字节的顺序,典型的情况是整数在内存中的存放方式和 ......

用Flash与C语言制作工程实时数据动态曲线图

本文旨在说明如何利用Flash和C语言制作BS模式下的实时数据动态曲线图,现在流行的实时数据曲线图,大都是采用了CS模式的开发语言,BS模式的虽有一些实例,比如google上的股市曲线图,但其实现的方式和流程在网上很少能见到。
其运行时的界面如下,数据实时更新,曲线图从左往右动态移动:
下面先讲在实现过程中的工作环境 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号