Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Constants Questions
Pointers and qualifiers in C: What happens when calling fun(&ptr) if fun expects int ** but ptr has type const int * in the given program?
Const-correctness and arrays: Given a const int array in Turbo C and a function fun(int *), what is the outcome of calling fun(&arr[3]) after printing arr[3]?
Initialization of const from a function: What will be printed when const int x = get(); with get() returning 20 is executed and x is printed?
Binding a const to a non-const value: After int y = 128; const int x = y; what is printed by printf("%d ", x)?
Applying ++ to a const object: For const int i = 0; what happens when evaluating printf("%d ", i++);?
Printing through a pointer-to-const: In Turbo C, given const int *ptr pointing to i, then reassigned to j, what is the likely output of printing the pointer values (as hex) and the pointed integers?
Reading and printing a C string via a pointer-to-const: With const char *s = "" then s = str where str is "Hello", what does the loop while(*s) printf("%c", *s++); print?
Attempting to write into a const union member: Given const union employee e1; and strcpy(e1.name, "K");, what is the compilation result?
Dereferencing a pointer-to-const bound to a const object: With const int x = 5; const int *ptrx = &x; what happens when executing *ptrx = 10; then printing x?
C programming: what will be the output of this program on a standards-compliant compiler? #include
int main() { const c = -11; const int d = 34; printf("%d, %d ", c, d); return 0; }
C program diagnosis: modifying a const union and printing its members\n\n#include
\n#include
\n#include
\n\nunion employee {\n char name[15];\n int age;\n float salary;\n};\nconst union employee e1;\n\nint main()\n{\n strcpy(e1.name, "K");\n printf("%s", e1.name);\n e1.age = 85;\n printf("%d", e1.age);\n printf("%f", e1.salary);\n return 0;\n}
Const-correctness check in C: assigning a const char* to a non-const char* variable\n\n#include
\nconst char *fun();\n\nint main()\n{\n char *ptr = fun();\n return 0;\n}\nconst char *fun()\n{\n return "Hello";\n}
Turbo C (non-C99) diagnostic: using a const int variable as an array bound vs a #define constant\n\n#include
\n#define MAX 128\n\nint main()\n{\n const int max = 128; /* not an ICE in old Turbo C /\n char array[max]; / variable length array in old compiler → error /\n char string[MAX]; / OK: macro constant */\n array[0] = string[0] = 'A';\n printf("%c %c\n", array[0], string[0]);\n return 0;\n}
C const-correctness: attempting to modify a string literal through a const char* return value\n\n#include
\nconst char *fun();\n\nint main()\n{\n *fun() = 'A';\n return 0;\n}\nconst char *fun()\n{\n return "Hello";\n}
Const qualifier on the pointed-to data: diagnose the error when writing through a pointer to const\n\n#include
\n\nint main()\n{\n char mybuf[] = "India";\n char yourbuf[] = "CURIOUSTAB";\n char const ptr = mybuf; / pointer to const char */\n ptr = 'a'; / attempt to modify through const-qualified pointer /\n ptr = yourbuf; / re-pointing is allowed */\n return 0;\n}
Const object initialization rule in C: diagnosing assignment after declaration\n\n#include
\n\nint main()\n{\n const int x; /* uninitialized /\n x = 128; / attempted assignment after declaration */\n printf("%d\n", x);\n return 0;\n}
Type qualification mismatch: binding a pointer to non-const int to the address of a const int\n\n#include
\n\nint main()\n{\n const int k = 7;\n int const q = &k; / constant pointer to non-const int */\n printf("%d", *q);\n return 0;\n}
Const pointer vs pointer to const: identify the actual error in reassignment\n\n#include
\n\nint main()\n{\n char mybuf[] = "India";\n char yourbuf[] = "CURIOUSTAB";\n char const ptr = mybuf; / const pointer to char (pointer is fixed) */\n ptr = 'a'; / writing through ptr is allowed (mybuf is writable) /\n ptr = yourbuf; / ERROR: cannot reassign a const pointer */\n return 0;\n}