Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

GATE | GATE-CS-2017 (Set 1) | Question 55

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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\n", 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 _________.

Note: This questions appeared as Numerical Answer Type.
(A) 2
(B) 3
(C) 4
(D) 5

Answer: (B)

Explanation: ((strlen(s) – strlen(t)) > c) ? strlen (s) : strlen (t)
= (3 – 5 > 0)
= (-2 > 0)

Important point here is while comparing -2 with c,  result will be a positive number as c is unsigned. So, out of these two , strlen (s) will be printed. Therefore, option B is correct.

See the code for clarification :

Quiz of this Question

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads