C Program Learning Part-4
MANAGING INPUT AND OUTPUT OPERATORS
READING A CHARACTER
Reading a single character can be done by using the function getchar. The getchar takes the following form: variable_name = getchar();
char name;
name = getchar();
main()
{
char answer;
printf(“Would you want
to know my name.\n”);
printf(“Enter Y for YES
and N for NO”);
answer = getchar();
if(answer == ‘Y’ ||
answer == ‘y’)
printf(“My name is
Billah.”);
else
printf(“You are good for
nothing.”);
}
WRITING A CHARACTER
putchar is used for writing character one at a time as shown below putchar (variable_name);
main()
{
char alphabet;
printf(“Enter an alphabet.\n”);
putchar(‘\n’);
alphabet = getchar();
if(islower(alphabet))
putchar(toupper(alphabet));
else
putchar(tolower(alphabet));
}
Formatted Input:
scanf(“control string”, arg1, arg2,……….., argN);
Inputting Character Strings:
%wc or %wc
int no;
char name1[15], name2[15], name3[15];
printf(“Enter serial and name one. \n”);
scanf(“%d %15c”, &no, name1);
printf(“%d %15s”, &no, name1);
printf(“Enter serial and name two. \n”);
scanf(“%d %s”, &no, name2);
printf(“%d %15s”, &no, name2);
printf(“Enter serial and name three. \n”);
scanf(“%d %15s”, &no, name3);
printf(“%d %15s”, &no, name3);
Formatted Output:
For Printing an integer number is : %w d
Format |
Output |
||||||||||
printf(“%d”,9876); |
9 |
8 |
7 |
6 |
|||||||
printf(“%6d”,9876); |
|
|
9 |
8 |
7 |
6 |
|||||
printf(“%2d”,9876); |
9 |
8 |
7 |
6 |
|||||||
printf(“%-6d”,9876); |
9 |
8 |
7 |
6 |
|||||||
printf(“%06d”,9876); |
0 |
0 |
9 |
8 |
7 |
6 |
|||||
For Printing an integer number is : %w p f
For printing the output of the number y=98.7654
Format |
Output |
|||||||||||||||||
printf(“%7.4f”,y); |
9 |
8 |
. |
7 |
6 |
5 |
4 |
|||||||||||
printf(“%7.2f”,y); |
|
|
9 |
8 |
. |
7 |
7 |
|||||||||||
printf(“%-7.2f”,y); |
9 |
8 |
. |
7 |
7 |
|
|
|||||||||||
printf(“%f”,y); |
9 |
8 |
. |
7 |
6 |
5 |
4 |
|||||||||||
printf(“%10.2e”,y); |
|
|
9 |
. |
8 |
8 |
e |
+ |
0 |
1 |
||||||||
printf(“%11.4e”,-y); |
- |
9 |
. |
8 |
7 |
6 |
5 |
e |
+ |
0 |
1 |
|||||||
printf(“%-10.2e”,y); |
9 |
. |
8 |
8 |
e |
+ |
0 |
1 |
|
|
||||||||
printf(“%e”,y); |
9 |
. |
8 |
7 |
6 |
5 |
4 |
0 |
e |
+ |
0 |
1 |
||||||
Printing of a Single Character:
A single character can be displayed using the format: %wc
Printing of Strings:
The format is : %w.ps
For Printing a string “NEW DELHI 110001”
Format |
Output |
|||||||||||||||||||
%s |
N |
E |
W
|
|
D
|
E |
L |
H |
I |
|
1 |
1 |
0 |
0 |
0 |
1 |
|
|
|
|
%20s |
|
|
|
|
N |
E |
W |
|
D |
E |
L |
H |
I |
|
1 |
1 |
0 |
0 |
0 |
1 |
%20.10s |
|
|
|
|
|
|
|
|
|
|
N |
E |
W |
|
D |
E |
L |
H |
I |
|
%.5s |
N |
E |
W
|
|
D |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%-20.10s |
N |
E |
W |
|
D |
E |
L |
H |
I |
|
|
|
|
|
|
|
|
|
|
|
%5s |
N |
E |
W |
|
D |
E |
L |
H |
I
|
|
1 |
1 |
0 |
0 |
0 |
1 |
|
|
|
|