GATE 2024 CS (CS2) – Question 33
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?
Practise this question in The GATE Grind →
Show answer and explanation
Correct 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
Explanation
The function only reads the null-terminated string and returns its length, so it works for a literal, a char array, and a char pointer to a literal. The code compiles fine.