The GATE Grind

GATE 2024 CS (CS2) – Question 33

Programming and Data Structures · Programming in C · 1 mark · Multiple select

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?

  1. The function call fX("abcd") will always return a value
  2. Assuming a character array c is declared as char c[] = "abcd" in main(), the function call fX(c) will always return a value
  3. The code of the function will not compile
  4. Assuming a character pointer c is declared as char *c = "abcd" in main(), the function call fX(c) will always return a value

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.