In C Language, `printf()` is a standard library function that prints formatted output to the standard output stream on the console.
It is declared in the header file
The printf() function allows us to display text, variables, and expressions in a formatted manner.
int printf(const char *format, ...);
format: A string that specifies the format of the output. It may contain format specifiers, which are placeholders for the values of variables or expressions.
"...": A variable number of arguments that correspond to the format specifiers in the format string.
Format specifiers begin with the percent sign "%" and are followed by a character that specifies the type of data to be printed.
Some commonly used format specifiers are:
"%d" or "%I": we can print the integer values of variables using format specifiers.
"%f": we can print the Floating Point values of variables using format specifiers.
"%c": we can print the Character values of variables using format specifiers.
"%s": We can print literal text by including it directly in the formatted string.
"%x" or "%X": we can print the Hexadecimal values of variables using format specifiers.
"%p": we can print the Pointers values of variables using format specifiers.
#include <stdio.h> int main() { int numberValue = 10; float floatValue = 3.14; char characterValue = 'A'; char stringValue[] = "Hello World, in C Language!"; int *integerPtr = &numberValue; char *characterPtr = &characterValue; // Print value in integer format printf("Integer Value: %d\n", numberValue); // Print value in float format printf("Float Value: %f\n", floatValue); // Print value in character format printf("Character Value: %c\n", characterValue); // Print value in String format printf("String Value: %s\n", stringValue); // Print integer in hexadecimal format printf("Hexadecimal Value: %x\n", numberValue); // Print value of pointer address printf("Pointer address of:\n Integer value: %p;\n Character value: %p;\n", (void*)integerPtr, (void*)characterPtr); return 0; }
Integer Value: 10 Float Value: 3.140000 Character Value: A String Value: Hello World, in C Language! Hexadecimal Value: a Pointer address of: Integer value: 0x7ffd113acf84; Character value: 0x7ffd113acf83;
it allows us to control the output format, making it versatile for displaying different values.
It's one of the fundamental functions in C for outputting information to the user or to a file.