Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The scanf() function

The scanf() is a standard input library (in built) function used for entering input data (through keyboard).
This function can accept values of different types like numbers, characters & string.

Syntax

scanf(“format string”,variable_address_list);

Example No. 1

#include <stdio.h>
int main()
{
    int i;
    printf("Enter any integer value = ");
    scanf("%d",&i);
    printf("Entered value = %d",i);
    return 0;
}

Output

 Enter any integer value = 10
 Entered value = 10

Example No. 2

#include <stdio.h>
int main()
{
    float x;
    printf("Enter any float value = ");
    scanf("%f",&x);
    printf("Entered value = %f",x);
    return 0;
}

Output

 Enter any float value = 7.5
 Entered value = 7.500000

Example No. 3

#include <stdio.h>
int main()
{
    int i;
    float x;
    char ch;
    printf("Enter integer, float & character values = ");
    scanf("%d%f%s",&i,&x,&ch);
    printf("\n Integer value = %d",i);
    printf("\n Float value = %f",x);
    printf("\n Character value = %c",ch);
    return 0;
}

Output

 Enter integer, float & character values = 5
 7.5
 A

 Integer value = 5
 Float value = 7.500000
 Character value = A

 

Leave a reply