Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C program.
    #include
    #include
    void print length (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 ________.
    1. 0
    2. 1
    3. 2
    4. 3
Correct Option: D

In the given program.
X is pointer of string “abc” which length is 3.
Strlen (S) = 3
where (S) is pointer that pointed to X.
Y is pointer of string “defgh”, which length is 5.
Strlen (t) = 5.
where t is pointer that pointed to y, value of C is 0.
Now consider the statement :
Intlen = [(Strlen (S) – Strlen (t) > 0)? Strlen (S): Strlen (t)]
= [(3 – 5)] > 0
= [–2] > 0
It is not true but, since it is given in question
[Strlen (S) – Strlen (t)] will give unsigned value.
So, [Strlen (S) – Strlen (t)] = |–2| = 2.
Therefore
⇒ 2 > 0 (it is true): Strlen (S): Strlen (t);
Since (2 > 0) will evaluate true so it will give output as
Strlen (S) = 3. Hence ‘3’ will be printed.



Your comments will be displayed only after manual approval.