Input in C

Receiving input by scanf( )

In the previous program, we assume the value of the variable. But with the more flexible program, the user wants to input the value. scanf() function is used for this purpose.

Syntax of scanf()

scanf("format specification",&list of variable);

Note: & is called "address of" operator.

Example of scanf()

scanf("%d",&a); //This function reads an integer from user and store it into variable a.
scanf("%d%d",&a,&b); //This function reads two integer values, first is stored in a and second is stored in b.
scanf("%d%f%c",&a,&b,&c); //This function reads 3 different types of values, first is stored in a, second is stored in b, third is stored in c. In this situation a should be an int variable, b should be float and c should be declared as a character.

Program to find the Sum of two numbers.

/*program to add two numbers*/
#include "stdio.h"
int main() { 
      int x, y, sum;
      printf("Enter any two numbers: ")
      sum = x + y;

Program to calculate the simple interest.

/*program to find out the simple interst*/
int main() {
      float p,r,si;
      int t;
      printf("Enter the principle,rate and time: ");
      printf("Simple interst=%.2f",si);
In the above program two things should be noted:
  1. First is the expression si=p*t*r/100. In this expression, the variable p and r are of float type so the result will be in the float.
  2. Expression evaluation rules are...
  3. The int-int calculation produces an integer result
  4. float-float calculation produce float result
  5. In int-float or float-int calculation, the First integer value is changed into float and then arithmetic performed, and produce the float result.
  6. The summary is that in the arithmetic the result will belong to the larger type.
  7. The second thing that should be noted is that printf("Simple interst=%.2f", si);. Here %.2f means the only 2 digits will be printed after the decimal places. By default, 6 digits are printed.

Input in C with sum of two numbers | Video Tutorial in Hindi