More C Interview Questions
1. How do you decide which integer type to use?
2. What should the 64-bit type on a machine that can support it?
3. What’s the best way to declare and define global variables
4. and functions?
5. What does extern mean in a function declaration?
6. What’s the auto keyword good for?
7. I can’t seem to define a linked list successfully. I tried
typedef struct {
char *item;
NODEPTR next;
*NODEPTR;
but the compiler gave me error messages. Can’t a structure in C
contain a pointer to itself?
8. How do I declare an array of N pointers to functions returning
pointers to functions returning pointers to characters?
9. How can I declare a function that can return a pointer to a
function of the same type?
10. What’s the right declaration for main()?
Is void main() correct?
11. What am I allowed to assume about the initial valuesof variables which are not explicitly initialized?
12. If global variables start out as “zero”, is that good enough for null pointers and floating-point zeroes?
13. This code, straight out of a book, isn’t compiling:
int f()
{
char a[] = “Hello, world!”;
14. What’s wrong with this initialization?
char *p = malloc(10);
15. What is the difference between these initializations?
char a[] = “string literal”;
char *p = “string literal”;
16. What’s the difference between these two declarations?
struct x1 { … };
typedef struct { … } x2;
17. Why doesn’t
struct x { … };
x thestruct;
work?
18. Can a structure contain a pointer to itself?
19. What’s the best way of implementing opaque (abstract) data types
in C?
20. I came across some code that declared a structure like this:
struct name {
int namelen;
char namestr[1];
};
and then did some tricky allocation to make the namestr array act like it had several elements. Is this legal or portable?
21. Is there a way to compare structures automatically?
22. How can I pass constant values to functions which accept structure arguments?
23. How can I read/write structures from/to data files?
24. Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end?
25. How can I determine the byte offset of a field within a structure?
26. How can I access structure fields by name at run time?
27. This program works correctly, but it dumps core after it finishes. Why?
struct list {
char *item;
struct list *next;
}
/* Here is the main program. */
main(argc, argv)
{ … }
28. Can I initialize unions?
29. What is the difference between an enumeration and a set of preprocessor #defines?
30. Is there an easy way to print enumeration values symbolically?
31. Why doesn’t this code:
a[i] = i++;
work?
32. I’ve experimented with the code
int i = 3;
i = i++;
on several compilers. Some gave i the value 3, and some gave 4.Which compiler is correct?
33. Can I use explicit parentheses to force the order of evaluation I want? Even if I don’t, doesn’t precedence dictate it?
34. How can I understand these complex expressions? What’s a “sequence point”?
35. If I’m not using the value of the expression, should I use i++ or ++i to increment a variable?
36. Why doesn’t the code
int a = 1000, b = 1000;
long int c = a * b;
work?
37. I’m trying to declare a pointer and allocate some space for it,
but it’s not working. What’s wrong with this code?
char *p;
*p = malloc(10);
38. Does *p++ increment p, or what it points to?
39. I have a char * pointer that happens to point to some ints, and I want to step it over them. Why doesn’t
((int *)p)++;
work?
40. I have a function which accepts, and is supposed to initialize,
a pointer:
void f(int *ip)
{
static int dummy = 5;
ip = &dummy;
}
But when I call it like this:
int *ip;
f(ip);
the pointer in the caller remains unchanged.Why?
41. Can I use a void ** pointer as a parameter so that a function can accept a generic pointer by reference?
42. I have a function
extern int f(int *);
which accepts a pointer to an int. How can I pass a constant by
reference? A call like
f(&5); doesn’t seem to work.
43. Does C even have “pass by reference”?
44. What is infamous null pointer?
45. How do I get a null pointer in my programs?
46. Is the abbreviated pointer comparison “if(p)” to test for non-null pointers valid? What if the internal representation for null pointers is nonzero?
47. What is NULL and how is it #defined?
48. How should NULL be defined on a machine which uses a nonzero bit pattern as the internal representation of a null pointer?
49. If NULL were defined as follows:
#define NULL ((char *)0)
wouldn’t that make function calls which pass an uncast NULL work?
50. If NULL and 0 are equivalent as null pointer constants, which should I use?
51. What does a run-time “null pointer assignment” error mean? How can I track it down?
52. Why are array and pointer declarations interchangeable as function formal parameters?
53. How can an array be an lvalue, if you can’t assign to it?
54. Practically speaking, what is the difference between arrays and pointers?
55. How do I declare a pointer to an array?
56. How can I set an array’s size at run time? How can I avoid fixed-sized arrays?
57. How can I declare local arrays of a size matching a passed-in array?
58. How can I dynamically allocate a multidimensional array?
59. How do I write functions which accept two-dimensional arrays when the width is not known at compile time?
60. How can I use statically- and dynamically-allocated multidimensional arrays interchangeably when passing them to functions?
61. Why doesn’t sizeof properly report the size of an array when the array is a parameter to a function?
62. Why doesn’t this fragment work?
char *answer;
printf(”Type something:\n”);
gets(answer);
printf(”You typed \”%s\”\n”, answer);
63. I just tried the code
char *p;
strcpy(p, “abc”); and it worked. How? Why didn’t it crash?
64. How much memory does a pointer variable allocate?
65. Why am I getting “warning: assignment of pointer from integer lacks a cast” for calls to malloc()?
66. Why does some code carefully cast the values returned by malloc to the pointer type being allocated?
67. Why isn’t a pointer null after calling free()? How unsafe is it to use (assign, compare) a pointer value after it’s been freed?
68. When I call malloc() to allocate memory for a pointer which is local to a function, do I have to explicitly free() it?
69. I’m allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer?
70. Must I free allocated memory before the program exits?
71. How does free() know how many bytes to free?
72. Can I query the malloc package to find out how big an allocated block is?
73. Is it legal to pass a null pointer as the first argument to realloc()?
74. What’s the difference between calloc() and malloc()? Is it safe to take advantage of calloc’s zero-filling? Does free() work on memory allocated with calloc(), or do you need a cfree()?
75. What is alloca() and why is its use discouraged?
76. Why doesn’t
strcat(string, ‘!’);
work?
77. How can I get the numeric (character set) value corresponding to a character, or vice versa?
78. What is the right type to use for Boolean values in C?
79. Is if(p), where p is a pointer, a valid conditional?
80. How can I write a generic macro to swap two values?What’s the best way to write a multi-statement macro?
81. Is it acceptable for one header file to #include another?
82. What’s the difference between #include and #include “” ?
83. What are the complete rules for header file searching?
84. How can I construct preprocessor #if expressions which compare strings?
85. Does the sizeof operator work in preprocessor #if directives?
86. Can I use an #ifdef in a #define line, to define something two different ways?
87. Is there anything like an #ifdef for typedefs?
88. How can I use a preprocessor #if expression to tell if a machine is big-endian or little-endian?
89. How can I list all of the predefined identifiers?
90. How can I write a macro which takes a variable number of arguments?
91. What is the “ANSI C Standard?”
92. What’s the difference between “const char *p” and “char * const p”?
93. Why can’t I pass a char ** to a function which expects a const char **?
94. What’s the correct declaration of main()?
95. Can I declare main() as void ?
96. What does the message “warning: macro replacement within a string literal” mean?
97. What are #pragmas and what are they good for?
98. What does “#pragma once” mean?
99. Is char a[3] = “abc”; legal? What does it mean?
100. Why can’t I perform arithmetic on a void * pointer?
101. What’s the difference between memcpy() and memmove()?
102. What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?
103. What’s wrong with this code?
char c;
while((c = getchar()) != EOF) …
104. Why does the code
while(!feof(infp)) {
a. fgets(buf, MAXLINE, infp);
b. fputs(buf, outfp);
}
copy the last line twice?
105. How can I read one character at a time, without waiting for the RETURN key?
106. How can I print a ‘%’ character in a printf format string?
107. What printf format should I use for a typedef like size_t when I don’t know whether it’s long or some other type?
108. How can I implement a variable field width with printf?
109. How can I print numbers with commas separating the thousands? What about currency formatted numbers?
110. Why doesn’t the call scanf(”%d”, i) work?
111. How can I specify a variable width in a scanf() format string?
112. Why doesn’t this code:
double d;
scanf(”%f”, &d);
work?
113. How can I tell how much destination buffer space I’ll need for an arbitrary sprintf call? How can I avoid overflowing the destination buffer with sprintf()?
114. What’s the difference between fgetpos/fsetpos and ftell/fseek?
115. What are fgetpos() and fsetpos() good for?
116. How can I redirect stdin or stdout to a file from within a program?
117. How can I read a binary data file properly?
118. How can I convert numbers to strings (the opposite of atoi)? Is there an itoa() function?
119. Why does strncpy() not always place a ” terminator in the destination string?
120. Why do some versions of toupper() act strangely if given an upper-case letter?
121. How can I split up a string into whitespace-separated fields? How can I duplicate the process by which main() is handed argc and argv?
122. How can I sort a linked list?
123. How can I sort more data than will fit in memory?
124. How can I get the current date or time of day in a C program?
125. How can I add N days to a date? How can I find the difference between two dates?
126. How can I get random integers in a certain range?
127. How can I generate random numbers with a normal or Gaussian distribution?
128. What does it mean when the linker says that _end is undefined?
129. When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
130. What’s a good way to check for “close enough” floating-point equality?
131. How do I round numbers?
132. Why doesn’t C have an exponentiation operator?
133. How do I test for IEEE NaN and other special values?
134. What’s a good way to implement complex numbers in C?
135. How can %f be used for both float and double arguments in printf()? Aren’t they different types?
136. How can I write a function that takes a variable number of arguments?
137. How can I write a function that takes a format string and a variable number of arguments, like printf(), and passes them to printf() to do most of the work?
138. How can I write a function analogous to scanf(), that calls scanf() to do most of the work?
139. How can I discover how many arguments a function was actually called with?
140. How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)?
141. How can I call a function with an argument list built up at run time?bobby
Posts: 398
Joined: Tue Jul 01, 2008 11:26 pmTop
2. What should the 64-bit type on a machine that can support it?
3. What’s the best way to declare and define global variables
4. and functions?
5. What does extern mean in a function declaration?
6. What’s the auto keyword good for?
7. I can’t seem to define a linked list successfully. I tried
typedef struct {
char *item;
NODEPTR next;
*NODEPTR;
but the compiler gave me error messages. Can’t a structure in C
contain a pointer to itself?
8. How do I declare an array of N pointers to functions returning
pointers to functions returning pointers to characters?
9. How can I declare a function that can return a pointer to a
function of the same type?
10. What’s the right declaration for main()?
Is void main() correct?
11. What am I allowed to assume about the initial valuesof variables which are not explicitly initialized?
12. If global variables start out as “zero”, is that good enough for null pointers and floating-point zeroes?
13. This code, straight out of a book, isn’t compiling:
int f()
{
char a[] = “Hello, world!”;
14. What’s wrong with this initialization?
char *p = malloc(10);
15. What is the difference between these initializations?
char a[] = “string literal”;
char *p = “string literal”;
16. What’s the difference between these two declarations?
struct x1 { … };
typedef struct { … } x2;
17. Why doesn’t
struct x { … };
x thestruct;
work?
18. Can a structure contain a pointer to itself?
19. What’s the best way of implementing opaque (abstract) data types
in C?
20. I came across some code that declared a structure like this:
struct name {
int namelen;
char namestr[1];
};
and then did some tricky allocation to make the namestr array act like it had several elements. Is this legal or portable?
21. Is there a way to compare structures automatically?
22. How can I pass constant values to functions which accept structure arguments?
23. How can I read/write structures from/to data files?
24. Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end?
25. How can I determine the byte offset of a field within a structure?
26. How can I access structure fields by name at run time?
27. This program works correctly, but it dumps core after it finishes. Why?
struct list {
char *item;
struct list *next;
}
/* Here is the main program. */
main(argc, argv)
{ … }
28. Can I initialize unions?
29. What is the difference between an enumeration and a set of preprocessor #defines?
30. Is there an easy way to print enumeration values symbolically?
31. Why doesn’t this code:
a[i] = i++;
work?
32. I’ve experimented with the code
int i = 3;
i = i++;
on several compilers. Some gave i the value 3, and some gave 4.Which compiler is correct?
33. Can I use explicit parentheses to force the order of evaluation I want? Even if I don’t, doesn’t precedence dictate it?
34. How can I understand these complex expressions? What’s a “sequence point”?
35. If I’m not using the value of the expression, should I use i++ or ++i to increment a variable?
36. Why doesn’t the code
int a = 1000, b = 1000;
long int c = a * b;
work?
37. I’m trying to declare a pointer and allocate some space for it,
but it’s not working. What’s wrong with this code?
char *p;
*p = malloc(10);
38. Does *p++ increment p, or what it points to?
39. I have a char * pointer that happens to point to some ints, and I want to step it over them. Why doesn’t
((int *)p)++;
work?
40. I have a function which accepts, and is supposed to initialize,
a pointer:
void f(int *ip)
{
static int dummy = 5;
ip = &dummy;
}
But when I call it like this:
int *ip;
f(ip);
the pointer in the caller remains unchanged.Why?
41. Can I use a void ** pointer as a parameter so that a function can accept a generic pointer by reference?
42. I have a function
extern int f(int *);
which accepts a pointer to an int. How can I pass a constant by
reference? A call like
f(&5); doesn’t seem to work.
43. Does C even have “pass by reference”?
44. What is infamous null pointer?
45. How do I get a null pointer in my programs?
46. Is the abbreviated pointer comparison “if(p)” to test for non-null pointers valid? What if the internal representation for null pointers is nonzero?
47. What is NULL and how is it #defined?
48. How should NULL be defined on a machine which uses a nonzero bit pattern as the internal representation of a null pointer?
49. If NULL were defined as follows:
#define NULL ((char *)0)
wouldn’t that make function calls which pass an uncast NULL work?
50. If NULL and 0 are equivalent as null pointer constants, which should I use?
51. What does a run-time “null pointer assignment” error mean? How can I track it down?
52. Why are array and pointer declarations interchangeable as function formal parameters?
53. How can an array be an lvalue, if you can’t assign to it?
54. Practically speaking, what is the difference between arrays and pointers?
55. How do I declare a pointer to an array?
56. How can I set an array’s size at run time? How can I avoid fixed-sized arrays?
57. How can I declare local arrays of a size matching a passed-in array?
58. How can I dynamically allocate a multidimensional array?
59. How do I write functions which accept two-dimensional arrays when the width is not known at compile time?
60. How can I use statically- and dynamically-allocated multidimensional arrays interchangeably when passing them to functions?
61. Why doesn’t sizeof properly report the size of an array when the array is a parameter to a function?
62. Why doesn’t this fragment work?
char *answer;
printf(”Type something:\n”);
gets(answer);
printf(”You typed \”%s\”\n”, answer);
63. I just tried the code
char *p;
strcpy(p, “abc”); and it worked. How? Why didn’t it crash?
64. How much memory does a pointer variable allocate?
65. Why am I getting “warning: assignment of pointer from integer lacks a cast” for calls to malloc()?
66. Why does some code carefully cast the values returned by malloc to the pointer type being allocated?
67. Why isn’t a pointer null after calling free()? How unsafe is it to use (assign, compare) a pointer value after it’s been freed?
68. When I call malloc() to allocate memory for a pointer which is local to a function, do I have to explicitly free() it?
69. I’m allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer?
70. Must I free allocated memory before the program exits?
71. How does free() know how many bytes to free?
72. Can I query the malloc package to find out how big an allocated block is?
73. Is it legal to pass a null pointer as the first argument to realloc()?
74. What’s the difference between calloc() and malloc()? Is it safe to take advantage of calloc’s zero-filling? Does free() work on memory allocated with calloc(), or do you need a cfree()?
75. What is alloca() and why is its use discouraged?
76. Why doesn’t
strcat(string, ‘!’);
work?
77. How can I get the numeric (character set) value corresponding to a character, or vice versa?
78. What is the right type to use for Boolean values in C?
79. Is if(p), where p is a pointer, a valid conditional?
80. How can I write a generic macro to swap two values?What’s the best way to write a multi-statement macro?
81. Is it acceptable for one header file to #include another?
82. What’s the difference between #include and #include “” ?
83. What are the complete rules for header file searching?
84. How can I construct preprocessor #if expressions which compare strings?
85. Does the sizeof operator work in preprocessor #if directives?
86. Can I use an #ifdef in a #define line, to define something two different ways?
87. Is there anything like an #ifdef for typedefs?
88. How can I use a preprocessor #if expression to tell if a machine is big-endian or little-endian?
89. How can I list all of the predefined identifiers?
90. How can I write a macro which takes a variable number of arguments?
91. What is the “ANSI C Standard?”
92. What’s the difference between “const char *p” and “char * const p”?
93. Why can’t I pass a char ** to a function which expects a const char **?
94. What’s the correct declaration of main()?
95. Can I declare main() as void ?
96. What does the message “warning: macro replacement within a string literal” mean?
97. What are #pragmas and what are they good for?
98. What does “#pragma once” mean?
99. Is char a[3] = “abc”; legal? What does it mean?
100. Why can’t I perform arithmetic on a void * pointer?
101. What’s the difference between memcpy() and memmove()?
102. What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?
103. What’s wrong with this code?
char c;
while((c = getchar()) != EOF) …
104. Why does the code
while(!feof(infp)) {
a. fgets(buf, MAXLINE, infp);
b. fputs(buf, outfp);
}
copy the last line twice?
105. How can I read one character at a time, without waiting for the RETURN key?
106. How can I print a ‘%’ character in a printf format string?
107. What printf format should I use for a typedef like size_t when I don’t know whether it’s long or some other type?
108. How can I implement a variable field width with printf?
109. How can I print numbers with commas separating the thousands? What about currency formatted numbers?
110. Why doesn’t the call scanf(”%d”, i) work?
111. How can I specify a variable width in a scanf() format string?
112. Why doesn’t this code:
double d;
scanf(”%f”, &d);
work?
113. How can I tell how much destination buffer space I’ll need for an arbitrary sprintf call? How can I avoid overflowing the destination buffer with sprintf()?
114. What’s the difference between fgetpos/fsetpos and ftell/fseek?
115. What are fgetpos() and fsetpos() good for?
116. How can I redirect stdin or stdout to a file from within a program?
117. How can I read a binary data file properly?
118. How can I convert numbers to strings (the opposite of atoi)? Is there an itoa() function?
119. Why does strncpy() not always place a ” terminator in the destination string?
120. Why do some versions of toupper() act strangely if given an upper-case letter?
121. How can I split up a string into whitespace-separated fields? How can I duplicate the process by which main() is handed argc and argv?
122. How can I sort a linked list?
123. How can I sort more data than will fit in memory?
124. How can I get the current date or time of day in a C program?
125. How can I add N days to a date? How can I find the difference between two dates?
126. How can I get random integers in a certain range?
127. How can I generate random numbers with a normal or Gaussian distribution?
128. What does it mean when the linker says that _end is undefined?
129. When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
130. What’s a good way to check for “close enough” floating-point equality?
131. How do I round numbers?
132. Why doesn’t C have an exponentiation operator?
133. How do I test for IEEE NaN and other special values?
134. What’s a good way to implement complex numbers in C?
135. How can %f be used for both float and double arguments in printf()? Aren’t they different types?
136. How can I write a function that takes a variable number of arguments?
137. How can I write a function that takes a format string and a variable number of arguments, like printf(), and passes them to printf() to do most of the work?
138. How can I write a function analogous to scanf(), that calls scanf() to do most of the work?
139. How can I discover how many arguments a function was actually called with?
140. How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)?
141. How can I call a function with an argument list built up at run time?bobby
Posts: 398
Joined: Tue Jul 01, 2008 11:26 pmTop
0 Response to "More C Interview Questions"
Post a Comment