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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!