Question
C PROGRAMMING Exercise one Here is a demonstration C program bug.c: #include #include int main() { char s[100]; gets(s); if (atoi(s) == 12) printf(do something
C PROGRAMMING Exercise one
Here is a demonstration C program bug.c:
#include#include int main() { char s[100]; gets(s); if (atoi(s) == 12) printf("do something "); printf("in any case, do something else "); return(0); }
Compile and run it. (We'll discuss the warning about "gets" in a moment; ignore that at first.)
Try typing the secret value 12, and try typing other numbers.
This seems fine, but it it uses the gets() function. If you did not see the compiler warning about gets(), you omitted the "Wall" option to gcc. Don't do this. Run it again with "Wall" to see the warning. You should always compile with "gcc Wall".
What is wrong with the gets() function? The problem is that it does not take a parameter specifying the size of the array, so it is impossible for it to be written properly and it is impossible for any program which calls it to be correct! If you type more than 99 characters, your program will exceed the 's' array bounds. If you're lucky, your program will simply crash.
1. Cause this program to crash with "segmentation fault".
2. Fix the bug without changing the size of s.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started