C/C++ÓïÑÔÖÐStaticµÄ×÷ÓÃÏêÊö
ÔÚCÓïÑÔÖУ¬staticµÄ×ÖÃæÒâ˼ºÜÈÝÒ×°ÑÎÒÃǵ¼ÈëÆç;£¬ÆäʵËüµÄ×÷ÓÃÓÐÈýÌõ¡£
£¨1£©ÏÈÀ´½éÉÜËüµÄµÚÒ»ÌõÒ²ÊÇ×îÖØÒªµÄÒ»Ìõ£ºÒþ²Ø¡£
µ±ÎÒÃÇͬʱ±àÒë¶à¸öÎļþʱ£¬ËùÓÐδ¼Óstaticǰ׺µÄÈ«¾Ö±äÁ¿ºÍº¯Êý¶¼¾ßÓÐÈ«¾Ö¿É¼ûÐÔ¡£ÎªÀí½âÕâ¾ä»°£¬ÎÒ¾ÙÀýÀ´ËµÃ÷¡£ÎÒÃÇҪͬʱ±àÒëÁ½¸öÔ´Îļþ£¬Ò»¸öÊÇa.c£¬ÁíÒ»¸öÊÇmain.c¡£
ÏÂÃæÊÇa.cµÄÄÚÈÝ
char a = 'A'; // global variable
void msg()
{
printf("Hello\n");
}
ÏÂÃæÊÇmain.cµÄÄÚÈÝ
int main(void)
{
extern char a; // extern variable must be declared before use
printf("%c ", a);
(void)msg();
return 0;
}
³ÌÐòµÄÔËÐнá¹ûÊÇ£º
A Hello
Äã
¿ÉÄÜ»áÎÊ£ºÎªÊ²Ã´ÔÚa.cÖж¨ÒåµÄÈ«¾Ö±äÁ¿aºÍº¯ÊýmsgÄÜÔÚmain.cÖÐʹÓã¿Ç°Ãæ˵¹ý£¬ËùÓÐδ¼Óstaticǰ׺µÄÈ«¾Ö±äÁ¿ºÍº¯Êý¶¼¾ßÓÐÈ«¾Ö¿É¼û
ÐÔ£¬ÆäËüµÄÔ´ÎļþÒ²ÄÜ·ÃÎÊ¡£´ËÀýÖУ¬aÊÇÈ«¾Ö±äÁ¿£¬msgÊǺ¯Êý£¬²¢ÇÒ¶¼Ã»ÓмÓstaticǰ׺£¬Òò´Ë¶ÔÓÚÁíÍâµÄÔ´Îļþmain.cÊǿɼûµÄ¡£
Èç
¹û¼ÓÁËstatic£¬¾Í»á¶ÔÆäËüÔ´ÎļþÒþ²Ø¡£ÀýÈçÔÚaºÍmsgµÄ¶¨ÒåÇ°¼ÓÉÏstatic£¬main.c¾Í¿´²»µ½ËüÃÇÁË¡£ÀûÓÃÕâÒ»ÌØÐÔ¿ÉÒÔÔÚ²»Í¬µÄÎļþÖж¨
ÒåͬÃûº¯ÊýºÍͬÃû±äÁ¿£¬¶ø²»±Øµ£ÐÄÃüÃû³åÍ»¡£Static¿ÉÒÔÓÃ×÷º¯ÊýºÍ±äÁ¿µÄǰ׺£¬¶ÔÓÚº¯ÊýÀ´½²£¬staticµÄ×÷ÓýöÏÞÓÚÒþ²Ø£¬¶ø¶ÔÓÚ±ä
Á¿£¬static»¹ÓÐÏÂÃæÁ½¸ö×÷Óá£
£¨2£©staticµÄµÚ¶þ¸ö×÷ÓÃÊDZ£³Ö±äÁ¿ÄÚÈݵij־ᣴ洢ÔÚ¾²Ì¬Êý¾ÝÇøµÄ±äÁ¿»áÔÚ³ÌÐò¸Õ¿ªÊ¼ÔËÐÐʱ¾ÍÍê³É³õ
ʼ»¯£¬Ò²ÊÇΨһµÄÒ»´Î³õʼ»¯¡£¹²ÓÐÁ½ÖÖ±äÁ¿´æ´¢ÔÚ¾²Ì¬´æ´¢Çø£ºÈ«¾Ö±äÁ¿ºÍstatic±äÁ¿£¬Ö»²»¹ýºÍÈ«¾Ö±äÁ¿±ÈÆðÀ´£¬static¿ÉÒÔ¿ØÖƱäÁ¿µÄ¿É¼û·¶
Χ£¬Ëµµ½µ×static»¹ÊÇÓÃÀ´Òþ²ØµÄ¡£ËäÈ»ÕâÖÖÓ÷¨²»³£¼û£¬µ«ÎÒ»¹ÊǾÙÒ»¸öÀý×Ó¡£
££i nclude
int fun(void){
static int count = 10; //ÊÂʵÉϴ˸³ÖµÓï¾ä´ÓÀ´Ã»ÓÐÖ´Ðйý
return count--;
}
int count = 1;
int main(void)
{
printf("global\t\tlocal static\n");
for(; count <= 10; ++count)
printf("%d\t\t%d\n", count, fun());
return 0;
}
³ÌÐòµÄÔËÐнá¹ûÊÇ£º
global local static
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1
£¨3£©static
µÄµÚÈý¸ö×÷ÓÃÊÇĬÈϳõʼ»¯Îª0¡£Æäʵȫ¾Ö±äÁ¿Ò²¾ß±¸ÕâÒ»ÊôÐÔ£¬ÒòΪȫ¾Ö±äÁ¿Ò²´æ´¢ÔÚ¾²Ì¬Êý¾ÝÇø¡£ÔÚ¾²Ì¬Êý¾ÝÇø£¬ÄÚ´æÖÐËùÓеÄ×Ö½ÚĬÈÏÖµ¶¼ÊÇ0x00£¬Ä³Ð©
ʱºòÕâÒ»Ìصã¿ÉÒÔ¼õÉÙ³ÌÐòÔ±µÄ¹¤×÷Á¿¡£±ÈÈç³õʼ»¯Ò»¸öÏ¡Êè¾ØÕó£¬ÎÒÃÇ¿ÉÒÔÒ»¸ö
Ïà¹ØÎĵµ£º
char* my_strrev( char* string )
{
char *left, *right, ch;
left = right = string;
while( *right++ != '\0');
right -= 2;
while( left<right )
{
ch = *left;
*left = *right;
*right = ch;
++left; --right;
&n ......
http://www.cnblogs.com/hoodlum1980/archive/2007/08/15/857067.html
Ìâ¸É£ºÊäÈëÁ½¸ö½Ï´óµÄÊý£¬Êä³öÏà³ËµÄ½á¹û¡£
Òâ˼Ҳ¾ÍÊÇÁ½¸öÊýºÜ´ó£¬³¬³öÁËintµÄ´æ´¢·¶Î§¡£
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
void GetDigits( ......
ͨ³££¬ÔÚCÓïÑÔµÄÍ·ÎļþÖо³£¿ÉÒÔ¿´µ½ÀàËÆÏÂÃæÕâÖÖÐÎʽµÄ´úÂ룺
#ifdef __cplusplus
extern "C" {
#endif
/**** some declaration or so *****/
#ifdef __cplusplus
}
#endif /* end of __cplusplus */
ÄÇô£¬ÕâÖÖд·¨Ê²Ã´ÓÃÄØ£¿Êµ¼ÊÉÏ£¬ÕâÊÇΪÁËÈÃCPPÄܹ»ÓëC½Ó¿Ú¶ø²ÉÓõÄÒ»ÖÖÓï·¨ÐÎʽ¡£Ö®ËùÒÔ²ÉÓÃÕâÖÖ·½Ê½ ......
ÒÔÇ°Ôø¾×ܽá¹ýһƪ(http://www.cppblog.com/franksunny/archive/2007/11/29/37510.html
)£¬¹ØÓÚÔÚ
C
ÖÐÈçºÎµ÷ÓÃ
C++
µÄ´úÂ룬µ±Ê±²¢Î´×öÍêÈ«µÄÕ¹¿ª£¬Ö»ÊǼòµ¥µÄ×öÁËϵ÷ÊÔ£¬×î½ü¿´µ½Ò»¸öÌâÄ¿ÒªÇóʵÏÖ
C
ºÍ
C++
ÖдúÂëµÄ»¥Ïàµ÷Óã¬Æä½á¹ûËäÈ»¶¼ÊÇͨ¹ý
extern “C”
À´ÊµÏÖ
£¬µ«ÊǾßÌ廹ÊÇÓÐЩ²î±ðµÄ¡ ......
Visual C++6.0(5.0)¿ª·¢¹¤¾ß¹¦Äܷdz£Ç¿´ó£¬µ«ÊǶÔÓÚ³õѧÕßÀ´Ëµ£¬È´Óкܶàϸ½ÚµÄÎÊÌâÐèҪעÒâ¡£×÷ÕßËѼ¯ÕûÀíÁËÒÔÏÂһЩʵÓÃС¼¼ÇÉ£¬Ï£Íû¶Ô³õѧÕßÓÐËù°ïÖú¡£
1:ʹÓÃvc¿ª·¢ÏîĿʱ£¬³£»áÓöµ½ÕâÖÖÇé¿ö£º
¼´Ã÷Ã÷Ö»¸Ä¶¯ÁËÒ»¸öÎļþ£¬È´Òª°ÑÕû¸öÏîÄ¿È«²¿ÖØбàÒëÁ¬½ÓÒ»´Î¡£¸Õ¸ÕÁ¬½ÓºÃ£¬Ò»ÔËÐУ¬ÓÖÌáʾ ......