Thursday, 14 February 2013

Formatted Input


C provides scanf() function for entering input data
Syntax
scanf(“control string”, address1, address2….);
Control string specifies the format in which data has to be entered
address1, address2 specifies the address of locations where data is to be stored

Examples Integer Numbers

Format: %wd
w is the field width
Ex 1
int marks;
scanf(“%d”,&marks);
Ex 2
char str[30];
scanf(“%s”,str);    Value will not be stored in str
Ex 3
int basic,da;
scanf(“%d%d”,&basic,&da);




Ex 4

int hra,da;

scanf(“%d:%d”,&hra,&da);     1500:200
Ex 5
int num1,num2;
scanf(“%2d %5d”,&num1,num2);  21345 50
21 will be assigned to num1 and 345 will be assigned to num2 and 50 that is unread will be assigned to next scanf call
Ex 6

int a,b;
scanf(“%d %*d %d”, &a,&b);123 456 789
123 to a
456 skipped (because of *)
789 to b
Examples Real Numbers
Ex 1
float x;
scanf(“%f”,&x);
Assigns: 4.321 to x
Ex 2
double y;
scanf(“%lf”,&y);

No comments:

Post a Comment

String functions

Read This: strncpy() strncat() strstr () strlen() — Predefined function defined in string.h . — — Returns the len...