1331com银河游戏

cstatic

cstatic

ƽ̨ûĻоѡcstaticصģ1C澲̬staticʲôð 2CԱСstaticʲô˼? 3Cstaticʲô 4cstatic ɶð 5c static̬ 6c static ÷Χʲô? 7Cstaticľʲô벻ҪcopyŶ 8Cstatic ϸ˵ 9cstatic int i=1ʲô˼ 10cstatic int intʲô
cstatic
ƽ̨û 2018-06-06 16:51:37

Ǿѡƽ̨ûʱ

Ұ·
Ұ·


Ұ·C澲̬staticʲôð

ѧߡ Ŀ #include void Caculate() { static int iSum=3; int iTemp; iTemp=iSum; //iSumǾ̬ iSum=iTemp*3; printf("%d\n",iSum); } int main() { Caculate(); //õiSumƽ Caculate(); //... ѧߡ
Ŀ
#include

void Caculate()
{
static int iSum=3;
int iTemp;
iTemp=iSum; //iSumǾ̬
iSum=iTemp*3;
printf("%d\n",iSum);
}
int main()
{
Caculate(); //õiSumƽ
Caculate(); //õiSum
return 0;
}

Ȼн
9
27
Press any key to continue

ڵڶεCaculate()ʱ򣬲¸ֵiSum=3ΪʲôڶεõʱiSum=3ûأ
Ľѱ8781˵
staticؼCһ̬ľֲ
ã
ʱϣеľֲֵںýʧԭֵռõĴ洢ԪͷţһٵøúʱñֵһκýʱֵʱӦָþֲΪֲ̬ùؼstatic

þ̬洢Ҫռڴ棨ռòͷţ̬洢һ洢ԪȺΪʹãԼڴ棩ҽ˳ĿɶԣDZҪҪþֲ̬
һα༭ʱ Ƽ2018-03-02 10:38:40
351125153
351125153


351125153CԱСstaticʲô˼?

Ľѱ7429˵
static ڲͬĵط岻ͬġ
ں֮ʹãʾöڴļȫֿɷʵģļ֮Dzɷʵġ
ںڲʾñԶһԳʼıʽʼĬֵ0ıֵԺ󣬺ʱֵҲᱣ
һα༭ʱ Ƽ2017-09-23 03:18:52
5564ad
5564ad


5564adCstaticʲô

ллֹһ˼~~~
Ľѱ7652˵
Dzֹһ˼Ҫõ磺
static int a;
int b;
void func(void)
{
static int c;
int d;
}
abȫֱߵǣbԱļʹãaֻڱļʹãstaticȫֱá
cdǣdһԶfuncִdԶͷšcȴᱻͷţһεfuncʱcֵᱣϴεֵʹá
һα༭ʱ Ƽ2017-11-24 11:25:26
105069789
105069789


105069789cstatic ɶð

ٸ˵ #include int f(int x) { static int a=3; a+=x; return a; } void main () { int k=2,m=1,n; n=f(k); n=f(m); printf("%d",n); } ֪ n=fmʱ Ǹ static int a=3 ЧaΪʲô... ٸ˵

#include
int f(int x)
{
static int a=3;
a+=x;
return a;
}

void main ()
{
int k=2,m=1,n;
n=f(k);
n=f(m);
printf("%d",n);
}
֪ n=fmʱ

Ǹ static int a=3 ЧaΪʲô n=fm֮õĽأ

УҪ~
Ľѱ1576˵
CУstaticֱ£

ͬʱļʱδstaticǰ׺ȫֱͺȫֿɼԡ
2ֱݵij־ã
洢ھ̬ıڳտʼʱɳ ʼҲΨһһγʼ
3ĬϳʼΪ0
ھ̬ڴеֽĬֵ0x00ijЩ ʱһصԼٳԱĹ
һα༭ʱ Ƽ2017-09-30 08:19:31
langtj
langtj


langtjc static̬

