In this article, we will discuss

  • How to read input from the console and store it in a variable?
  • How to perform addition, subtraction & multiplication on those variables?
  • How to print the results back to the display?

scanf:

To read values from the console, we use the scanf() function. Scanf() is a pre-defined/in-built function that resides in the standard C library. Below is the prototype of the scanf() function

Syntax: int scanf(const char *format, …);

printf:

To write values to the console, we use the printf() function. printf() is a pre-defined/in-built function that resides in standard C library. Below is the prototype of the printf() function.

Format specifier string:

In the first parameter of printf and scanf, we pass Format string or Format specifier string, we specify, what type of the value that the user is going to enter.

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.

Note: The major difference between printf and scanf is, In printf() we pass variable values whereas in scanf() we pass the variable address.

Example program:

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

Explanation:

In the above program, we are using the scanf() to read the input from the console and storing them in variables a & b. After that, we are performing the arithmetic operations like addition, subtraction, and multiplication and later storing the results in the 3 new variables (Sum, Sub, Mul) and finally, by using the printf() function we are printing the results.

Example snippets on printf & scanf

#Example 1

#include <stdio.h>

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

In the above program, scanf() reads two inputs from the program even though we are passing one variable because we specified two format specifiers. Always remember, In scanf()/printf() all the format specifiers that we pass should be the same, else we may get some unexpected runtime errors.

#Example 2

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

In the above program,  scanf() reads only one input from the user because we specified only one format specifier. So from this example, we conclude that no of the inputs will be taken depends on the number of format specifiers.

#Example 3:

#include <stdio.h>

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

In the above program, we are passing the value of a variable to scanf() instead of the variable address. This will lead to a run-time error. In Linux, we will get a segmentation fault error.

#Example 4:

#include <stdio.h>
 
int main()
{
	int a = 10; int b = 20;
	printf("the value of a = %d, the value of b = %d", a, b);
}

In the above program, when printf scans the format string, it will replace the format specifiers (%d) with the actual values that we pass to printf(). i.e. the first format specifier will get replaced by the first value that we pass in printf() and so on.

Categorized in:

Tagged in: