UNDERSTANDING THE printf FUNCTION IN C
Introduction
Print formatted, shortened as printf is the main function used in printing formatted output to a terminal in C language. The basic version of printf use takes a string as an argument and print the exact text, for instance:
printf(“hello”); prints the simple text, hello.
However, an elaborate use of the function takes an argument, then a list of values separated by comma(s) to match their associated format specifiers in the string.
Consider these two examples below:
Example 1. Hello, World
#include <stdio.h>
/**
* main — displays printf message
* Return: 0 (Success)
*/
int main(void)
{
printf(“Hello, World\n”);
return (0);
}
/** This example is another simple application of printf but the alternate special character \n is used here to print a newline (more on special characters and format specifiers below) **/
Example 2. Printing “Myself”
#include <stdio.h>
/**
* main — Entry point
* Return: 0 (Success)
*/
int main(void)
{
char name[] = “Abena”;
int age = 31;
printf(“My name is %s, I am %d years old\n”, name, age);
return (0);
}
Note: I recommend the Betty coding and documentation style for best practices.
/** In this example, the format specifiers %s (string) and %d (integer) indicates that a string followed by an integer value should be printed. That is, the variable, name is substituted for the %s specifier whilst the variable, age represents %d in the format string, and the resulting string is printed to the terminal **/
Special Characters
They are essential symbols (e.g. \, *, “, etc) in C programming that allows users to perform specific tasks or represent certain values. For instance, the double quotes (“, ”) sign are used to start and end a string while the percentage (%) symbol is used before some characters for formatting. To distinguish from its natural special meaning, the backslash symbol (\) is used as an escape character in front of special characters. See below an illustration:
Alternatively, there are some regular characters that becomes special when a backslash is placed before it; a common example is the newline character \n. That is, without the escape character (\), the letter n on its own is just a letter. Some other examples are \t for tab and \b for backspace.
Format/Conversion Specifiers
All format specifiers are written as the percent (%) sign in front of a letter (usually, data types). Some common types are %c, %s, %d…You can include more than one format specifier in a printf string as in Example 2 above.
The table below explains some common conversion specifiers:
Specifier Representation
%d / %i: Signed decimal integer
%u: Unsigned decimal integer
%c : Single character
%s : Prints string characters
%o: Unsigned octal integer
%x / %X: Unsigned hexadecimal (lower/upper)
Conclusion
printf is included in the standard library header #include <stdio.h> and is the C language’s standard function for displaying formatted output to the terminal. Understanding some concepts like special characters, format specifiers, width, precision etc. helps break down how the function works.
Further Related Learning Resources:
printf Basics: Watch This
Secrets of printf: Read This
Happy Learning 👩🏿💻