The printf()
function stands for print format. It is an Input/Output function that is used to print values of variables in Scala. As the name suggests, it is used to print values in different formats. For instance, %f
stands for float, %d
stands for integer, %s
stands for string, and %c
stands for character format.
The syntax for using the printf
function is shown below:
printf("%format", variable)
%format
: this is the format representing the data type of the variable we want to print.
variable
: this is the variable whose value we want to print to the console.
The value returned is the value of the variable
.
Let's view a code example.
object Main extends App { // create mutable variables in Scala var age:Int = 50 var isMarried:Boolean = false var name:String = "John Doe" var gender:Char = 'M' var height:Float = 5.65F // print values of variables printf("age is = %d\n", age) printf("isMarried is = %b\n", isMarried) printf("name is = %s\n", name) printf("gender is = %c\n", gender) printf("height is = %f\n", height) }
RELATED TAGS
CONTRIBUTOR
View all Courses