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

Course Content

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

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

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

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

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

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

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; }

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

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

What will the program output?

Select the correct answer

Everything was clear?

Section 2. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt