Pages

Sunday, 23 October 2016

Scanf : C Language Function

Scanf C Language Function

Scanf

A function is use to take input from keyboard and transfer to a variable.

Syntax

Scanf(“format string”,&variable_list);

Format string : %d,%c,%f,%s has been use as format string .

Variable_list : Name of variable followed by address operator where input though keyboard been transferred.

Example

Scanf(%d”,&a);  Integer input
Scanf(“%c”,&a); character input

Scanf(“%f”,&a); float input
Scanf(“%s”,&a); string input
Space will not be accept through keyboard
Scanf(‘%d%d%s”,&a,&b,&c);

Taking character input with more than one data type using single scanf function

void main()
{
    char ch;
    int n;    
    printf("Enter integer and a character>> ");
    scanf("%d\n%c",&n,&ch);
    printf("\nInteger input %d\nCharacter input %c",n,ch);
}

Complete Example

/* #########################################
    Girfa : student Help
        scanf Function
    for more program visit :http://girfahelp.blogspot.in/p/c-language.html
    #########################################
*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int a,d;
    float b;
    char c;
    /* Two Input through single scanf */
    printf("Enter two number>> ");
    scanf("%d%d",&a,&d);
    printf("Enter a float number>> ");
    scanf("%f",&b);
    printf("Enter a character>> ");
    fflush(stdin);  /* Clears the keyboard buffer  */
    scanf("%c",&c);
    printf("\nInteger input is %d and %d",a,d);
    printf("\nFloat input is %f",b);
    printf("\nCharacter input is %c",c);
    getch();
}

Character Array Input with space

char name[20];
clrscr();
printf("Enter name>> ");
scanf("%[^\n]",name);
puts(name);

Download PDF

No comments:

Post a Comment