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

bookPrintf

The printf is a function in C used to display output on the screen. It stands for print formatted and allows you to show text, numbers, or variable values in a specific format.

Main.c

Main.c

copy
12345678910
#include <stdio.h> int main() { // Declaring and initialization `int` type variable int iVariable = 832; printf("iVariable = %d \n", iVariable); return 0; }

The printf function is responsible for displaying formatted output on the screen. In this example, it prints both text and the value of the variable iVariable.

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 is a format specifier that tells printf to display an integer. Specifiers act as placeholders for variable values, such as %f for floats, %c for characters, and %s for strings.

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

Main.c

copy
123456789101112
#include <stdio.h> int main() { int iVariable = 832; // Variable of `int` type float fVariable = 54.984; // Variable of `float` type char cVariable = '#'; // Variable of `char` type printf("iVariable = %d \n", iVariable); // Using `%d` for integer printf("fvariable = %f \n", fVariable); // Using `%f` for float printf("cVariable = %c \n", cVariable); // Using `%c` for single character }
Note
Note

The \n is an escape sequence that tells the program to move to a new line when printing text.

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

main.c

main.c

copy
123456789101112
#include <stdio.h> int main() { int iVariable = 832; float fVariable = 54.984; char cVariable = '#'; printf("iVariable = %d \n fvariable = %f \n cVariable = %c \n", iVariable, fVariable, cVariable); return 0; }

The format specifiers and the variables are paired in sequence, which allows printf to insert each variable's value into its corresponding placeholder in the correct order.

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

Main.c

Main.c

copy
12345678910
# include <stdio.h> int main() { int iVariable = 1234; printf("%f", iVariable); // `%f` - a floating point number for floats return 0; }
question mark

Which format specifier should you use to correctly print the value of an integer variable?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain what happens if I use the wrong format specifier in printf?

What are some common mistakes when using printf in C?

Can you show examples of correct and incorrect usage of format specifiers?

Awesome!

Completion rate improved to 2.63

bookPrintf

Veeg om het menu te tonen

The printf is a function in C used to display output on the screen. It stands for print formatted and allows you to show text, numbers, or variable values in a specific format.

Main.c

Main.c

copy
12345678910
#include <stdio.h> int main() { // Declaring and initialization `int` type variable int iVariable = 832; printf("iVariable = %d \n", iVariable); return 0; }

The printf function is responsible for displaying formatted output on the screen. In this example, it prints both text and the value of the variable iVariable.

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 is a format specifier that tells printf to display an integer. Specifiers act as placeholders for variable values, such as %f for floats, %c for characters, and %s for strings.

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

Main.c

copy
123456789101112
#include <stdio.h> int main() { int iVariable = 832; // Variable of `int` type float fVariable = 54.984; // Variable of `float` type char cVariable = '#'; // Variable of `char` type printf("iVariable = %d \n", iVariable); // Using `%d` for integer printf("fvariable = %f \n", fVariable); // Using `%f` for float printf("cVariable = %c \n", cVariable); // Using `%c` for single character }
Note
Note

The \n is an escape sequence that tells the program to move to a new line when printing text.

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

main.c

main.c

copy
123456789101112
#include <stdio.h> int main() { int iVariable = 832; float fVariable = 54.984; char cVariable = '#'; printf("iVariable = %d \n fvariable = %f \n cVariable = %c \n", iVariable, fVariable, cVariable); return 0; }

The format specifiers and the variables are paired in sequence, which allows printf to insert each variable's value into its corresponding placeholder in the correct order.

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

Main.c

Main.c

copy
12345678910
# include <stdio.h> int main() { int iVariable = 1234; printf("%f", iVariable); // `%f` - a floating point number for floats return 0; }
question mark

Which format specifier should you use to correctly print the value of an integer variable?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
some-alt