What is the output of the following program? Can you see it at a glance ???
Evaluate the knowledge of basic C skills.
1.
[Cpp] # include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <string>
Using namespace std;
Int main ()
{
Int a = 2, B = 7, c = 5;
Switch (a> 0)
{
Case 1: switch (B <0)
{
Case 1: printf ("@"); break;
Case 2: printf ("! "); Break;
}
Case 0: switch (c = 5)
{
Case 1: printf ("*"); break;
Case 2: printf ("#"); break;
Default: printf ("#"); break;
}
Default: printf ("&");
}
Printf ("\ n ");
Return 0;
}
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <string>
Using namespace std;
Int main ()
{
Int a = 2, B = 7, c = 5;
Switch (a> 0)
{
Case 1: switch (B <0)
{
Case 1: printf ("@"); break;
Case 2: printf ("! "); Break;
}
Case 0: switch (c = 5)
{
Case 1: printf ("*"); break;
Case 2: printf ("#"); break;
Default: printf ("#"); break;
}
Default: printf ("&");
}
Printf ("\ n ");
Return 0;
}
The answer is:
*&
Because a> 0, enter case 1, and find that B <0 is false, should enter 0, but no, exit the switch, but case1 does not have break, can only enter case0, if the condition is met, * is output, and no break exits. If the condition is set to default, output and exit.
2.
[Cpp] # include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <string>
Using namespace std;
Int I = 0;
Int fun1 (int I)
{
I = (I % I) * (I * I)/(2 * I) + 4;
Printf ("I = % d \ n", I );
Return (I );
}
Int fun2 (int I)
{
I = I <= 2? 5: 0;
Return (I );
}
Int main ()
{
Int I = 5;
Fun2 (I/2); printf ("I = % d \ n", I );
Fun2 (I = I/2); printf ("I = % d \ n", I );
Fun2 (I/2); printf ("I = % d \ n", I );
Fun1 (I/2); printf ("I = % d \ n", I );
Return 0;
}
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <string>
Using namespace std;
Int I = 0;
Int fun1 (int I)
{
I = (I % I) * (I * I)/(2 * I) + 4;
Printf ("I = % d \ n", I );
Return (I );
}
Int fun2 (int I)
{
I = I <= 2? 5: 0;
Return (I );
}
Int main ()
{
Int I = 5;
Fun2 (I/2); printf ("I = % d \ n", I );
Fun2 (I = I/2); printf ("I = % d \ n", I );
Fun2 (I/2); printf ("I = % d \ n", I );
Fun1 (I/2); printf ("I = % d \ n", I );
Return 0;
}
Answer:
5, 2, 2, 2
Resolution:
Knowledge Point: 1. functions are passed by value, so the external I value will not be changed. 2. Global variables and local variables must follow the proximity principle.
So the first time I = 5
Then I = 5/2 = 2
However, after entering the layer-by-layer operation, the I changes in it, but the I is released when it is returned, which has no effect on the original I. So I = 2
Third time I = 2
The fourth time, after entering the function, according to the proximity principle, I is equivalent to a local variable. The local variable is 4, SO 4 is output. However, when printf is used, I is still the I in main, it has nothing to do with I in the function just now, so it is still 2
Excerpted from a stray calf