From the course: C: Data Structures, Pointers, and File Systems
Sending string output - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
Sending string 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.
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
-
-
-
-
-