#include "iostream.h"
int p(int x)
{
static int y=1;
y=y+1;
return (x+y);}
int main()
{
cout<cout<
35Ϊʲô34أ
һεõʱѵ static int y=1; û岻ˣ
Ľѱ7623˵
cԵstaticڳصʱΪ֮ڴģһֱִͨһηһΡԵһִp1ʱyѾ1ˣ´ִp2ʱyָ12͵ȫֱĹܴУ
һα༭ʱ Ƽ2017-09-15 13:47:29
һʱ_bobo
һʱ_bobo


һʱ_boboc static ÷Χʲô?

Ľѱ3612˵

static

һһãء

ͬʱļʱδstaticǰ׺ȫֱͺȫֿɼԡ

ܻʣΪʲôa.cжȫֱaͺmsgmain.cʹãǰ˵δstaticǰ׺ȫֱͺȫֿɼԣԴļҲܷʡУaȫֱmsgǺҶûмstaticǰ׺˶Դļmain.cǿɼġ

staticͻԴļءamsgĶǰstaticmain.cͿˡһԿڲͬļжͬͬصͻStaticͱǰ׺ںstaticýأڱstaticá

staticĵڶDZֱݵij־á

洢ھ̬ıڳտʼʱɳʼҲΨһһγʼֱ洢ھ̬洢ȫֱstaticֻȫֱstaticԿƱĿɼΧ˵staticصġ

staticĵĬϳʼΪ0

ʵȫֱҲ߱һԣΪȫֱҲ洢ھ̬ھ̬ڴеֽĬֵ0x00ijЩʱһصԼٳԱĹ

ϸ

һα༭ʱ 2018-03-30
yonder88
yonder88


yonder88Cstaticľʲô벻ҪcopyŶ

Ľѱ310˵

Cstaticľǣ

  1. һЧʲôط

  2. ļʹͬı³ͻ

Cľ̬ͺǣ

  1. ̬Ǻһ֣̬ͷǾ̬֡

  2. ̬staticεĺûУΪǾ̬(ȫֺ)

  3. ֻһCļʱû𣬵ɶCļʱߵ򣬼ʹõķΧͬ

  4. ̬(static)ֻڱļʹã޷ļǾ̬(static)κһļʹáļʹʱҪʹá

һα༭ʱ 2018-03-31



Cstatic ϸ˵

Ľѱ9384˵
google˽ҳĹCstaticݣֿõϢ٣Ҫôƪ۲֪Ҫôڹؼ֮Թھײԭijѧ˵οԲǺܴԣƪIJڼ֮ѻϵϹֱ࣬д֤֮
CԴļΪλ֯ģһԴԴļУһⲿעⲻǾֲߺֻһԴжһΣظĻͻᱨŲͬԴļͺ֮໥Լ໥Ĺϵexternstaticؼ֡
棬ϸһstaticؼڱдʱе÷
һstaticȫֱ
֪һڴеIJͼ1ʾ

.textαִеijļ.dataαеѳʼȫֱ.bssαδʼȫֱлкܶ߰ĶΣҲڽ̵У.dataκ.bssڵʱͬģҲڽ֮̽ЩݲŻվޡ
һ̵ȫֱΪstatic֮о̬ȫֱ̬ȫֱȫֱĴ洢ص㲢û𣬶.dataΣѳʼ.bssΣδʼڣֻڶԴļЧԴļ޷ԣͨȫֱstaticºͱֻܱԴļɣеıʡ
һЩʾ
file1.h£
[cpp] view plaincopy
#include

void printStr();
file1.cжһ̬ȫֱhello, file1.cеĺprintStr.
[cpp] view plaincopy
#include "file1.h"

static char* hello = "hello cobing!";

void printStr()
{
printf("%s\n", hello);
}
file2.cǵļfile2.chello
[cpp] view plaincopy
#include "file1.h"

int main()
{
printStr();
printf("%s\n", hello);
return 0;
}
£
[liujx@server235 static]$ gcc -Wall file2.c file1.c -o file2
file2.c: In function main:
file2.c:6: 󣺡hello δ (ڴ˺ڵһʹ)
file2.c:6: (ʹһڶγ֣ÿδıʶ
file2.c:6: ڵĺֻһΡ)

ǽfile2.cΪʽ
[cpp] view plaincopy
#include "file1.h"

int main()
{
printStr();
return 0;
}
˳ӡ
гĽ£
[liujx@server235 static]$ gcc -Wall file2.c file1.c -o file2
[liujx@server235 static]$ ./file2
hello cobing!

Уfile1.cеhelloһ̬ȫֱԱͬһļеprintStrãDzܱͬԴļеfile2.cá

staticֲ
ͨľֲջռϷ䣬ֲڵĺεʱÿεֲջϵλöһֲͬҲڶ϶̬䣬ǼǵʹѿռҪͷ֮
staticֲоֲ̬ͨľֲ¼
1λãֲ̬ȫִ洢.dataע⣺.bssڣԭ3ȻǾֲģڳдڡ
2Ȩޣֲֻ̬ܱڵıʡҲ˵ȻڳдڣstaticģܱĺԴļʡ
3ֲֵ̬ûбûʼᱻԶֵΪ0Ժÿεþֲ̬ʱϴεúֵȽϺ⣬ÿκþֲ̬ʱ޸Ȼ뿪´ζʱȫִ洢ľֲ̬ϴ޸ĺֵ
һЩʾ
file1.hݺеͬfile1.c£
[cpp] view plaincopy
#include "file1.h"

void printStr()
{
int normal = 0;
static int stat = 0; //this is a static local var
printf("normal = %d ---- stat = %d\n",normal, stat);
normal++;
stat++;
}
Ϊ˱ڱȽϣҶֲͨnormal;ֲ̬statǶֵ0
file2.cеfile1.h
[cpp] view plaincopy
#include "file1.h"

int main()
{
printStr();
printStr();
printStr();
printStr();
printf("call stat in main: %d\n",stat);
return 0;
}
ûᱨΪfile2.cfile1.cеľֲ̬stat£
[liujx@server235 static]$ gcc -Wall file2.c file1.c -o file2
file2.c: In function main:
file2.c:9: 󣺡stat δ (ڴ˺ڵһʹ)
file2.c:9: (ʹһڶγ֣ÿδıʶ
file2.c:9: ڵĺֻһΡ)
˵statδΪfile1.cеstatעһУ
[cpp] view plaincopy
#include "file1.h"

int main()
{
printStr();
printStr();
printStr();
printStr();
// printf("call stat in main: %d\n",stat);
return 0;
}
[liujx@server235 static]$ gcc -Wall file2.c file1.c -o file2
[liujx@server235 static]$ ./file2
normal = 0 ---- stat = 0
normal = 0 ---- stat = 1
normal = 0 ---- stat = 2
normal = 0 ---- stat = 3
ʾԿÿαãֲͨ·䣬ֲ̬ϴεõֵ䡣
Ҫעstaticֲԣʹúֲ̬ĺò룬ÿεÿܻͬĽڶ̱߳ʱܻΪһҪע⡣

static
ŴһǵC++еprivate˽кֻиijԱԱԷʡCУҲСprivateǽҪ˵staticprivateĹܡ
ijкܶԴļʱ϶ijԴļֻṩһЩҪĽӿڣĺΪʵЩӿڶдЩĺܲϣ磨DZԴļʱͿstaticЩĺ
staticDZԴļΪеprivateͿˡ
һЩʾ
file1.h£
[cpp] view plaincopy
#include

static int called();
void printStr();
file1.c£
[cpp] view plaincopy
#include "file1.h"

static int called()
{
return 6;
}
void printStr()
{
int returnVal;
returnVal = called();
printf("returnVal=%d\n",returnVal);
}
file2.cеfile1.h˴ǹcalled():
[cpp] view plaincopy
#include "file1.h"

int main()
{
int val;
val = called();
printStr();
return 0;
}
ʱᱨ
[liujx@server235 static]$ gcc -Wall file2.c file1.c -o file2
file1.h:3: 棺called ʹùδ
/tmp/ccyLuBZU.o: In function `main':
file2.c:(.text+0x12): undefined reference to `called'
collect2: ld 1
Ϊfile1.hеstaticfile2.cʾҲ:undefined reference to 'called'
޸file2.c:
[cpp] view plaincopy
#include "file1.h"

int main()
{
printStr();
return 0;
}
У
[liujx@server235 static]$ gcc -Wall file2.c file1.c -o file2
[liujx@server235 static]$ ./file2
returnVal=6

staticԺܺõؽͬԭļкͬ⣬ΪһԴļԴļеstaticDzɼġ
һα༭ʱ Ƽ2018-05-10 18:04:24



cstatic int i=1ʲô˼

Ľѱ4606˵

Ҫģ

  1. Ƿںڣôһֲ̬ڴ治ͷţڵͬȫֱ

  2. ںᣬôʾһɿļȫֱԱԴļextern

һα༭ʱ Ƽ2017-05-21 21:38:52



cstatic int intʲô

Ľѱ7376˵
main𲻴ܴstaticܱϴεúĽintںغˣ´ε½
һα༭ʱ Ƽ2017-04-19 17:16:54
网站地图

银河1331官网