Escape Sequences ๐Ÿชœ#

Escape sequences are a special sequence of characters beginning with \ that represent a single character. They are often used to write out untypeable characters, such as new lines, tabs, or double-quotation marks inside of a string.

Hereโ€™s a list of common escape sequences:

Escape Sequence

Inserted Character

Description

\n

Inserts a newline.

This escape sequence is needed because you canโ€™t just hit enter to get a newline โ€” that would break Cโ€™s syntax.

\t

Inserts a tab.

This escape sequence is needed because often you canโ€™t just hit tab to get a tab character โ€” that would break Cโ€™s syntax.

\%

Inserts a % character.

This escape sequence is needed when writing format strings, because format specifiers start with %. (ie. %d, %s)

\'

Inserts a ' (single-quote).

This escape sequence is needed when writing the single-quote character, as characters are surrounded by single-quotes. (ie. โ€˜aโ€™, โ€˜bโ€™, โ€˜\โ€™โ€™)

\"

Inserts a " (double-quote).

This escape sequence is needed when using double-quotes inside of a string, because strings are surrounded by double-quotes. (ie. โ€œThis is an \โ€apple\โ€.โ€);

\\

Inserts a \ (backslash).

This sequence is needed because the start of every escape sequence is a backslash.

Ex.

// Escape characters are needed here because double quotes are
// already used to denote the start and end of a string.
printf("My name is \"Bob\"");

// Add a tab between "I am" and " spaced out!"
printf("I am \t spaced out!");

Tasks ๐ŸŽฏ#

Add a tab between โ€œappleโ€ and โ€œbananaโ€ in print statement for the code below.

You should get the output:

apple   banana
#include <stdio.h>

int main() {
    printf("apple banana");
}
Solution โœ…
#include <stdio.h>

int main() {
    printf("apples\tbananas");
}

Add a newline between the two sentences that are printed in code below.

You should get the output:

Hello there!
The weather's looking fine today.
#include <stdio.h>

int main() {
    printf("Hello there! The weather's looking fine today.");
}
Solution โœ…
#include <stdio.h>

int main() {
    printf("Hello there!\nThe weather's looking fine today.");
}