In C programming, output refers to the data that is sent from the program to the screen or any other output device. The printf() function is the most frequently used function for printing text, numbers, and variables to the console.
The printf() function is part of the stdio.h library. It stands for "print formatted." To use it, you must include the standard input-output header file at the top of your code.
By default, printf() does not insert a new line at the end of the output. If you use multiple printf() statements, they will appear on the same line. To move to a new line, use the \n escape sequence.
Escape sequences are special characters used inside strings to represent things like tabs, new lines, or quotes.
| Escape Sequence | Description |
|---|---|
\n |
Inserts a New Line. |
\t |
Inserts a horizontal Tab space. |
\\ |
Inserts a Backslash character. |
\" |
Inserts a Double Quote character. |
To print variables (numbers or characters), you must use **Format Specifiers**. These tell the printf() function what type of data it is printing.
%d or %i - Used for Integers (whole numbers).%f - Used for Float (decimal numbers).%c - Used for Characters.%s - Used for Strings (text).You can mix plain text and format specifiers within a single printf() function. The values of the variables will be inserted at the position of the specifiers in the order they are listed.
When printing floating-point numbers, C often prints many zeros after the decimal. You can control the precision by using a dot (.) followed by a number between the % and the f.
%d specifiers but only one variable, the program will output random "garbage" data.