From the course: Secure Coding in C
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
Reading input with fgets() - C Tutorial
From the course: Secure Coding in C
Reading input with fgets()
- [Instructor] In this code, the gets function fetches input and stores it in the named buffer. This is the original C language string input function, which lacks an input limit and can easily overflow. In fact, it's underscored here in the Code Spaces editor because it is a forbidden function, but it can build. Now, there was a warning, which you didn't see, but over here in the problems tab, you do see a problem. Even so, the code has been built and it's running right now, and it looks like it works, though some compilers will also build in a warning to any program that attempts to use the gets function. The proper replacement for gets is the F gets function. This function uses the input buffer as its first argument. The second argument is the size of the input buffer, the same value. Text input is limited to this size, minus one character, to account for the null character added to the end of the input string. The second argument is generally the same size or less than the buffer…