Syntax Output
One of the first commands to learn in the C Programming Language is to print output text on the screen.
printf("Hello World\n");
printf is the command to print onto the screen.
\n is used to move to the next line. This "\n" is an example of an "escape sequence" used to print out special characters and/or format the text. Below is a table with the most common escape sequences we will use in class. Here is a link with even more escapes sequences for your reference: Link.
Activity 1
a. Edit the code above to print "Hello <your name>!"
b. Still using only one "printf" command, print three lines that say:
Hello <your name>!
Welcome to C Programming.
This is going to be fun.
Solution - Link
Activity 2
Use escape sequences to draw a picture of an animal. Link.
Solution with tips - Link (complex) - Link (simpler)
Printing Multiple Lines
There are a few ways to print on multiple lines:
Note #1 - If you are printing multiple lines in a single "printf()" statement, each line must begin and end in double quotations.
Note #2 - You need to manually include "\n" whenever you want to go to a new line, even a multi-line "printf()" statement will show up on a single line if you leave out the "\n" escape sequences.
FAQs
Question: Why does the program below give an error?
Program Code:
Error message on Repl.it:
Answer:
You can only have one main function. Inside of the "main" function, you can have many lines of code (millions even!). However, you cannot create the same function again with the same name. The correct way to write these two print statements is below.
Common Mistakes
1. Using "print()" and leaving out the 'f' - Flashbacks to Python
2. Leaving out the semicolon at the end of the line