Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

c - static variable changing value with no error displayed?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For starters in the beginning of the program you changed the static variable b

b = b-1;

the keyword static does not have the meaning as the keyword const.

On the other hand, in this call of printf there is evidently a typo

printf("%d 
","%d",a,b);
               ^^^^

In fact you are trying to output a pointer to the string literal "%d" as an integer.

That is the function has four arguments and only one conversion specifier in the first argument.

The compiler could issue a warning that there are redundant arguments in the call of printf.

You could write for example like :)

printf("%d 
" "%d",a,b);

In this case the output would be

11
1

Because the above call is equivalent to

printf("%d 
%d",a,b);

But it seems you mean

printf("%d %d
", a, b);

So neither static variable is changed spontanno.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...