Consider the following C function definition. int fX(char *a) { char *b = a; while(*b) b++; return b - a; } Which of the following statements is/are TRUE?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2024 Set 2 · Q33
Time
int fX(char *a) {
char *b = a;
while(*b)
b++;
return b - a;
}Which of the following statements is/are TRUE?Select all that applyOne or more correct
Solution
Official answer: A) The function call fX("abcd") will always return a value; B) Assuming a character array c is declared as char c[] = "abcd" in main() , the function call fX(c) will always return a value; D) Assuming a character pointer c is declared as char *c = "abcd" in main() , the function call fX(c) will always return a value
Programming in CThe given function fX calculates the length of a null-terminated string (similar to strlen):
It initializes a pointer b to the start of the string a.
The loop while(*b) traverses the characters until it encounters the null-terminating character \0 (which evaluates to / false).
It returns the pointer difference b - a, which gives the integer length of the string.
Evaluating the statements:
Option A: Passing a string literal "abcd" provides a pointer to a valid null-terminated character sequence ('a', 'b', 'c', 'd', '\0'). The function terminates and returns . Hence, A is TRUE.
Option B: An array declared as char c[] = "abcd" is null-terminated and decays to char * when passed to fX(c), returning . Hence, B is TRUE.
Option C: The syntax of the function is completely valid C code and will compile without errors. Hence, C is FALSE.
Option D: A pointer initialized as char *c = "abcd" points to a valid null-terminated string literal, and passing it to fX(c) returns . Hence, D is TRUE.
Thus, statements A, B, and D are TRUE.
Try the question first, then check your work.
Examiner trap
Analyze what the while(*b) loop does when traversing null-terminated strings and whether string literals in C are automatically null-terminated.
This Programming in C previous year question is Q33 of the GATE CSE 2024 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. This is the only 1-mark MSQ among them, and GATE CSE 2024 Set 2 has 4.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.