1. The following program does not necessarily output hello-std-out. Do you know why?
# Include <stdio. h>
# Include <unistd. h>
Intmain ()
{
While (1)
{
Fprintf (stdout, "hello-std-out ");
Fprintf (stderr, "hello-std-err ");
Sleep (1 );
}
Return 0;
}
Reference answer: Is stdout and stderr the same as the device descriptor. Stdout is a block device, and stderr is not. Block devices are input only in the following situations. 1) return a carriage return, 2) the buffer is full, and 3) flush is called. But stderr does not.
2. Next, let's look at a cross-compilation task. Can the two files below be compiled? If yes, what is the result?
File1.c
Int arr [80];
File2.c
Externint * arr;
Intmain ()
{
Arr [1] = 100;
Printf ("% d \ n", arr [1]);
Return 0;
}
Reference answer: This program can be compiled, but an error occurs during running. Why? The reason is that using extern int * arr in another file to declare an array externally does not get the expected value because their types do not match. Therefore, the pointer does not actually point to the array. Note: A pointer to an array is not equal to an array. Modify: extern int arr []. (Refer to section 6.5.4.2 of ISO c language)
3. What is the output of the following program? And explains why? (Note that this program does not output "B is 20 ″)
# Include <stdio. h>
Intmain ()
{
Int a = 1;
Switch ()
{
Int B = 20;
Case 1:
Printf ("B is % d \ n", B );
Break;
Default:
Printf ("B is % d \ n", B );
Break;
}
Return 0;
}
Reference answer: during compilation, a warning: unreachable code at beginning ofswitch statement may appear. We thought that after entering the switch, variable B will be initialized, but it is not because the switch-case statement will directly skip the initialization of variable B. Therefore, the program outputs a random memory value.
4. What is the output of the following program?
# Include <stdio. h>
Intmain ()
{
Int I;
I = 10;
Printf ("I: % d \ n", I );
Printf ("sizeof (I ++) is: % d \ n", sizeof (I ++ ));
Printf ("I: % d \ n", I );
Return 0;
}
Reference answer: If you think the output is respectively, 10, 4, 11, then you are wrong. The error is in the third place. The first is 10, and there is no problem. The second is 4, there is no problem because an int on a 32-bit machine has four bytes. But why is the third output not 11? Actually 10? The reason is that sizeof is not a function, but an operator, which calculates the size of the I ++ type. This is a task that can be completed before the program runs (during compilation). Therefore, sizeof (I ++) is directly replaced by 4, and there will be no I ++ expression at runtime.
5. What is the output of the following program? (Definitely not 10)
# Include
# DefinePrintInt (expr) printf ("% s: % dn", # expr, (expr ))
Intmain ()
{
Int y= 100;
Int * p;
P = malloc (sizeof (int ));
* P = 10;
Y = y/* p;/* dividing y by * p */;
PrintInt (y );
Return 0;
}
Reference answer: the answer to this question is 100. Why? The problem lies in y = y/* p;. we originally thought of y/(* p). However, we did not add spaces and parentheses, result/* in y/* p is interpreted as the beginning of the annotation. This is also the beginning of the nightmare.
6. What is the output of the following program? (Assume: Input Hello, World)
# Include <stdio. h>
Intmain ()
{
Char dummy [80];
Printf ("Enter a string: \ n ");
Scanf ("% [^ r]", dummy );
Printf ("% s \ n", dummy );
Return 0;
}