/* Think it's okayProgramCan run in various compilers, soCodeDevelopers should be careful and learn the truth! */INT main () {int A = 42; printf ("% d \ n", );}
What is the problem with the above small program? What do you think? Add the following points to the C program that is close to "correct.
1. # include <stdio. h>
2. Return 0;
The result of running the program is: Print 42 on the screen and wrap the line.
In fact, in addition to the above two modifications, you can also improve ----- 3. Int main (void)
Let's look at the following small program:
# Include <stdio. h> void fun (void) {int A = 3;/* changed to static int A = 3; what should I do? */++ A; printf ("% d \ n", a) ;}int main (void) {fun (); return 0 ;}
Output result: 4 (line feed) 4 (line feed) 4 (line feed)
01.What if I modified the program according to the comments? Result: 4 (line feed) 5 (line feed) 6 (line feed)
02.If the above program is modified again, modify the comment statementStatic int;What are the results? Error message? Or is there three "Garbage counts "?
None! Result: 1 (line feed) 2 (line feed) 3 (line feed)
Cause:When no value is assigned to a static variable, the default value is 0! This is irrelevant to the compiler.
Q: If the auto variable is not assigned a value, the default value is 0?No! However, the result you get may be 1 (line feed) 2 (line feed) 3 (line feed), which may be caused by your compiler. Try another compiler.
Q: Why does C treat auto and static differently?A: C language is a language that emphasizes efficiency. If auto is assigned an initial value of 0 by default, it will increase the time spent on function calling.
03.If you change the comment statement to int A and move it to the external body of the function, which is defined as a global variable, the result will be? A:1 (line feed) 2 (line feed) 3 (line feed)[That is, when it is defined as a global variable, static int A; the result is the same as that of int]
Let's look at the next program:
# Include <stdio. h> void fun (void) {int A; printf ("% d \ n", a);} void bar (void) {int A = 42 ;} int main (void) {bar (); fun (); Return 0 ;}
In VC 6.0, the result is a "Garbage count", but the result obtained in GCC is 42. [This is not the code we want to write. Avoid it as much as possible !]
Next program:
# Include <stdio. h> int B (void) {puts ("3"); return 3 ;}int C (void) {puts ("4"); return 4 ;} int main (void) {int A = B () + C (); printf ("A = % d \ n", a) Return 0 ;}
Running result: 3 (line feed) 4 (line feed) 7 (line feed) or 4 (line feed) 3 (line feed) 7 (line feed) [After puts output, it will wrap! It is also the reason for the compiler !]