Category

Saturday, January 28, 2023

Printf statement in C Language

 The printf() function is a standard C library function that is used to output text to the console. It is included in the stdio.h header file and is used to print characters, strings, and other data types to the screen.

The basic syntax for using the printf() function is:

bash
printf("format string", argument1, argument2, ...);

The "format string" is a string that contains text and special placeholders called format specifiers, which are used to insert the values of the arguments into the string.

Here are some examples of how to use the printf() function:

  • Print a string:
bash
printf("Hello, World!");
  • Print an integer:
perl
int age = 25; printf("I am %d years old.", age);
  • Print a floating-point number:
bash
float pi = 3.14159; printf("The value of pi is approximately %f.", pi);
  • Print multiple values:
perl
int x = 5, y = 10; printf("The value of x is %d and the value of y is %d.", x, y);

The printf() function also allows you to specify the width, precision, and alignment of the output using format specifiers. For example, you can control the number of decimal places printed for a floating-point number or specify the minimum width of a field.

It's worth noting that the printf() function returns the number of characters that were printed. This can be useful for checking if the function was successful or for counting the number of characters in the output.

No comments:

Post a Comment