Chapter 4 C Programming
Chapter 4 C Programming
OUTPUT
Enter An alphabet
a
A
FORMATTED INPUT
◼ Formatted input refers to an input data that has
been arranged in a particular format.
◼ The formatted data can be read with the help of
scanf(). The general form of scanf() is
▪ scanf(“control string”, &arg1, &arg2…. &argn);
– The control string specifies the field format in which the
data is to be entered and
– the arguments arg1,arg2...argn specifies the address of
locations where the data are stored. Arguments are
separated by commas.
– Control string contains field specification which direct the
interpretation of input data.
◼ Control string contains field specification
which direct the interpretation of input
data.
◼ It may include Field (or format)
specifications, consisting of conversion
character %, a data type character, and
an optional number, specifying the field
width.
Inputting integer numbers
◼ The field specification for reading an
integer number is
%wd
◼ Eg: scanf(“%2d %5d”,&num1, &num2);
◼ An input field may be skipped by
specifying * in the place of field width.
◼ For eg ,
– scanf(“%d %*d %d”,&a, &b);
Sample Program
◼ /*Reading integer numbers*/ OUTPUT :
main()
{ Enter three integer numbers
123
int a, ,b, c, x, y, z; 1 3 –3577
int p, q, r; Enter two 4-digit numbers
printf(“Enter three integer numbers \n”); 6789 4321
scanf(“%d %*d %d”,&a, &b, &c); 67 89
printf(“%d %d %d \n \n”,a, b, c); Enter two integer numbers
44 66
printf(“Enter two 4-digit numbers \n”); 4321 44
scanf(“%2d %4d ”,&x, &y); Enter a nine digit numbers
printf(“%d %d \n \n”,x, y); 123456789
printf(“Enter two integer numbers \n”); 66 1234 567
scanf(“%d %d”,&a, &x); Enter two three digit numbers
123 456
printf(“%d %d \n \n”,a, x); 89 123
printf(“Enter a nine digit numbers \n”);
scanf(“%3d %4d %3d”,&p, &q, &r);
printf(“%d %d %d \n \n”,p, q, r);
printf(“Enter two three digit numbers \n”);
scanf(“%d %d”,&x, &y);
printf(“%d %d \n \n”,x, y);
}
Inputting Real Numbers
main()
main()
{ char address[80]; { char address[80];
printf(“Enter the address\n”);
scanf(“%[a-z]”, address); printf(“Enter the address\n”);
printf(“%-80s\n\n”, address); scanf(“%[^\n]”, address);
} printf(“%-80s\n\n”, address);
Output: }
Enter address Output:
New delhi 111102 Enter address
New delhi New delhi 111102
New delhi 111102
Reading Mixed Data Types
◼ It is possible to use one scanf statement
to input a data line containing mixed
mode data. In such cases, it should be
ensured that the input data items match
the control specifications in order and
type.