In the C Programming Language, the printf feature writes a formatted string to the stdout stream.
Syntax
The syntax for the printf function in the C Language is:
int printf(const char *format, ...);
Parameters or Arguments
format Describes the output as well as affords a placeholder to insert the formatted string. Here are a few examples: Format Explanation Example %d Display an integer 10 %f Displays a floating-point number in fixed decimal layout 10.500000 %.1f Displays a floating-point range with 1 digit after the decimal 10.5 %e Display a floating-point variety in exponential (scientific notation) 1.050000e+01 %g Display a floating-point variety in both constant decimal or exponential format relying on the dimension of the wide variety (will no longer display trailing zeros) 10.5
Returns
The printf characteristic returns the number of characters that used to be written. If an error occurs, it will return a bad value.
Required Header
In the C Language, the required header for the printf characteristic is:
#include <stdio.h>
Applies To
In the C Language, the printf feature can be used in the following versions:
ANSI/ISO 9899-1990
printf Example
Here are some examples that use the printf function:
printf("%s\n","TechOnTheNet.com");
Result: TechOnTheNet.com
printf("%s is over %d years old.\n","TechOnTheNet.com",10);
Result: TechOnTheNet.com is over 10 years old.
printf("%s is over %d years old and pages load in %f seconds.\n","TechOnTheNet.com",10,1.4);
Result: TechOnTheNet.com is over 10 years old and pages load in 1.400000 seconds.
printf("%s is over %d years old and pages load in %.1f seconds.\n","TechOnTheNet.com",10,1.4);
Result: TechOnTheNet.com is over 10 years old and pages load in 1.4 seconds.
Example – Program Code
Let’s look at an example to see how you would use the printf function in a C program:
/* Example using printf */
#include <stdio.h>
int main(int argc, const char * argv[])
{
/* Define variables */
int age = 10;
float load = 1.4;
/* Display the results using the appropriate format strings for each variable */
printf("TechOnTheNet.com is over %d years old and pages load in %.1f seconds.\n", age, load);
return 0;
}
This C software would print “TechOnTheNet.com is over 10 years historic and pages load in 1.4 seconds.”
Similar Functions
Other C features that are similar to the printf function:
fprintf feature sprintf characteristic vfprintf characteristic vprintf function vsprintf function
See Also
Other C features that are noteworthy when dealing with the printf function:
fscanf characteristic scanf function sscanf characteristic
Leave a Review