The printf()
method is used to print provided values to the console or command-line interface in C and C++ programming languages. The printf() method prints provided values in a formatted manner. The printf() method can be used to print, character, string, integer, floating-point, etc. values and variables to the console.
printf() Method Syntax
The printf() method syntax is like below.
printf(FORMAT,VALUE_VARIABLE)
- FORMAT is the formatting part which is printed to the console. It may contain string and specifiers to print VALUE and VARIBLE.
- VALUE_VARIABLE is a value or variable we want to print its value in the FORMAT part.
Format Specifier | Type |
---|---|
%d or %i | Integer |
%u | Floating-point or double |
%o | Unsigned Octal |
%x | unsigned Hexadecimal |
%X | Unsigned hexadecimal |
%f | Decimal floating point |
%s | String of characters |
%c | Character |
print String Value and Variable
String variables and values can be printed by using the printf() method. The %s
is used as string format specifier.
/* printf string example */
#include <stdio.h>
int main()
{
char name[]="ismail";
printf ("My name is %s", "Ahmet");
printf ("My name is %s", name);
return 0;
}
Print Integer Value and Variable
The integer value or integer variable can be printed by using the printf() method. The %d
format specifier is used to expressing integer values and variables.
/* printf integer example */
#include <stdio.h>
int main()
{
int age = 27;
printf ("I am %d years old.", age);
printf ("I am %d years old.",37);
return 0;
}
Print Floating-Point Value and Variable
Floating-point values can be printed with the printf() method. The %f
format specifier is used to expressing floating-point values or variables.
/* printf floating-point example */
#include <stdio.h>
int main()
{
int a= 2.7;
printf ("I am %f years old.", age);
printf ("I am %df years old.",2.7);
return 0;
}
Print Multiple Values and Variables
The printf() method can print multiple values in a single statement, the variables or values are added as parameters to the printf() method like below.
/* printf example */
#include <stdio.h>
int main()
{
char name[]="İsmail";
int age = 37;
printf ("%s is %d years old", name,age);
printf ("%s %s is %d years old",name," Baydan",2.7);
return 0;
}