• edited by
6,803 views
1 1 vote

The following is a C code-

int main(void)
{
    char buff[10];
    memset(buff,0,sizeof(buff));
    gets(buff);
    printf(" The buffer entered is [%s] ",buff);
    return 0;
}

(A) The program assigns a memory area to buff successfully and works fine.

(B) The program may not compile properly.

(C) The program is compiled  but it may lead to buffer over overflow sometimes.

(D) The program has no issue

1 Answer

0 0 votes
The problem with the code above is the use of the function gets(). This function accepts a string from stdin without checking the capacity of buffer in which it copies the value. This may well result in buffer overflow. The standard function fgets() is advisable to use in these cases.

Therefore option C is correct
Position:
Show:

Related questions

6 6 votes
4 4 answers
2.1k
2.1k views
GO Classes asked May 2, 2022
2,109 views
What will be output printed by the following program?#include<stdio.h main() { int c=4; switch(c) { c=c-1; case 4: printf("IITB ")...
7 7 votes
2 2 answers
1.2k
1.2k views
GO Classes asked May 2, 2022
1,228 views
Trace the execution of the following if statements and select the output from this code fragmentint main() { int a = 3, b = 7 ; if ( (a + b) % 2 != 0 ) { printf("tetrahed...