C printf
Created By: chatGPT
C's
printf
function is part of the C standard library and allows you to output formatted text to the console. This function is extremely versatile and can be used to display strings, integers, floating-point numbers, and more.#include <stdio.h>
int main() {
int number = 10;
float pi = 3.14;
char *text = "Hello, World!";
printf("Integer: %d\n", number);
printf("Float: %.2f\n", pi);
printf("String: %s\n", text);
return 0;
}
Format specifiers are used in the
printf
function to define the type of data being printed. For example, %d
is used for integers, %f
for floating-point numbers, and %s
for strings.printf("int: %d\n", num);
printf("float: %f\n", decimal);
printf("string: %s\n", str);
You can also control the width and precision of the output. For example, specifying a width will adjust how much space the number takes when it is printed.
printf("%5d\n", 42); // prints ' 42' (4 spaces before 42)
printf("%.2f\n", 3.14159); // prints '3.14'
To print multiple variables, simply insert additional arguments into the
printf
function corresponding to each format specifier.int a = 5, b = 10;
printf("a: %d, b: %d\n", a, b);
Escape sequences are sequences starting with a backslash (
\
) that allow you to format the output. Common escape sequences include \n
for new line and \t
for tab space.printf("Hello,\nWorld!\n"); // Outputs 'Hello,' on one line and 'World!' on the next