Printing Output
The printf function is used to print text on the screen. The data which we print on screen is a text or a variable.
- If it is text, it is enclosed in quotation marks.
- If it is variable, name of variable is written without quotation marks.
Note: Do not forget to put ; at the end
Adding line breaks
\n: it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.
Different Escape Sequences
| Escape Sequence | Description | Code |
|---|---|---|
| \n | New line | printf("\n"); |
| \t | Tab - prints four spaces | printf("\t"); |
| \a | Beep sound | printf("\a"); |
| \b | Backspace | printf("\b"); |
| \f | Form feed | printf("\f"); |
| \r | Carriage return | printf("\r"); |
| \\ | It adds backslash | printf("\\"); |
| \' | Single quote | printf("\'"); |
| \? | Question mark | printf("\?"); |
| \0 | Null character | printf("\0"); |
Printing Numbers
If we want to print numbers in C, we store them in variables then print variables on screen using %d (for integers). We will discuss about this in detail in variable section. Here is a brief example.
Note: If we write numbers in double quotes, these are not treated as numbers. Everything inside double quotation marks is a string literal which is text.
Printing Different Types of Variables
Printing data stores in variables is not our concern for now. This is just for overview. You do not need to understand it here. Just go through it once.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP