String input can be accomplished by using the scanf() or fgets() functions. The scanf() function is limited in that it stops reading after the first whitespace character. Buffer overflow is also an issue with scanf(). The fgets() function limits text input but it also includes a newline character in the buffer.
- [Instructor] In this code, the scanf function … is kajiggered to read a string at line eight. … It's not the best option, but it's often listed … in C language textbooks and other material. … The %s placeholder represents string input. … The string will be stored in the input array, … a buffer of 10 characters declared at line five. … Now, an ampersand isn't need to prefix … the variable name at line eight as input is an array. … The output generated at line nine, again, … uses the %s placeholder. … Build and run. … My name is Dan. … Pleased to meet you, Dan. … Two issues arise with using scanf in this manner. … The first is that the function stops reading … after a whitespace character is typed. … I'll run the code again, … and pretend my name is Jim Bob. … And only the text up until the whitespace is read. … The second, more deadly, issue is overflow. … Say my name is Bartholomew. … This name is longer than there are characters available … in the buffer. … And, of course, it works, but the program could err. …
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions
Skill Level Intermediate
1. Arrays and Structures
2. Characters and Strings
4. Files and the Operating System
5. The Interesting and Extraordinary
Next steps1m 52s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.