GateAI
Programming in C

GATE CSE 2017 Set 1 · Q53

NAT2 marksEasy

Time

Consider the following C program.#include <stdio.h> #include <string.h> void printlength(char *s, char *t) { unsigned int c = 0; int len = ((strlen(s) - strlen(t)) > c) ? strlen(s): strlen(t); printf("%d ", len); } void main() { char *x = "abc"; char *y = "defgh"; printlength(x,y); }Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int. The output of the program is _________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 3 (±0)

Programming in C
  1. In main(), x = "abc" has length 33, and y = "defgh" has length 55.

  2. In printlength(s, t):

    • strlen(s) returns (size_t) 3

    • strlen(t) returns (size_t) 5

  3. The subtraction strlen(s) - strlen(t) is performed using unsigned arithmetic:

    3U−5U=UINT_MAX−13\text{U} - 5\text{U} = \text{UINT\_MAX} - 1

    which results in a very large positive unsigned integer (e.g., 42949672944294967294 on a 32-bit system).

  4. The comparison (strlen(s) - strlen(t)) > c compares this large unsigned value with c = 0U, which evaluates to true (11).

  5. Since the condition is true, the ternary operator selects strlen(s), which is 33.

  6. Therefore, len = 3, and `printf("%d

", len)` prints 3.

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 Q53 of the GATE CSE 2017 Set 1 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. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2017 Set 1 has 4.

How GATE tests Programming in C

More from GATE CSE 2017 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2017 Set 1 · Q53 · NAT

Consider the following C program. #include #include void printlength(char *s, char *t) { unsigned int c = 0; int len = ((strlen(s) - strlen(t)) > c) ? strlen(s): strlen(t); printf("%d ", len); } void main() { char *x = "abc"; char *y = "defgh"; printlength(x,y); } Recall that strlen is defined in string.h as returning a value of type sizet , which is an unsigned int . The output of the program is …

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.