﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-心无尘土-随笔分类-标准C</title><link>http://www.cnitblog.com/Yama/category/1367.html</link><description>一段平凡的日子，一段流逝的岁月</description><language>zh-cn</language><lastBuildDate>Wed, 28 Sep 2011 00:38:47 GMT</lastBuildDate><pubDate>Wed, 28 Sep 2011 00:38:47 GMT</pubDate><ttl>60</ttl><item><title>comp.lang.c FAQ list精华整理 1.5/1.6 关于指针类型声明的问题</title><link>http://www.cnitblog.com/Yama/archive/2006/07/28/14333.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Fri, 28 Jul 2006 05:31:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2006/07/28/14333.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/14333.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2006/07/28/14333.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/14333.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/14333.html</trackback:ping><description><![CDATA[1.5 <font color="#ff0000">char* p1, p2; </font>来声明两个指针失败的原因。作成中。。。。。。。。。。。。。。。。。。<br /><br /><br />    结果是p1为指针，p2仅仅为char.<br />The <tt>*</tt> in a pointer declaration is not part of the base type;
it is part of the <a href="http://c-faq.com/sx1/index.html#declarator"><dfn>declarator</dfn></a>
containing the name being declared
(see question <a href="http://c-faq.com/decl/cdecl1.html">1.21</a>).
That is,


in C, the syntax and interpretation of a declaration is not really
<pre><i>type identifier</i> ;<br /></pre>
but rather
<pre><i>base_type thing_that_gives_base_type</i> ;<br /></pre>
where ``<i>thing_that_gives_base_type</i>''--the
<a href="http://c-faq.com/sx1/index.html#declarator"><dfn>declarator</dfn></a>--is
either a simple identifier,
or a notation like <tt>*p</tt> or <tt>a[10]</tt> or <tt>f()</tt>
indicating that the variable being declared is a pointer to,
array of, or function returning that <i>base_type</i>.
(Of course, more complicated declarators are possible as well.)
<p>In the declaration
as written in the question,
no matter what the whitespace
suggests,
the base type is <tt>char</tt> and
the first declarator is ``<tt>* p1</tt>'',
and since the declarator contains a <tt>*</tt>,
it declares <tt>p1</tt> as a pointer-to-<tt>char</tt>.
The declarator for <tt>p2</tt>, however,
contains nothing but <tt>p2</tt>,
so <tt>p2</tt> is declared as a plain <tt>char</tt>,
probably not what was intended.
To declare two pointers within the same declaration,
use
</p><pre>	char *p1, *p2;<br /></pre>
Since the <tt>*</tt> is part of the declarator,
it's best to use whitespace as shown;
writing <tt>char*</tt> invites mistakes and confusion.<br /><br /><h1>
comp.lang.c FAQ list
<font color="blue">·</font><!-- qtag -->Question 1.6
</h1><p><font color="blue" face="Helvetica" size="8"><b>Q:</b></font>
I'm trying to declare a pointer and allocate some space for it,
but it's
not working.
What's wrong with
this code?
</p><pre>char *p;<br />*p = malloc(10);</pre><hr /><p><font color="blue" face="Helvetica" size="8"><b>A:</b></font>
The pointer you declared is <tt>p</tt>,
not <tt>*p</tt>.
See question <a href="http://c-faq.com/ptrs/mimic.html">4.2</a>.
</p><br /><img src ="http://www.cnitblog.com/Yama/aggbug/14333.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2006-07-28 13:31 <a href="http://www.cnitblog.com/Yama/archive/2006/07/28/14333.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>comp.lang.c FAQ list精华整理 1.2 / 1.3/1.4 关于类型大小的讨论，具体编程关系不大，可看可不看</title><link>http://www.cnitblog.com/Yama/archive/2006/07/28/14330.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Fri, 28 Jul 2006 05:19:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2006/07/28/14330.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/14330.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2006/07/28/14330.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/14330.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/14330.html</trackback:ping><description><![CDATA[1.2 没有强制定义标准类型大小，这样可以适应不同机器并且大部分情况下没有必要用具体大小，还可以隐藏执行细节。（文档加上个人理解,不对勿怪-yama）<br />Why aren't the sizes of the standard types precisely defined?<span style="font-weight: bold;"><br /><br /></span>Though C is considered relatively low-level
as high-level languages go,
it does take the position
that the exact size of
an object
(i.e. in bits)
is an implementation detail.
(The only place where C lets you specify a size in bits
is in bit-fields within structures;
see questions 2.25 and 2.26<span style="text-decoration: underline;"></span>)
Most programs
do not need precise control
over
these sizes;
many programs that do
try to achieve this control
would be better off if they didn't.
<p>Type
<tt>int</tt> is supposed to represent a machine's natural word size.
It's the right type to use for most integer variables;
see question 1.1 for other guidelines.
See also questions12.42 and 20.5</p>1.3具体自己定义16，32位int来实现各种机器上面的整数是没有必要的。因为要注意很多事情哦（文档加上个人理解,不对勿怪-yama）<span style="font-weight: bold;"><br /></span> Since C doesn't define sizes exactly,
I've been using
typedefs
like <tt>int16</tt> and
<tt>int32</tt>.
I can then define these typedefs
to be <tt>int</tt>, <tt>short</tt>, <tt>long</tt>,
etc. depending on what machine I'm using.

