Wednesday 1 October 2014

Reading Data in C

Input Data in C

Input data can be entered into the memory from a standard from a standard input device(keyboard). C provide scanf() library function for entering input data. this scanf() can take all type of values ( numeric, character, string, float etc) as input.

Syntax of sacnf():  

scanf("control string/conversion specification",address1 , address2,........n);

This function should have at least two parameters, First parameter is a control string, which contains conversion specification characters. It should be within double quotes. The conversion specification character may be one or more. number of conversion specification depends on number of variable/address in the scanf ().if we use 2 address then use 2 conversion specification, three address, three conversion specification. it depends on the number of variable we want to input. the other parameter are addresses of variables. in the scanf() function at least one address should be present.The address of variable is found by preceding the variable name by an ampersand(&) sign. This sign is called address operator and it gives the starting address of the variable name in memory. A string variable is not preceded by &sign to get address. discus in String chapter.
 let's write a input code......

#include<stdio.h>
main()
{
int marks;
..............
scanf("%d",&marks);
..............
}
in above example the control string contains only one conversion specification character %d, which implies that one integer value should be entered as input. This entered value will be stored in the variable marks.

**************************************************************

#include<stdio.h>
main()
{
char ch;
scanf("%c",&ch);
..............
}

here the control string contains conversion specification character %c, which means that a single character should be entered as input. This entered value will be stored in the variable ch.

**************************************************************

#include<stdio.h>
main()
{
float fee;
scanf("%f",&fee);
}

here the control string contains conversion specification character %f, which means that a floating point number should be entered as input. This entered value will be stored in the variable fee.

**************************************************************

#include<stdio.h>
main()
{
char str[30];
scanf("%s",str);
}

In this, String has conversion specification character %s implying that a string should be taken as input. Note that the variable str is preceded by & sign. The entered string will be stored in the variable str.

**************************************************************


More than one value can also entered by scanf() function-

#inclue<stdio.h>
main()
{
int basic, da;
...........
scanf("%d%d",&basic,&da);
..........
}
in this example the control string has two conversion specification character imply that two value should be entered. These values are stored in the variable basic and da. the data can be entered with space as the delimiter.Number of   conversion specification is responsible for number of variable.

**************************************************************


How to input different type of value 


#inclue<stdio.h>
main()
{
int basic;
float hra;
char grade;
................
scanf("%d %f %c",&basic,&hra,&grade);
...............
}

Here the control string has three conversion specification characters %d %f and %c, means that one integer value, one floating point value and one single character can be entered as input. These values are stored in the variables basic, hra and grade. The input data can be entered as-
1500 , 200.50, A.
When more than one value are input by scanf(),These values can be separated by whitespace character like space. tab or newline. A specific character can be also be placed between two conversion specification character.

#include<stdio.h>
main()
{
int basic ;
float hra;
scanf("%d : %f",&basic, &hra);
}
here the delimiter is colon(:). the input data can be entered as 
1500:200.5

the value 1500 is stored in variable basic and 200.50 is stored in hra.

#inclue<stdio.h>
main()
{
int basic;
float hra;
scanf("%d,%f",&basic,&hra);
}

Here the delimiter is comma (,). The input data can be enterd as-
1500, 200.40

#include<stdio.h>
main()
{
int day,month,year;
int basic;
.............
scanf("%d-%d-%d",&day,&month,&year);
scanf("$%d",&basic);
............
}
here if the data is entered as-
24-5-1992
$2000

Then 24 is stored in variable day, 5 is variable month and 1992 is stored in the variable years and 2000 stored in the variable basic.

#inclue<stdio.h>
main()
{
int x, y, z;
scanf("%d %d %d",&x ,&y,&z);
}
  if the data is entered as
12  34  56
12 is stored in x, 34 is stored in y and 56 is stored in z;

No comments:

Post a Comment