Learn

The main goal of our Hello World code example is to output the text "Hello World!" to the console. The line of code that outputs text is:

printf("Hello World!\n");

Let’s dive deeper into the 2 parts of this line are:

  • printf() is known as a function and performs the action of printing text to the console.
  • "Hello World!\n" is a string. A string is text in between a pair of double quotes.

Placing the string in between the parentheses of the printf() function prints the text (without the quotes) to the console.

Functions and strings are topics covered in later lessons. Don’t worry if you don’t understand the concepts fully. The important thing to note is that this is how we create output in the console.

What about the \n at the end of the string? Good question! This is called an escape sequence and is used to add a non-visual character within a string.

In this case, \n adds a new line to the end of the string. Look what happens when we place it in between Hello and World!:

printf("Hello\nWorld!");

The above code will output:

Hello World!

It’s important to remember an escape sequence is a character and must be within the double-quotes.

Another escape sequence is \t. This is equivalent to the tab key and will insert spaces within a string:

printf("Hello\tWorld!");

The above code will output:

Hello World!

\n and \t are just two of many different escape sequences that can be put inside a string.

Instructions

1.

The code in the workspace outputs some text to the console. This is supposed to be an easy-to-follow recipe but when you run the code you’ll see it is not that easy to read.

Run the code to move on and fix the recipe.

2.

There are no newlines in the text.

Starting with the first printf() statement in main():

  • Add a newline escape sequence to the end of the string.

Run the code to see if the recipe looks better.

3.

The next statement needs a bit of space in the middle.

Within the second printf() statement in main():

  • Add a tab escape sequence in between the words butter and (Room Temperature)
4.

This recipe needs to be sweeter.

Within main():

  • Add a printf() statement that outputs the string: "\n2/3 Cups: Granulated Sugar"

Notice the \n at the front of the string to ensure it is output on a new line.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?