The C library printf() function is a fundamental tool for outputting formatted text to the standard output stream. It allows for versatile printing of variables, strings, and other data types.
SyntaxFollowing is the C library syntax of the printf() function −
int printf(const char *format, ...)Parameters
Following is the list of parameters −
Returns the number of characters printed (excluding the null byte used to end the output to strings) if successful.On error, it returns a negative value.
Example 1: Printing Integer and StringHere, printf() prints an integer and a string using format specifiers %d and %s respectively.
Below is the illustration of the C library printf() function.
#include <stdio.h> int main() { int num = 10; char str[] = "Hello"; printf("Integer: %d, String: %s\n", num, str); return 0; }Output
The above code produces following result −
Integer: 10, String: HelloExample 2: Printing Octal and Hexadecimal Numbers
Here, the printf() prints an octal number (octal_num) using %o format specifier and a hexadecimal number (hex_num) using %X format specifier.
#include <stdio.h> int main() { // Octal representation of 61 int octal_num = 075; // Hexadecimal representation of 31 int hex_num = 0x1F; printf("Octal: %o, Hexadecimal: %X\n", octal_num, hex_num); return 0; }Output
After execution of above code, we get the following result
Octal: 75, Hexadecimal: 1F
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4