Why is printf a macro rather than a function in Julia?

Overview

We can use Julia, a programming language, for computational mathematics and related functionalities in data science and statistics.

Like other programming languages, Julia comprises libraries, classes, and methods that help scientists and programmers accomplish different tasks.

Difference between a function and a macro

A function is a set of predefined instructions that enable a scientist or programmer to accomplish a specific task. Functions help store instructions as one block of code used throughout the program. Once a function is defined, it can be used repetitively throughout our program/task.

A macro is a set of instructions from keystrokes or mouse ticks that replace repetitive tasks. With a macro, these tasks can be accomplished at once without having to repeat the process. A macro records repetitive tasks by following mouse ticks and keystrokes and then generates a new code every time it’s run.

Suppose we’re tasked with analyzing sales records in our company, and our analysis involves deleting any blank spaces and formatting the records. These records may have uniform fonts and sum up all the weekly sales. We can automate this process using a macro. This way, we won’t need to repeat these tasks every time; instead, we’ll only run a macro on the data.

Every time a macro is run, it generates a custom code for that task.

We often use macros in excel.

Why printf is a macro instead of a function?

In Julia, printf is a macro and not a function.

Since a macro generates a custom code every time it’s used, printf works the same way when running through a format instruction.

At runtime, printf generates a customized code for each specific format specifier and then outputs the results as needed.

Format specifiers in Julia

  • %c: This is a single character (letter, number, special symbol).
  • %s: This is a string (character combinations)
  • %d: This is an integer (a whole number).
  • %f: This is a float (number with floating decimal points).
  • %.nf: We can restrict the float to n decimal places.
  • %e: This is a scientific representation of a float number.

Examples

julia> using Printf
julia> @printf "The number is %e" 1.234
julia> using Printf
julia> @printf "Her name is %s" "Jane"

Practice Terminal

Note: Use the terminal below to execute the commands. First, type julia to run the program and copy-paste the above commands.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved