Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 2.63

bookPrintf

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3
some-alt