Let's take a look at a program (slightly changed) in Expert c programming ):
# Include <stdio. h>
Void init (char * str)
{
Static char s = '';
Printf ("% c % s \ n", s, str );
S = ',';
}
Int main ()
{
Init ("hhh ");
Init ("hhh ");
Init ("hhh ");
Return 0;
}
The output is as follows:
Local static variables in c are initialized only once. For understanding, let's look at the assembly code:
. Data
. Type s.0, @ object
. Size s.0, 1
S.0:
. Byte 32 # initialize here
. Section. rodata
. LC0:
. String "% c % s \ n"
. Text
. Globl init
. Type init, @ function
Init:
Pushl % ebp
Movl % esp, % ebp
Subl $8, % esp
Subl $4, % esp
Pushl 8 (% ebp)
Movsbl s.0, % eax # s-> eax
Pushl % eax
Pushl $. LC0
Call printf
Addl $16, % esp
Movb $44, s.0
Leave
Ret
We can see that there is no statement for s initialization in init. s is located in the Data zone and initialized outside init.