Question: You are given the assignment of writing a function that determines whether one string is longer than another. You decide to make use of the
You are given the assignment of writing a function that determines whether one string is longer than another. You decide to make use of the string library function strlen having the following declaration:

When you test this on some sample data, things do not seem to work quite right. You investigate further and determine that, when compiled as a 32-bit program, data type size_t is defined (via typedef) in header file stdio.h to be unsigned.
A. For what cases will this function produce an incorrect result?
B. Explain how this incorrect result comes about.
C. Show how to fix the code so that it will work reliably.
/* Prototype for library function strlen */ size_t strlen(const char *s); Here is your first attempt at the function: /* Determine whether strings is longer than string t */ /* WARNING: This function is buggy */ int strlonger (char *s, char *t) { return strlen(s) - strlen(t) > 0; }
Step by Step Solution
3.42 Rating (152 Votes )
There are 3 Steps involved in it
This example demonstrates a subtle feature of unsigned arithmetic and also ... View full answer
Get step-by-step solutions from verified subject matter experts
