GateAI
Programming in C

GATE CSE 2024 Set 2 · Q33

MSQ1 markMedium

Time

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?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified 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 C

The given function fX calculates the length of a null-terminated string (similar to strlen):

  1. It initializes a pointer b to the start of the string a.

  2. The loop while(*b) traverses the characters until it encounters the null-terminating character \0 (which evaluates to 00 / false).

  3. 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 44. 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 44. 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 44. Hence, D is TRUE.

Thus, statements A, B, and D are TRUE.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

Where do you stand for GATE?

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.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

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 C

More from GATE CSE 2024 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2024 Set 2 · Q33 · MSQ

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.