Difference Between Printf And Scanf In C

Example

#include <stdio.h>

In this guide, we will explore the variance between printf and scanf. Prior to delving into the disparities, it's essential to grasp the concepts of printf and scanf alongside their syntax and illustrations.

Printf:

It displays content enclosed within double quotation marks using format specifiers such as %c, %d, %f, etc., to represent the value stored in a variable or constant. Additionally, it supports escape sequences like \n for a new line and \t for a horizontal tab.

Syntax:

Example

printf("(string) %format_specifier (string)",variable);

Scanf:

It enables the retrieval of single or multiple user-input values from the console using the keyboard. The number of format specifiers can match the desired input count, with or without a specified format.

Syntax:

Example

scanf(%format_specifier, &pointer_to_variable);

Format Specifier string:

In the initial argument of printf and scanf, the format string or format specifier string is provided to indicate the data type of the input value expected from the user.

Data Type Conversion format string
Integer short integer %d or % i
short unsigned % u
long signed % ld
long unsigned % lu
unsigned hexadecimal % x
unsigned octal %O
Real float % f or % g
double %lf
signed character %c
unsigned char %c
string %s

For example, if a user enters 20 as input, as 20 is a decimal integer value , we specify all the decimal integer values in C by using the %d format specifier . Similarly, float values are specified using %f .

Basic Code for Printf and Scanf:

Example 1:

Example

#include <stdio.h>

int main()
{
    int a, b;
    int sum = 0, sub = 0, mul = 0;

    printf("Please enter the two values:");

   /* Read input */
   scanf("%d%d", &a, &b);
    sum = a+b;
    sub = a-b;
    mul = a*b;

    /* Print output */
    printf("The sum = %d \nsub = %d \nmul = %d", sum, sub, mul);

    return 0;
}

Output:

Output

Please enter the two values:8
6
The sum = 14 
sub = 2 
mul = 48

Explanation:

In the aforementioned code, we utilize the scanf function to capture user input from the console and assign them to variables a and b. Subsequently, arithmetic calculations such as addition, subtraction, and multiplication are executed, with the outcomes stored in new variables (Sum, Sub, Mul). Ultimately, the printf function is employed to display the calculated results.

Example 2:-

Example

#include <stdio.h>

int main()
{
	int a;
	scanf("%d %d", &a);
	printf("Execution finished...");
       return 0;
}

In the program mentioned above, the scanf function is responsible for accepting two inputs from the program despite only one variable being provided. This occurs because two format specifiers were explicitly specified. It is important to note that when using scanf or printf, all format specifiers should match to avoid potential unexpected errors during runtime.

Example 3:

Example

int main()
{
    int a, b;
    scanf("%d", &a, &b);
    printf("Execution finished...");
    return 0;
}

Output:

Output

5
Execution finished...

Explanation:

In the preceding code snippet, the scanf function is responsible for receiving a single input from the user as we have defined only one format specifier. Therefore, based on this illustration, it can be inferred that the quantity of format specifiers does not impact the number of inputs accepted.

Example 4:

Example

#include <stdio.h>

int main()
{
	int a;
	scanf("%d", a);
	printf("Execution finished");
}

Output:

Output

6
Segmentation fault

Explanation:

In the preceding code snippet, we are providing the variable's value to scanf rather than its memory address. This mistake will result in a runtime issue. On a Linux system, this error will trigger a segmentation fault.

Example 5:

Example

#include <stdio.h>

int main()
{
	int a = 10; int b = 20;
	printf("the value of a = %d, the value of b = %d", a, b);
}

Output:

Output

the value of a = 10, the value of b = 20

Explanation:

When the program above is executed, as printf scans through the format string, it will substitute the format specifiers (%d) with the corresponding values provided within printf. Each format specifier will be substituted with the respective value passed in the printf function, maintaining the order in which they appear.

Basic Differences between Printf and Scanf

Few Differences between printf and scanf are:-

  • printf function outputs data to the standard output, i.e., to the console. In contrast, the scanf function reads data from the standard input, i.e., input devices
  • printf rarely uses pointer in a few cases but scanf always uses a pointer to assign value to the given variable.
  • printf and scanf both have the same return type of integer . The printf returns the number of characters it has successfully printed on the console, whereas scanf returns 0,1 or EOF based on the format specifier provided.

Input Required

This code uses input(). Please provide values below: