Complementary task for topic: 1
M Nemeth · 2023-08-29 15:21:04.602220'
Printf: text, tabulator
Printf: text, tabulator
Task: Print a line with tabbed text
Write a C program that uses printf() to display a line of text with tabbed sections.
Output like:
Item Quantity Price
Apple 3 $0.99
Banana 5 $0.50
Orange 2 $0.75
Hint: The \n creates a newline (newline character, enter...), \t creates a tabulator
Usually we can add white space characters with \, therefore the printf("\") results in error. To print a backslash you need to use \\ e.g.: printf("\\") will print out: \
Solution
#include
int main() {
printf("Item\tQuantity\tPrice\n");
printf("Apple\t3\t$0.99\n");
printf("Banana\t5\t$0.50\n");
printf("Orange\t2\t$0.75\n");
return 0;
}
Explanation
In this example, the program uses printf() to display a line of text. The \t escape sequence is used to insert a tab character between the sections, creating consistent spacing between the columns.