That should solve everything, right?<span style="font-weight: bold;"><br /><br /></span>If you truly need
control
over exact type sizes,
this is the right approach.
There remain several things to be aware of:
<ul><li>There might not be an exact match on some machines.
(There are, for example, 36-bit machines.)

</li><li>A typedef like <tt>int16</tt> or <tt>int32</tt>
accomplishes nothing
if its intended meaning is ``at least'' the specified size,
because types <tt>int</tt> and <tt>long</tt>
are <em>already</em> essentially defined as being
``at least 16 bits'' and
``at least 32 bits,'' respectively.
</li><li>Typedefs
will never
do anything about byte order problems
(e.g.
if you're trying to interchange data
or conform to
externally-imposed storage layouts).
</li><li>You no longer have to define your own typedefs,
because the Standard header <tt>&lt;inttypes.h&gt;</tt>
contains a complete set.
</li></ul>1.4 机器上面64位的类型的支持是因为编译器大部分实现了long long类型或者他的扩展_longlong.也可以来定义short int为16位，int为32位，long int为64位来实现。（文档加上个人理解,不对勿怪-yama）<p> What should the 64-bit type be
on a
machine that can support it?</p><p> 
The
new
C99 Standard
specifies type <tt>long long</tt> as effectively being
at least 64 bits,
and this type has been implemented by a number of compilers
for some time.
(Others have implemented extensions such as <tt>__longlong</tt>.)
On the other hand,
it's also appropriate to
implement
type <tt>short int</tt> as 16,
<tt>int</tt> as 32,
and <tt>long int</tt> as 64 bits,
and some compilers do.
</p><img src ="http://www.cnitblog.com/Yama/aggbug/14330.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2006-07-28 13:19 <a href="http://www.cnitblog.com/Yama/archive/2006/07/28/14330.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>comp.lang.c FAQ list精华整理 1.1 关于选用整数类型的原则</title><link>http://www.cnitblog.com/Yama/archive/2006/07/27/14280.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Thu, 27 Jul 2006 05:47:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2006/07/27/14280.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/14280.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2006/07/27/14280.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/14280.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/14280.html</trackback:ping><description><![CDATA[1.<br />    if ((n &gt; 32,767) or (n &lt; -32,767)) { n.selectUseType = <font color="#ff0000">long</font>; }<br />2.<br />    if (there are large arrays or many structures) { n.selectUseType = <font color="#ff0000">short</font>; }<br />3.<br />    if ((well-defined overflow characteristics are important
) or <br />        (not negative values) or <br />        (manipulating bits or bytes and not want to meet sign-extension problems))<br />    { <br />        n.selectUseType = <font color="#ff0000">unsigned </font>n.selectUseType; <br />    }<br />4. <br />    assert(<font color="#ff0000">not use <tt>unsigned char</tt> replace integer type</font>);<br />5.    <br />    assert(<font color="#ff0000">sizeof(char) &lt;= sizeof(short) &lt;= sizeof(int) &lt;= sizeof(long) &lt;= sizeof(long long)</font>);<br />6.<br />    if (Under <font color="#ff0000">ANSI C</font>) <br />    {<br />        nTypeMin = &lt;<font color="#ff0000">limits.h</font>&gt;.maximum;<br />        nTypeMax = &lt;<font color="#ff0000">limits.h</font>&gt;.minimum;<br />    }<br />7. <br />    if (want to define an
exact
size object) <br />    {<br />        n.selectUseType = <font color="#ff0000">typedef userDefineType</font>;    <br />    }<br /><img src ="http://www.cnitblog.com/Yama/aggbug/14280.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2006-07-27 13:47 <a href="http://www.cnitblog.com/Yama/archive/2006/07/27/14280.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>comp.lang.c FAQ list :Declarations and Initializations Question 1.1 </title><link>http://www.cnitblog.com/Yama/archive/2006/03/09/7385.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Thu, 09 Mar 2006 01:41:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2006/03/09/7385.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/7385.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2006/03/09/7385.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/7385.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/7385.html</trackback:ping><description><![CDATA[How should I decide which integer type to use<BR><BR>If you might need large values (above 32,767 or below -32,767), use <TT>long</TT>. Otherwise, if space is very important (i.e. if there are large arrays or many structures), use <TT>short</TT>. Otherwise, use <TT>int</TT>. If well-defined overflow characteristics are important and negative values are not, or if you want to steer clear of sign-extension problems when manipulating bits or bytes, use one of the corresponding <TT>unsigned</TT> types. (Beware when mixing signed and unsigned values in expressions, though; see question <A href="http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=expr#preservingrules">3.19</A>.) 
<P>Although character types (especially <TT>unsigned&nbsp;char</TT>) can be used as ``tiny'' integers, doing so is sometimes more trouble than it's worth. The compiler will have to emit extra code to convert between <TT>char</TT> and <TT>int</TT> (making the executable larger), and unexpected sign extension can be troublesome. (Using <TT>unsigned&nbsp;char</TT> can help; see question <A href="http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=stdio#getcharc">12.1</A> for a related problem.) </P>
<P>A similar space/time tradeoff applies when deciding between <TT>float</TT> and <TT>double</TT>. (Many compilers still convert all <TT>float</TT> values to <TT>double</TT> during expression evaluation.) None of the above rules apply if pointers to the variable must have a particular type. </P>
<P>Variables referring to certain kinds of data, such as sizes of objects in memory, can and should used predefined abstract types such as <TT>size_t</TT>. </P>
<P>It's often incorrectly assumed that C's types are defined to have certain, exact sizes. In fact, what's guaranteed is that: 
<UL>
<LI>type <TT>char</TT> can hold values up to 127; 
<LI>types <TT>short int</TT> and <TT>int</TT> can hold values up to 32,767; and 
<LI>type <TT>long int</TT> can hold values up to 2,147,483,647. 
<LI>something like the relation <PRE>	sizeof(char) &lt;= sizeof(short) &lt;= sizeof(int) &lt;= sizeof(long) &lt;= sizeof(long long)
</PRE>holds. <A href="http://c-faq.com/decl/fn1.html" rel=subdocument>[footnote]</A> </LI></UL>
<P></P>
<P>From these values, it can be inferred that <TT>char</TT> is at least 8 bits, <TT>short int</TT> and <TT>int</TT> are at least 16 bits, and <TT>long int</TT> is at least 32 bits. (The signed and unsigned versions of each type are guaranteed to have the same size.) Under ANSI C, the maximum and minimum values for a particular machine can be found in the header file <TT>&lt;limits.h&gt;</TT>; here is a summary: 
<TABLE>
<TBODY>
<TR>
<TD>Base type</TD>
<TD>Minimum size (bits)</TD>
<TD>Minimum value (signed)</TD>
<TD>Maximum value (signed)</TD>
<TD>Maximum value (unsigned)</TD></TR>
<TR>
<TD><TT>char</TT></TD>
<TD>8</TD>
<TD>-127</TD>
<TD>127</TD>
<TD>255</TD></TR>
<TR>
<TD><TT>short</TT></TD>
<TD>16</TD>
<TD>-32,767</TD>
<TD>32,767</TD>
<TD>65,535</TD></TR>
<TR>
<TD><TT>int</TT></TD>
<TD>16</TD>
<TD>-32,767</TD>
<TD>32,767</TD>
<TD>65,535</TD></TR>
<TR>
<TD><TT>long</TT></TD>
<TD>32</TD>
<TD>-2,147,483,647</TD>
<TD>2,147,483,647</TD>
<TD>4,294,967,295</TD></TR></TBODY></TABLE>(These values are the minimums guaranteed by the Standard. Many implementations allow larger values, but portable programs shouldn't depend on it.) </P>
<P>If for some reason you need to declare something with an exact size (usually the only good reason for doing so is when attempting to conform to some externally-imposed storage layout, but see question <A href="http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=misc#binaryfiles">20.5</A>), be sure to encapsulate the choice behind an appropriate typedef, but see question <A href="http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=decl#int16">1.3</A>. </P>
<P>If you need to manipulate huge values, larger than the guaranteed range of C's built-in types, you need an arbitrary-precision (or ``multiple precision'') arithmetic library; see question <A href="http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=resources#mplib">18.15d</A>. </P><img src ="http://www.cnitblog.com/Yama/aggbug/7385.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2006-03-09 09:41 <a href="http://www.cnitblog.com/Yama/archive/2006/03/09/7385.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于C99翻译募集（自由参加，free学习）</title><link>http://www.cnitblog.com/Yama/archive/2006/03/03/7101.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Fri, 03 Mar 2006 03:05:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2006/03/03/7101.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/7101.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2006/03/03/7101.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/7101.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/7101.html</trackback:ping><description><![CDATA[一直想腾出时间来好好把C99全部翻译出来，很多地方看过了但是用中文表达的确很难以描述。所以<BR>想到集众人的力量来好好翻译一下，请希望学习和我一样对C报有兴趣的人来参加。<BR>因为不是盈利项目，如果你实在没有时间也不要紧。期待大家的参加，最好是7月底前能够完成。<BR>希望一个人负责一节左右，看过c99英文式样的人都知道关键是第6章和第七章。<BR><BR>1。我的邮件是<A href="mailto:chinatimeover@hotmail.com">chinatimeover@hotmail.com</A>(来信请注明c99,当然别的朋友也欢迎，不过最近很忙，如果不能回信请见谅)<BR>2。所有的人可以在自己的blog上翻译，但是务必mail给我（成果物按周提交，我会对它和检查。不能按时完成任务请注明。）<BR>3。我会开2篇文章，每周发布一次。对于参加的每位成员我也会单独把成果以mail形式转送<BR>&nbsp;&nbsp;&nbsp;一篇是用到的术语统一名称贴，一篇是成果物贴（我会注明翻译者的名字）<BR>4。募集准备到本月为止，然后我会再次发布任务分配，如果你对于C99有过很好的研究，可以注明你希望翻译的章节（也许有人翻译过，但是就目前网上看很多翻译的作品离我想象得太远，特别是上次的RFC翻译事件，老实说很多文章和英文版有差距）<BR>5。声明C99英文版是有版权的，所以我们的作品仅仅是为了学习，没有任何营利目的，如果侵害到某某版权问题，请来信告知，我会妥善处理。所以不要找我要求c99文档-〉下载有的我调查过，嘻嘻。<BR>6。翻译和校对时间 4，5，6三个月（视情况决定）。最后再次希望社区内的大拿们鼎力协作。把我们blog社区办的更有影响力<BR><BR><BR>心无尘土<BR><BR>20060303<BR><BR><BR><img src ="http://www.cnitblog.com/Yama/aggbug/7101.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2006-03-03 11:05 <a href="http://www.cnitblog.com/Yama/archive/2006/03/03/7101.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>extern</title><link>http://www.cnitblog.com/Yama/archive/2006/01/11/6122.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Wed, 11 Jan 2006 06:38:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2006/01/11/6122.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/6122.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2006/01/11/6122.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/6122.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/6122.html</trackback:ping><description><![CDATA[<STRONG>extern</STRONG>是C/C++语言中表明函数和全局变量<STRONG>作用范围</STRONG>（可见性）的关键字.<BR>它告诉编译器，其<STRONG>声明</STRONG>的函数和变量可以在本模块或其它模块中使用。<BR><FONT style="BACKGROUND-COLOR: #ffffff" color=#ff0000><FONT color=#000000>1。对于extern变量来说，</FONT>仅仅是一个变量的声明，其并不是在定义分配内存空间</FONT>。如果该变量定义多次，会有连接错误<BR>2。通常，<FONT color=#ff0000><STRONG>在模块的头文件中对本模块提供给其它模块引用的函数和全局变量以关键字extern声明。</STRONG><STRONG>也就是说c文件里面定义，如果该函数或者变量与开放给外面，则在h文件中用extern加以声明。所以外部文件只用include该h文件就可以了。</STRONG>而且编译阶段，外面是找不到该函数的，但是不报错。link阶段会从定义模块生成的目标代码中找到此函数。<BR></FONT><FONT color=#000000>3。</FONT>与extern对应的关键字是static，被它修饰的全局变量和函数只能在本模块中使用。<BR><BR><BR>后面转载，阅读中。。。。。。。<BR>　　被extern "C"修饰的变量和函数是按照C语言方式编译和连接的；<BR><STRONG>未加extern “C”声明时的编译方式<BR><BR></STRONG>　　首先看看C++中对类似C的函数是怎样编译的。<BR><BR>　　作为一种面向对象的语言，C++支持函数重载，而过程式语言C则不支持。函数被C++编译后在符号库中的名字与C语言的不同。例如，假设某个函数的原型为： <BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>void foo( int x, int y );</TD></TR></TBODY></TABLE><BR>　　该函数被C编译器编译后在符号库中的名字为_foo，而C++编译器则会产生像_foo_int_int之类的名字（不同的编译器可能生成的名字不同，但是都采用了相同的机制，生成的新名字称为“mangled name”）。<BR><BR>　　_foo_int_int这样的名字包含了函数名、函数参数数量及类型信息，C++就是靠这种机制来实现函数重载的。例如，在C++中，函数void foo( int x, int y )与void foo( int x, float y )编译生成的符号是不相同的，后者为_foo_int_float。<BR><BR>　　同样地，C++中的变量除支持局部变量外，还支持类成员变量和全局变量。用户所<A class=bluekey href="http://www.yesky.com/key/4285/189285.html" target=_blank>编写</A>程序的类成员变量可能与全局变量同名，我们以"."来区分。而本质上，编译器在进行编译时，与函数的处理相似，也为类中的变量取了一个独一无二的名字，这个名字与用户程序中同名的全局变量名字不同。<BR><BR>　　<B>未加extern "C"声明时的连接方式</B><BR><BR>　　假设在C++中，模块A的头文件如下：<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>// 模块A头文件　moduleA.h<BR>#ifndef MODULE_A_H<BR>#define MODULE_A_H<BR>int foo( int x, int y );<BR>#endif</TD></TR></TBODY></TABLE><BR>　　在模块B中引用该函数：<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>// 模块B实现文件　moduleB.cpp<BR>#include "moduleA.h"<BR>foo(2,3);</TD></TR></TBODY></TABLE><BR>　　实际上，在连接阶段，连接器会从模块A生成的目标文件moduleA.obj中寻找_foo_int_int这样的符号！<BR><BR>　　<B>加extern "C"声明后的编译和连接方式</B><BR><BR>　　加extern "C"声明后，模块A的头文件变为：<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>// 模块A头文件　moduleA.h<BR>#ifndef MODULE_A_H<BR>#define MODULE_A_H<BR>extern "C" int foo( int x, int y );<BR>#endif</TD></TR></TBODY></TABLE><BR>　　在模块B的实现文件中仍然调用foo( 2,3 )，其结果是：<BR><BR>　　（1）模块A编译生成foo的目标代码时，没有对其名字进行特殊处理，采用了C语言的方式；<BR><BR>　　（2）连接器在为模块B的目标代码寻找foo(2,3)调用时，寻找的是未经修改的符号名_foo。<BR><BR>　　如果在模块A中函数声明了foo为extern "C"类型，而模块B中包含的是extern int foo( int x, int y ) ，则模块B找不到模块A中的函数；反之亦然。<BR><BR>　　所以，可以用一句话概括extern “C”这个声明的真实目的（任何语言中的任何语法特性的诞生都不是随意而为的，来源于真实世界的需求驱动。我们在思考问题时，不能只停留在这个语言是怎么做的，还要问一问它为什么要这么做，动机是什么，这样我们可以更深入地理解许多问题）： <BR><BR>　　实现C++与C及其它语言的混合编程。<BR><BR>　　明白了C++中extern "C"的设立动机，我们下面来具体分析extern "C"通常的<A class=bluekey href="http://www.yesky.com/key/263/160263.html" target=_blank>使用技巧</A>。<BR><BR>　　4.extern "C"的惯用法<BR><BR>　　（1）在C++中引用C语言中的函数和变量，在包含C语言头文件（假设为cExample.h）时，需进行下列处理：<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>extern "C"<BR>{<BR>#include "cExample.h"<BR>}</TD></TR></TBODY></TABLE><BR>　　而在C语言的头文件中，对其外部函数只能指定为extern类型，C语言中不支持extern "C"声明，在.c文件中包含了extern "C"时会出现编译语法错误。<BR><BR>　　笔者编写的C++引用C函数例子工程中包含的三个文件的源代码如下： <BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>/* c语言头文件：cExample.h */<BR>#ifndef C_EXAMPLE_H<BR>#define C_EXAMPLE_H<BR>extern int add(int x,int y);<BR>#endif<BR>/* c语言实现文件：cExample.c */<BR>#include "cExample.h"<BR>int add( int x, int y )<BR>{<BR>　return x + y;<BR>}<BR>// c++实现文件，调用add：cppFile.cpp<BR>extern "C" <BR>{<BR>　#include "cExample.h"<BR>}<BR>int main(int argc, char* argv[])<BR>{<BR>　add(2,3); <BR>　return 0;<BR>}</TD></TR></TBODY></TABLE><BR>　　如果C++调用一个C语言编写的.DLL时，当包括.DLL的头文件或声明接口函数时，应加extern "C" {　}。<BR><BR>　　（2）在C中引用C++语言中的函数和变量时，C++的头文件需添加extern "C"，但是在C语言中不能直接引用声明了extern "C"的该头文件，应该仅将C文件中将C++中定义的extern "C"函数声明为extern类型。<BR><BR>　　笔者编写的C引用C++函数例子工程中包含的三个文件的源代码如下：<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>//C++头文件 cppExample.h<BR>#ifndef CPP_EXAMPLE_H<BR>#define CPP_EXAMPLE_H<BR>extern "C" int add( int x, int y );<BR>#endif<BR>//C++实现文件 cppExample.cpp<BR>#include "cppExample.h"<BR>int add( int x, int y )<BR>{<BR>　return x + y;<BR>}<BR>/* C实现文件 cFile.c<BR>/* 这样会编译出错：#include "cExample.h" */<BR>extern int add( int x, int y );<BR>int main( int argc, char* argv[] )<BR>{<BR>　add( 2, 3 ); <BR>　return 0;<BR>}</TD></TR></TBODY></TABLE><BR>　　如果深入理解了第3节中所阐述的extern "C"在编译和连接阶段发挥的作用，就能真正理解本节所阐述的从C++引用C函数和C引用C++函数的惯用法。对第4节给出的示例代码，需要特别留意各个细节。 <img src ="http://www.cnitblog.com/Yama/aggbug/6122.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2006-01-11 14:38 <a href="http://www.cnitblog.com/Yama/archive/2006/01/11/6122.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C99-2</title><link>http://www.cnitblog.com/Yama/archive/2005/12/07/5217.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Wed, 07 Dec 2005 07:29:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2005/12/07/5217.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/5217.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2005/12/07/5217.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/5217.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/5217.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 从我的csdn的小窝转过来<br>c99 2.术语，定义，符号<br>&nbsp;&nbsp;<a href='http://www.cnitblog.com/Yama/archive/2005/12/07/5217.html'>阅读全文</a><img src ="http://www.cnitblog.com/Yama/aggbug/5217.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2005-12-07 15:29 <a href="http://www.cnitblog.com/Yama/archive/2005/12/07/5217.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C99-1</title><link>http://www.cnitblog.com/Yama/archive/2005/12/07/5215.html</link><dc:creator>Yama的家</dc:creator><author>Yama的家</author><pubDate>Wed, 07 Dec 2005 07:25:00 GMT</pubDate><guid>http://www.cnitblog.com/Yama/archive/2005/12/07/5215.html</guid><wfw:comment>http://www.cnitblog.com/Yama/comments/5215.html</wfw:comment><comments>http://www.cnitblog.com/Yama/archive/2005/12/07/5215.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Yama/comments/commentRss/5215.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Yama/services/trackbacks/5215.html</trackback:ping><description><![CDATA[<P>标准才是最重要。我们的目标是创造新的标准。</P>
<P>c99(ISO/IEC 9899：1999 international stardard)，他的前一个版本是ISO/IEC 9899：1990</P>
<P><STRONG><FONT color=#0000ff>1.范围</FONT></STRONG></P>
<UL>
<LI><STRONG>C99定义了以下的内容</STRONG></LI></UL>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P>-C程序的表现法</P>
<P>-关于C语言的语法和约束限制</P>
<P>-编译C程序的语义规则（Yama<IMG alt="" src="http://blog.csdn.net/fckeditor/editor/images/smiley/msn/wink_smile.gif">C99文档之所以收费，估计就是因为它了<IMG alt="" src="http://blog.csdn.net/fckeditor/editor/images/smiley/msn/wink_smile.gif">Yama）</P>
<P>-关于C语言处理入力数据的说明</P>
<P>-关于C语言产生出力数据的说明</P>
<P>-利用约束和限制的状态一致的C的执行</P></BLOCKQUOTE>
<UL>
<LI><STRONG>C99没有说明的内容</STRONG></LI></UL>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT color=#808080>-C程序被用来转化成数据处理系统的机制</FONT></P>
<P><FONT color=#808080>-C程序被应用于数据处理系统的机制</FONT></P>
<P><FONT color=#808080>-通过C程序转化入力数据的机制</FONT></P>
<P><FONT color=#808080>-入力数据通过C程序产生后转化的机制</FONT></P>
<P><FONT color=#808080>-程序的大小和复杂度。数据超过特有的数据处理系统或者特殊处理器的处理能力</FONT></P>
<P><FONT color=#808080>-一个数据处理系统的所有最小划分要求都能够支持一致性执行</FONT></P></BLOCKQUOTE><img src ="http://www.cnitblog.com/Yama/aggbug/5215.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Yama/" target="_blank">Yama的家</a> 2005-12-07 15:25 <a href="http://www.cnitblog.com/Yama/archive/2005/12/07/5215.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>