search
Log In
1 vote
227 views

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

in Programming
edited by
227 views

1 Answer

1 vote
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
0
your answer is correct but logic is incorrect.
0
Then please explain me the logic
0

@sushmita explain logic

Related questions

0 votes
1 answer
1
877 views
#include<stdio.h> #include<stdlib.h> int main(void) { int maxLineCount = 500, maxCharCount = 500, index, j, count; char *line = NULL; size_t size; char *a[maxLineCount]; for (index = 0; index < maxLineCount; index++) a[index] = (char *)malloc(maxCharCount * ... of the code is doing. suppose we have 3 string given as input in 3 different lines then how can we access each character of the string?
asked Jun 1, 2019 in Programming Satbir 877 views
0 votes
1 answer
2
108 views
#include<stdio.h> int main() { FILE *fp[2]; if((fp[0]=fp[1]=fopen("test.txt","w"))!=NULL) //test.txt file should be empty during first compiling { fputs("one",fp[0]); fclose(fp[0]); fputs("two",fp[1]); fclose(fp[1]); } return 0; } what will be printed ... and also am not understanding what is done after fputs("one",fp[0]); and fputs("two",fp[1]); can anyone tell me with explanation...please .
asked Jan 19, 2019 in Programming shiva0 108 views
0 votes
1 answer
3
334 views
main() { unsigned int i= 255; char *p= &i; int j= *p; printf("%d\n", j); unsigned int k= *p; printf("%d", k); } Both the outputs are -1. I have even tried with - int i = 255(3rd line). Still the output is -1. I don't understand how it is -1.
asked Jan 12, 2019 in Programming Psnjit 334 views
1 vote
0 answers
4
202 views
what is the result of comparing signed with unsigned number?? #include <stdio.h> int main() { unsigned int a = 5; if(a > -1) printf("5 is > -1\n"); return 0; }
asked Nov 15, 2018 in Programming Mahbub Alam 202 views
...