C-Input-Output-1
C-Input-Output-1
Computing Lab
https://www.isical.ac.in/~dfslab
flags
0 : right align, with zero padding on the left
- : left align
width: minimum field width
precision: usually, number of significant digits (for floating point
numbers) /
maximum number of characters to be printed (for string)
length modifier: l, ll, L, z
type: d, i, c, f, s section no. in manual
Character at a time:
Functions: getchar() OR fgetc(stdin) (equivalent)
Return value: reads the next character and returns it as an unsigned
char cast to an int, or EOF on end of file or error.
Typical usage: while (EOF != (c = fgetc(fp))) ...
Caution: Do not forget to declare c as int type.
If c is of type char:
reading from the terminal: will probably work without any problem
reading from a file: in some (rare) cases, problems could occur
Line at a time:
Function: fgets(s, n, stdin)
Return value
reads at most n-1 characters or one line (whichever is shorter), stores
input in character array s and terminates s using '\0'
if a newline is read, it is stored in s
returns s or NULL on end of file (i.e., there is nothing to be read) or error
Typical usage: while (NULL != fgets(s, n, stdin)) ...
Caution: Without exception, do not use gets()!
Format string:
1. Write a program that reads text typed at the terminal, and counts
the number of occurrences of vowels in the input text;
the number of words in the input text. Assume that any contiguous
sequence of letters and / or digits forms one word.