+ 1

What is the use of this code and how it work and why not work

#include <stdio.h> int main() { char a[100]; gets(a); printf("You entered: %s", a); return 0; }

18th Apr 2021, 8:49 PM
Melu
Melu - avatar
6 Réponses
+ 2
It reads in user input into the array 'a'. You shouldn't use this code though, because the gets function doesn't care about the size of the input. If the input is bigger than (in this case) 100 characters, it'll overflow the array. That is called a "buffer overflow", it's a serious security vulnerability. Use fgets instead, which can limit the amount of characters it'll read/write.
18th Apr 2021, 9:15 PM
inxanedev!
inxanedev! - avatar
+ 1
Please i want example about fgets
18th Apr 2021, 9:19 PM
Melu
Melu - avatar
18th Apr 2021, 9:20 PM
inxanedev!
inxanedev! - avatar
+ 1
Wow😍 tnx bro i like it
18th Apr 2021, 9:22 PM
Melu
Melu - avatar
+ 1
fgets(a, 100, stdin); /* a is the name of the arr 100 is the maximum char + \0 stdin means the standard input (input from the keyboard). */
19th Apr 2021, 2:08 AM
KissPain
KissPain - avatar