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 - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Using single character I/O4m 4s
-
(Locked)
Testing characters2m 34s
-
(Locked)
Working with character conversions2m 17s
-
(Locked)
Understanding strings in C3m 3s
-
(Locked)
Fetching string input3m 18s
-
Sending string output2m 46s
-
(Locked)
Reviewing printf() placeholders3m 8s
-
(Locked)
Using printf() placeholders3m 36s
-
(Locked)
Exploring string functions3m 29s
-
(Locked)
Manipulating strings3m 53s
-
(Locked)
Avoiding string problems2m 27s
-
(Locked)
Challenge: String I/O and manipulation1m 46s
-
(Locked)
Solution: String I/O and manipulation2m 16s
-
-
-
-
-