Scanf in C

About Scanf

Scanf is a kind of function which stands for Scan Formatted String. This will help to understand the data or read the data from standard input and write that result in the argument with function after taking input from standard input.

Scanf in C

Working of Scanf :

The main functionality of scanf function is to read the inputs from some standard inputs  .

Important Note :

  • Scanf reads the data from standard input (stdin).
  • Scanf accepts numeric, strings or character from standard input.

Declaration of Scanf:

int scanf(const char * format......)

Example 1

Write a program to implement the scanf function.

Code

Run
#include<stdio.h>
int main ()
{
  int a, b;
  printf ("Number 1 = ");
  scanf ("%d", &a);
  printf ("Number 2 = ");
  scanf ("%d", &b);
  printf ("A = %d\n", a);
  printf ("B = %d\n", b);
  return 0;
}

Output

Number 1 = 6
Number 2 = 8
A = 6
B = 8

Example 2

Write a program by using scanf function.

Code

Run
#include<stdio.h>
int main()
{
    float number1;
    double number2;
    printf("Enter first number = ");
    scanf("%f", &number1);
    printf("Enter second number = ");
    scanf("%lf", &number2);
    printf("number 1 = %f\n", number1);
    printf("number 2 = %lf", number2);
    return 0;
}

Output

Enter first number = 4
Enter second number = 5
number 1 = 4.000000
number 2 = 5.000000

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription