How to print whitespace in C?

Answered by John Hunt

To print whitespace in C, you can declare a character array and assign the whitespace characters to it. Then, you can use the printf function to print the array.

Here’s an example:

“`c
#include

Int main() {
Char whitespace[10] = ” “; // declare a character array with whitespace characters
Printf(“%s\n”, whitespace); // print the whitespace array
Return 0;
}
“`

In this example, we declare a character array called `whitespace` with a size of 10. We assign three whitespace characters (” “) to the array. The printf function is then used to print the contents of the `whitespace` array using the `%s` format specifier.

When you run this program, it will print three consecutive whitespace characters on the console.

You can also use escape sequences to represent whitespace characters in C. For example, the escape sequence `\t` represents a tab character, `\n` represents a newline character, and `\s` represents a space character. You can use these escape sequences directly in printf statements to print the desired whitespace.

Here’s an example using escape sequences:

“`c
#include

Int main() {
Printf(“This is a tab:\t\tThis is a space:\s\s\sThis is a newline:\n”);
Return 0;
}
“`

In this example, we use escape sequences to print a tab character, three space characters, and a newline character. The output of this program will show the effects of these escape sequences.

Printing whitespace in C can be done by assigning whitespace characters to a character array or using escape sequences directly in printf statements. Both methods allow you to control the amount and type of whitespace you want to print.