Practice Problem 2.26 (solution page 151)
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:
/* Prototype for library function strlen */
size_t strlen(const char *s);
Here is your first attempt at the function:
/* Determine whether string s is longer than string t */
/* WARNING: This function is buggy */
int strlonger(char *s, char *t) {
return strlen (s) - strlen (t) > 0;
}
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 is defined (via typed) 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.
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
EBK COMPUTER SYSTEMS
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (7th Edition)
Starting Out With Visual Basic (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
Starting Out with C++: Early Objects
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY