You can always craft your own string output functions. Otherwise, the C library features two common ones: puts() and printf(). The puts function outputs a string variable or literal and always appends a newline. The printf() function outputs a string directly or uses the percent-s placeholder to represent a string argument. Unlike the puts() function, printf() does not automatically append a newline to its output.
- [Instructor] In this exercise file, I've concocted my own string output function by using a while loop and the putchar function. A string literal is declared at line five. The while loop's condition at line nine states that the loop repeats as long as the value of element a doesn't equal the null character which the compiler automatically appends to the end of a string literal. The putchar function within the loop sends a single character from the string to standard output and then variable a is incremented which helps the loop march through the string. Build and run. And you are. Because the putchar function also returns the character it sends to standard output, you can reduce the loop by setting the function as the while loop's condition is shown here. The output is the same. And because the null character is interpreted as false by the compiler, you can remove that test. In this modification, the putchar function itself is the test. When it returns the null character, the loop stops. And finally, you can eliminate the loop's statement by appending the increment operator to variable a as shown here. Build and run. And the tidy little loop outputs the text. You don't really need to write your own string output function because the C library has a few. The puts functions at lines seven and eight in this exercise file send a string literal or variable to standard output. At line seven, a string literal is used. Line eight specifies the string character array declared at line five in the code. The puts function automatically appends a new line character to the output. You don't see the new line but you see its effect here and here. When you don't want the new line, you can use the printf function as shown in this exercise file. The string literal, hello earth, is output directly. There is no new line in the output because there was no new line in the string. It might look different and it might be difficult to see on your computer, but here you can see there is no space between the output text and the text that's supplied by code blocks. The first argument in the printf function is really a format string which you can use to generate output as shown at line five in this code. Line six however, uses that format string. The first argument specifies %s, which is a placeholder for a string, and the second argument is the string to output. Build and run. The printf function is powerful with lots of potential for string output, but at its basic level, it works well to send a line of text to standard output.
- 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.