int a => auto variable
The cost of setting auto variables to 0 would
increase the cost of function calls. C has a very
strong focus on execution speed.
static int a
the global data segment to 0
however, is a one time cost that happens at
start up, and that might be the reason why it is
so in C.
在C 語言中,static variables 預設是0,但在C++ 卻不是這樣,
And to be precise, in C++ however, static
variables are not set to 0, they are set to their
default values... which for native types means 0
#include <stdio.h>
int a;
void foo(void)
{
++a;
printf("%d\n", a);
}
int main(void)
{
foo();
foo();
foo();
}
=> 結果: 1
2
3
Why:
因為全域變數預設值為0
Memsetting the global data segment to 0
留言列表