scanf() function

 scanf() function

The scanf() function is used for input. It reads the input data from the console.

The syntax of scanf() function:

scanf("Control string",arguments); 

Control string – it is format specifier (%d, %f, %c) in double quotation mark (i.e. “ ”).

The format string can be

            %d for integer

            %f for float

            %c for character

%s for string

(Note - % is symbol which to  indicting format specifier)  

Arguments-  it is list of variables with & (ampersand) symbol. & write before variable. The variables separated by comma ( , )

                        Here we are sending addresses of variable (address are obtain by using ‘&’ the ‘address of’ operator) to scanf() function.

Example –

Int a ;

printf(“enter the value of V is”);

scanf(“%d”, &v);

printf(“\n enter the value of V is %d”,v);

 

output – value of V is 10

 

Simple Programs:

 

1)     Write a program to print string i.e. Vivekanand College Computer Department.

#include <stdio.h>   

main()

{

    clrscr();

// Displays the string inside quotations

    printf("Vivekanand College Computer Department");

    getch();

}

 

Output

Vivekanand College Computer Department

----------------------------------------------

2)     Write a program to print Characters.

#include <stdio.h>

main()

{

    char chr = 'a';

    clrscr();  

    printf("character = %c", chr); 

    getch();

}

 

Output

character = a

------------------------------------------------

3)     Write a program to print float number

#include <stdio.h>

main()

{

    float num = 27.5;

    clrscr();

    printf("num  = %f", num);

    getch();

}

 

Output

number1 = 27.500000

--------------------------------------------

4)     Write a program for Sum of two integer.

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,sum;

clrscr();

printf("enter two numbers = ",a,b);

scanf("%d%d",&a,&b);

sum=a+b;

printf("Sum =%d",sum);

getch();

}

 

Output (if we enter a = 5 and b= 6)

Sum = 11

Post a Comment

0 Comments