C Language FAQ and IAQs (infrequently)
The FAQs and the IAQs
31 flashcards · Programming Languages · by kgashok
What will the output be? 2147483650 Non determinable -2147483646 None of the above
Answer is 3. Actually, The best answer is 4. It depends on the compiler. Play with it at https://bit.ly/35eu9HF The INT_MAX (from https://en.cppreference.com/w/c/types/limits) is +2147483647.
A program needs to store integer values that will not be greater than the value of 2**32. To reliable accomplish this, what is the preferred data type of the variable so that memory usage is optimum?
unsigned int value;
Will the function do_something get called?
No.
char *string1 = "Hello"; char *string2 = "Hello"; if (string1 == string2) func_call(); Will the function func_call get called?
No. Unless == has been explicitly overloaded, this is the wrong way to compare the value of two strings. What is really being compared here are the values of the two pointers. To compare the values of the strings (which is much more likely to be what is wanted), use strcmp. The error is classified as a "model" error, since the user may well have a wrong model about what == can do. Play with this at https://bit.ly/37lVPe9
This C function for finding the minimum value is not working properly. Where is the problem? Inside the for-loop or outside?
Outside the for loop.
enum Foo { A, B, C=10, D, E=1, F, G=F+C }; What is the value of D, F and G?
D = 11 F = 2 G = 12
and we have this below: What will be the output of Line 7? a) 0 b) 1 c) Non-determinable d) memory_order is not a type. Compile error! e) 4
e) 4 (four). Play with it at https://bit.ly/3tHGCMa
Write your own my_strlen(const char str*) function that returns the length of a string. It must be a one-liner.
int my_strlen(const char *str) { return strchr(str, 0) - str; } Incredible, isn't it?! Try it out at https://bit.ly/3KKPmrP
What is the output of Line 13?
a a a a
What will be the output of Line 13 and Line 15?
a a a aA A A A Explore further at https://bit.ly/3vQhbKS
What will the output? A) 7, 8 B) Will not compile C) Undeterminable
A) 7, 8 ...can assign arrays wrapped in structs! Play with it at https://bit.ly/3tKzsa0
Write a C program that will not accept a integer from the standard input. It must accept any value in the range 0 to 65,535, ignoring anything else. After reading the integer, print the same integer. CLUE: If you use the right data type, you can avoid using any conditionals.
int main() { unsigned short usmall; puts("input a value that is less than 65536 "); scanf("%u", &usmall); printf("%d\n", usmall); return 0; } Play with it at https://bit.ly/3ItsU5M
Write a C program that will not accept a integer from the standard input. It should avoid all values greater than 32,767, regardless of how big a number is inputted. After reading the integer , print the same integer. CLUE: If you use the right data type, you can avoid using any conditionals.
int main() { short int small; scanf("%h", &small); printf("%h", small); return 0; } Play with it at https://bit.ly/3ItsU5M
What is the triangular number sequence ?
It is the representation of the numbers in the form of equilateral triangle arranged in a series or sequence. These numbers are in a sequence of 1, 3, 6, 10, 15, 21, 28, 36, 45, and so on. The numbers in the triangular pattern are represented by dots. The sum of the previous number and the order of succeeding number results in the sequence of triangular numbers.
A program below is given to generate the triangular number sequence. What is wrong with it? Where do you think the error is at? At which line number(s)?
Line 17.
pixels variable had the initialized value of 0 prior to running the following code snippet. After running the snippet, the value in pixels will be 0 numcols * numrows numcols + numrows numrows None of the above
Answer is 5. None of the above. What is the right answer?
What will the output of this code? A. It will not compile B. Statement 9 will produce the output "55 6". C. Statement 12 will fail Against each statement, choose "True" or "False"
A. False B. True and False. Depends where you compile and run. C. False and True. Depends where you compile and run. For alternative output, play at https://bit.ly/3M9Db9e
The following code does not compiling: typedef struct { char *data; NODE_PTR next; } NODE_PTR; How will you fix this? Give it a try at https://bit.ly/3vudquD
typedef cannot be used while it is still being defined.
A function is supposed to return a string. But when the function returns to the caller, the returned string contains garbage. Why? #include <stdio.h> char *itoa(int n) { char retbuf[20]; sprintf(retbuf, "%d", n); return retbuf; }
When a function returns, its automatic, local variables are discarded, so the returned pointer in this case is invalid (it points to an array that no longer exists). So how to fix this?
What is dangling pointer?
A dangling pointer a pointer that points to freed memory address.
When a function returns, its automatic, local variables are discarded, so the returned pointer in this case is invalid (it points to an array that no longer exists). So how to fix this dangling pointer problem? #include <stdio.h> char *itoa(int n) { char retbuf[20]; sprintf(retbuf, "%d", n); return retbuf; }
There are three options to this "dangling pointer" problem. Read more about it in the documentation at https://en.cppreference.com/w/c/language/lifetime Use a static variable. Get a pre-allocated string to populate with the result. Use malloc to allocate memory from the heap and ensure caller frees it after use.
Implement the static variable approach to make this function work properly. #include <stdio.h> char *itoa(int n) { char retbuf[20]; sprintf(retbuf, "%d", n); return retbuf; } Play with the code at https://bit.ly/3tyoNit
Eliminating Dangling Pointer using static <TO_BE_DEFINED>
Implement a pre-allocated buffer to be provided to make this function work properly. #include <stdio.h> char *itoa(int n) { char retbuf[20]; sprintf(retbuf, "%d", n); return retbuf; } Play with the code at https://bit.ly/3tyoNit
Eliminating Dangling Pointer using pre-allocated buffer <TO_BE_DEFINED>
Use malloc to allocate memory from the heap to make this function work properly. #include <stdio.h> char *itoa(int n) { char retbuf[20]; sprintf(retbuf, "%d", n); return retbuf; } Play with the code at https://bit.ly/3tyoNit
Eliminating Dangling Pointer using malloc <TO_BE_DEFINED>
Write your own version of strcat(char *dest, const char *src) using pointer logic. Exercise in Kernighan and Ritchie, Chapter 5, Page 107.
void strcat(char *s, const char *t) { while(*s++); /* Get to the end of the string */ s--; /*get back to the end of the string.*/ while((*s++ = *t++)); Play with it at https://bit.ly/3i5Xc2M
A. The above code will compile. And executable will run to completion. B. The above code will not compile. No executable will be produced. C. The above will compile. But segmentation fault will happen. D. "It depends".
C or D. The code will compile. But it will cause segmentation fault for most compilers on most platforms. The matrix requires 40MB of data to be allocated on the stack. That is far above the limits set for the stack size.
Very often, there is a need for big sized matrix, for computing and manipulations. There are two ways to allocate arrays in C: static arrays (fixed size) by allocating on the stack dynamic arrays (variable size) by allocating on the heap which is the preferred choice?
Dynamic Array allocation.
Suitably modify the data type of big so that it can still be used as big[1][10] or big[20][30] Write code in the indicated section for dynamic allocation of memory to which big should point to.
<TBA>
Draw a visualization of a dynamically memory allocated two dimensional array
A void pointer (on which no pointer arithmetic can be done) has been assigned a chunk of dynamically allocated memory of 1000 bytes. Write a program to examine the contents of the memory, byte by byte. Print the content of the memory if it is not equal to zero.
Cast the void pointer to a character pointer. Perform the incremental traversal byte by byte using the character pointer. Play with it at https://bit.ly/3IQBsmI
What will the output be? Won't compile Undeterminable 10 1410165408 10000100000 Hello World! None of the above
Answer: 3 See https://stackoverflow.com/a/2191323/307454 Play with the code at https://bit.ly/3uN5uUx