scanf() Function
scanf() function reads data from a standard input device into arguments specified by the format string.scanf(format string, arguments)
Eg:-
/* Input through the keyboard */
#include <stdio.h>
#include <conio.h>
int main()
{
int x,y,sum;
printf("Enter an Integer:");
scanf("%d",&x);
printf("Enter another:");
scanf("%d",&y);
sum = x + y ;
printf("sum =%d \n",sum);
}
/* Floating point Numbers */
#include <stdio.h>
#include <conio.h>
int main()
{
float x,y,sum;
printf("Enter a number:");
scanf("%f",&x);
printf("Enter another:");
scanf("%f",&y);
sum = x + y ;
printf("sum =%.2f \n",sum);
}
/* Employee Data */
#include <stdio.h>
#include <conio.h>
int main()
{
int empnum;
char name[10];
int dd, mm , yyyy;
float salary;
printf("Enter employee number:");
scanf("%d",&empnum);
printf("Enter name :");
scanf("%10s", &name);
printf("Enter date dd/mm/yyyy :");
scanf("%2d %2d %4d", &dd, &mm, &yyyy);
printf("Enter salary :");
scanf("%f",&salary);
printf("\n");`
printf("Emp Num :%d \n", empnum);
printf("Name :%s\n", name);
printf("Date of employment :%d%c%d%c%d\n", dd,'/',mm,'/', yyyy);
printf("Salary :%7.2f\n",salary);
}
Reading a character
getc()
getc() is a macro that reads one character from a stream.getchar()
getchar() is a macro that gets a character from stdin. It returns the next character on the input stream stdin.#include <stdio.h>
int main()
{
char ch;
printf("Enter a character:");
ch = getc(stdin);
printf("You have entered the character %c\n",ch);
}
Reading a String
gets() Function - Reads a string from a stdin./* Reading a string from the keyboard */
#include <stdio.h>
int main()
{
char string[80];
printf("Enter a word:");
gets(string);
printf("You have entered %s\n",string);
}
No comments:
Post a Comment