<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[实践OK]static和extern理解]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Thu, 28 Feb 2019 02:05:02 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	<a href="https://www.cnblogs.com/zhaopengdt/p/5250600.html" target="_blank">https://www.cnblogs.com/zhaopengdt/p/5250600.html</a><br/><br/>在C语言中，static的字面意思很容易把我们导入歧途，其实它的作用有三条。<br/>介绍它的第一条也是最重要的一条：隐藏。<br/>当我们同时编译多个文件时，所有未加static前缀的全局变量和函数都具有全局可见性。为理解这句话，我举例来说明。我们要同时编译两个源文件，一个是a.c，另一个是main.c。<br/><br/>下面是a.c的内容<br/><br/>char a = &#039;A&#039;; // global variable<br/>void msg()<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello&#92;n&quot;);<br/>&#125;<br/>下面是main.c的内容<br/><br/>复制代码<br/>int main(void)<br/>&#123;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;extern char a;&nbsp;&nbsp;&nbsp;&nbsp;// extern variable must be declared before use<br/>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%c &quot;, a);<br/>&nbsp;&nbsp;&nbsp;&nbsp;(void)msg();<br/>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br/>&#125;<br/>复制代码<br/>程序的运行结果是：<br/>A Hello<br/><br/>你可能会问：为 什么在a.c中定义的全局变量a和函数msg能在main.c中使用？前面说过，所有未加static前缀的全局变量和函数都具有全局可见性，其它的源文 件也能访问。此例中，a是全局变量，msg是函数，并且都没有加static前缀，因此对于另外的源文件main.c是可见的。<br/><br/>如果加了static，就会对其它源文件隐藏。例如在a和msg的定义前加上static，main.c就看不到它们了。利用这一特性可以在不同的 文件中定义同名函数和同名变量，而不必担心命名冲突。Static可以用作函数和变量的前缀，对于函数来讲，static的作用仅限于隐藏。<br/><br/>c语言中static和extern的用法详细解析（http://www.jb51.net/article/41686.htm）<br/>一，static和extern：<br/>大工程下我们会碰到很多源文档。<br/><br/>文档a.c<br/><br/>复制代码<br/>static int i; //只在a文档中用<br/>int j;&nbsp;&nbsp;&nbsp;&nbsp;//在工程里用<br/>static void init()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //只在a文档中用<br/>&#123;<br/>&#125;<br/>void callme()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//在工程中用<br/>&#123;<br/>&nbsp;&nbsp; static int sum;<br/>&#125;<br/>复制代码<br/>上面的全局i变量和init()函数只能用在a.c文档中,全局变量sum的作用域只在callme里。变量j和函数callme()的全局限扩充到整个 工程文档。所以能够在下面的b.c中用extern关键字调用。extern告诉编译器这个变量或函数在其他文档里已被定义了。<br/><br/>文档b.c<br/><br/>extern int j;&nbsp;&nbsp;&nbsp;&nbsp; //调用a文档里的<br/>extern void callme();&nbsp;&nbsp;//调用a文档里的<br/>int main()<br/>&#123;<br/>&nbsp;&nbsp;...<br/>&#125;<br/>extern的另外用法是当C和C++混合编程时假如c++调用的是c源文档定义的函数或变量，那么要加extern来告诉编译器用c方式命名函数：<br/>文档A.cpp调用a.c里面的变量i和函数callme()<br/><br/>复制代码<br/>extern &quot;C&quot;&nbsp;&nbsp;//在c++文档里调用c文档中的变量<br/>&#123;<br/>&nbsp;&nbsp; int j;<br/>&nbsp;&nbsp; void callme();<br/>&#125;<br/>int main()<br/>&#123;<br/>&nbsp;&nbsp; callme();<br/>&#125;<br/>复制代码<br/>二，static法则：<br/>A、若全局变量仅在单个C文档中访问，则能够将这个变量修改为静态全局变量，以降低模块间的耦合度; <br/><br/>B、若全局变量仅由单个函数访问，则能够将这个变量改为该函数的静态局部变量，以降低模块间的耦合度；<br/><br/>C、设计和使用访问动态全局变量、静态全局变量、静态局部变量的函数时，需要考虑重入问题；
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]static和extern理解]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>