C Output

C Output: Printing to the Console

In C programming, outputting data to the screen is an incredibly common task. Whether you want to display a welcome message, show the result of a calculation, or debug your application, you need to know how to print text to the console.

The primary function used for outputting text in C is the printf() function.


1. Using the printf() Function

The printf() function outputs text exactly as you format it. To output a simple string of text, you place the text inside double quotes "" within the parentheses.

Basic Output Example

#include <stdio.h>

int main() { printf("Hello, World!"); printf("I am learning C."); return 0; }

The Issue with Newlines

If you run the example above, you might expect the output to be on two separate lines. However, the output will actually look like this: Hello, World!I am learning C.

Why? Because the printf() function does not automatically add a line break at the end of your text. It prints exactly what you tell it to, right next to the previous output.


2. The Newline Character (\n)

To move the cursor to a new line, you must explicitly use the newline character: \n.

When the compiler encounters \n inside a printf() string, it understands it as a command to start a new line, rather than printing the literal characters '' and 'n'.

Using Newlines

#include <stdio.h>

int main() { printf("Hello, World!\n"); printf("I am learning C.\n"); printf("This will be on a third line."); return 0; }

You can also use multiple \n characters together to create blank lines:

printf("Hello!\n\n\nHow are you?"); 
// This creates two blank lines before printing "How are you?"

3. Printing Variables (Format Specifiers)

The printf() function is powerful because it doesn't just print static text. It can print the values of variables dynamically. To do this, we use format specifiers.

A format specifier starts with a percentage sign % followed by a character indicating the type of data to be printed.

Printing Variables Example

#include <stdio.h>

int main() { int myAge = 25; char myGrade = 'A';

// The %d will be replaced by the value of myAge printf("I am %d years old.\n", myAge);

// The %c will be replaced by the value of myGrade printf("My exam grade is %c.\n", myGrade);

return 0; }

We will dive much deeper into variables and data types in the upcoming chapters!


4. Other Escape Sequences

The newline character \n is called an escape sequence. It "escapes" the normal string interpretation to perform a special action. There are a few other handy escape sequences in C:

Escape Sequence Description Use Case
\n New Line Moves text to the next line.
\t Horizontal Tab Inserts a large gap, useful for aligning text into columns.
\\ Backslash Prints a literal backslash \.
\" Double Quote Prints a literal double quote " inside a string.

Escape Sequence Examples

#include <stdio.h>

int main() { // Using tab to align data printf("Name\tAge\n"); printf("John\t22\n");

// Printing quotes requires " printf("They call him "The Programmer".\n");

return 0; }


Exercise

?

Which escape sequence is used to force text to continue on a new line?