Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Printf | Data
C Basics

Printf

Indeed, printf() is not a standalone function — it's a part of a broader family of functions. This family includes sprintf(), vprintf(), vsprintf(), and vfprintf(). However, for the purposes of our introductory course, we'll focus solely on printf().

Take a look at a typical usage of the printf() function:

c

Main.c

The output can be broken down into two main parts:

  • The format string;
  • The data to be displayed.

The data to be displayed is straightforward — it's just the variable we're working with. But let's delve deeper into the format string.

Format String

C language doesn't inherently possess Input/Output (I/O) capabilities. The role of the printf() function is to take your variable's value, convert its content into characters, and then replace the "%d" with them.

Format Specifiers

The %d in our format string is what's called a format specifier.

Format specifiers indicate the type of data that should be displayed within the format string, serving as a heads-up of sorts. In our scenario, the specifier alerts the function to expect integer data. Throughout this course, we'll touch on a few of these specifiers, such as:

  • %d – for integers;
  • %f – for floating-point numbers;
  • %c - for single characters.

Note

The application of various format specifiers will hinge on your experience with C programming.

This concept aligns with the example we reviewed in the previous lesson:

c

Main.c

Note

Remember, "\n" is a control character that moves subsequent content to a new line.

One of the great things about the printf() function is its ability to print multiple variables simultaneously!

c

main.c

Here, the format specifiers and the variables are paired in sequence:

However, if you mismatch the specifier, your program will not display the data correctly:

c

Main.c

What will the program output?

Select the correct answer

Everything was clear?

Section 2. Chapter 3
course content

Course Content

C Basics

Printf

Indeed, printf() is not a standalone function — it's a part of a broader family of functions. This family includes sprintf(), vprintf(), vsprintf(), and vfprintf(). However, for the purposes of our introductory course, we'll focus solely on printf().

Take a look at a typical usage of the printf() function:

c

Main.c

The output can be broken down into two main parts:

  • The format string;
  • The data to be displayed.

The data to be displayed is straightforward — it's just the variable we're working with. But let's delve deeper into the format string.

Format String

C language doesn't inherently possess Input/Output (I/O) capabilities. The role of the printf() function is to take your variable's value, convert its content into characters, and then replace the "%d" with them.

Format Specifiers

The %d in our format string is what's called a format specifier.

Format specifiers indicate the type of data that should be displayed within the format string, serving as a heads-up of sorts. In our scenario, the specifier alerts the function to expect integer data. Throughout this course, we'll touch on a few of these specifiers, such as:

  • %d – for integers;
  • %f – for floating-point numbers;
  • %c - for single characters.

Note

The application of various format specifiers will hinge on your experience with C programming.

This concept aligns with the example we reviewed in the previous lesson:

c

Main.c

Note

Remember, "\n" is a control character that moves subsequent content to a new line.

One of the great things about the printf() function is its ability to print multiple variables simultaneously!

c

main.c

Here, the format specifiers and the variables are paired in sequence:

However, if you mismatch the specifier, your program will not display the data correctly:

c

Main.c

What will the program output?

Select the correct answer

Everything was clear?

Section 2. Chapter 3
some-alt