From the course: C: Data Structures, Pointers, and File Systems

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Fetching string input

Fetching string input - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Fetching string input

- [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. That's because…

Contents