38th questions
What is the bug in the following program? #include <stdlib.h> #include <stdio.h> #define SIZE 15 int main() { int *a, i; a = malloc(SIZE*sizeof(int)); for (i=0; i<SIZE; i++) *(a + i) = i * i; for (i=0; i<SIZE; i++) printf("%d\n", *a++); free(a); return 0; }
Description:
When printf prints the value of the stack, the value of a is changed. Therefore, the value of a in free (a) is not the starting address of the previously allocated stack.
39th questions
Is the following a valid C program? If so, what is the output of it? #include <stdio.h> int main() { int a=3, b = 5; printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]); printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"], 2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]); return 0; }
Knowledge points:
The most common expression of elements in an array is address [offset], such as a [0], a [1], and a [2].
There is also an uncommon representation: Offset [address], such as 0 [a], 1 [A], 2 [A].
Example:
#include <stdio.h>int main(){int a[] = {0, 1, 2};printf(“%d, %d, %d\n”, 0[a], 1[a], 2[a]);return 0;}
Running result: 0, 1, 2
Printf ("% d, % d, % d \ n", 0 [a], 1 [A], 2 [a]);
Equivalent
Printf ("% d, % d, % d \ n", a [0], a [1], a [2]);
Description:
Printf (& A ["ya! Hello! How is this? % S \ n "], & B [" junk/super "]);
Equivalent
Printf ("Hello! How is this? % S \ n "," super ");
Printf (& A ["What % C! \ N "], 1 [" this "], 2 [" beauty "], 0 [" tool "], 0 [" is "], 3 [" sensitive "], 4 ["cccccc"]);
Equivalent
Printf ("T % C! \ N ", 'h', 'A', 't', 'I', 's', 'C ');
40th questions
What is the output of the following, if the input provided is:Life is beautiful #include <stdio.h> int main() { char dummy[80]; printf("Enter a string:\n"); scanf("%[^a]",dummy); printf("%s\n",dummy); return 0; }
Knowledge points:
Scanf format controller:
% [...]: Read the string until it encounters a character that is not in;
% [^...]: Read the string until it encounters a character in.
For example:
Scanf ("% [A-Z]", dummy); when "ABC123" is input, dummy is "ABC ";
Scanf ("% [^ A-Z]", dummy); when "123abc" is input, dummy is "123 ";
Description:
"% [^ A]" means to read the string until the character 'a' is encountered '. So when the input is "Life is beauul ul", dummy is "Life is be ".
41st questions
Note : This question has more to do with Linker than C language We have three files a.c, b.c and main.c respectively as follows: a.c--- int a;b.c--- int a = 10;main.c------ extern int a;int main(){ printf("a = %d\n",a); return 0;}Let‘s see what happens, when the files are compiled together: bash$ gcc a.c b.c main.cbash$ ./a.outa = 10Hmm!! no compilation/linker error!!! Why is it so??
Knowledge points:
Refer to page 67th declaration and definition of c traps and defects.
If int A is defined in one file and int A is defined in the other file, the compiler will initialize a to 0;
If int A is defined in one file, int A = 10 is defined in the other file, and A is 10;
If int A = 1 is defined in one file and int A = 10 is defined in the other file, the compilation will produce a duplicate definition error.
42nd questions
The following is the offset macros which is used many a times. Figure out what is it trying to do and what is the advantage of using it. #define offsetof(a,b) ((int)(&(((a*)(0))->b)))
Knowledge points:
Calculates the offset of the struct member variable in the struct. A is a struct type, and B is a member variable.
43rd questions
The following is the macro implementation of the famous, Triple xor swap. #define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))What are the potential problems with the above macro?
Knowledge points:
For more information, see http://www.cnblogs.com/tanghuimin0713/p/3220665.html.
This method has the following limitations:
1) A and B cannot be the same variables. That is, if swap (A, A) is executed, the value is set to 0 no matter what the original a value is;
2) A and B cannot be floating-point numbers. The XOR operation has no significance for floating-point numbers;
3) A and B cannot be Composite data types such as, for the same reason as above;
4) A or B cannot be expressions;
44th questions
What is the use of the following macro? #define DPRINTF(x) printf("%s:%d\n",#x,x)
Description:
Print the value of X.
For example, if a = 10, the result of dprintf (a) is "A: 10".
45th questions
Let‘s say you were asked to code a function IAddOverFlow which takes three parameters, pointer to an integer where the result is to be stored, and the two integers which needs to be added. It returns 0 if there is an overflow and 1 otherwise: int IAddOverFlow(int* result,int a,int b) { /* ... */ }So, how do you code the above function? (To put in a nutshell, what is the logic you use for overflow detection?)
Description:
Reference for methods of detecting overflow: http://www.fefe.de/intof.html
C puzzles [38-45]