Chapter 1 · Lesson 2 of 5
02. Printing more lines
Several printf lines, \n for a new line and \t for a tab: how to lay out text on the screen.
C runs the lines inside main one after another, from top to bottom. Each printf adds to the screen where the last one stopped, so it's \n that starts a new line, not a new printf.
#include <stdio.h>
int main() { printf("Ready, "); printf("steady, "); printf("go!\n"); printf("One\nTwo\n"); return 0;}It shows
Ready, steady, go! One Two
\t is a tab: a jump to the next column, handy for lining things up like a table.
#include <stdio.h>
int main() { printf("Item\tPrice\n"); printf("Idli\t30\n"); printf("Dosa\t50\n"); return 0;}It shows
Item Price Idli 30 Dosa 50
Your turn
1Steps
- 1Print this little timetable, using
\tbetween the day and the subject and\nat the end of each line: - 2
MondaytabMaths, thenTuesdaytabScience, thenWednesdaytabEnglish.
Write it in main.c. If it reads input, put the answers in Program input, under the terminal.
Use: printf()
Starting code
The editor starts empty. Copy this into it to begin, or write your own from scratch.
#include <stdio.h>
int main() { printf("Monday Maths Tuesday Science Wednesday English"); return 0;}2Expected output
It should show
Monday Maths Tuesday Science Wednesday English
3Hints
Stuck? Open one hint at a time: each gives a bit more help.
Hint 1
Each day needs its own line, so each needs a \n at its end.
Hint 2
Between the day and its subject, use \t instead of a space.
Hint 3
The first line is printf("Monday\tMaths\n"); and the other two are made the same way.