d o g
First we need to set up a place for the user's string to be stored. The declaration
char str[100];sets aside 100 storage locations to store 100 character codes. It's a char array of size 100. In addition it sets up
str
as symbol for
the address of the first storage location.
We call str
a symbolic constant.
It is a constant not a variable, since the purpose of str
is to be the address of the beginning of the
array. If we could change str
as though it were a variable
it would no longer correspond to that beginning address.
Second: fgets. The function fgets
takes three arguments.
The first argument is an address of the start of a storage region.
The second argument is one more than the maximum number of
character codes to store.
fgets(str, 100, stdin);The third argument specifies the file to be used for input. The name
stdin
(declared in stdio.h
)
specifies input from keyboard buffer,
that is, stuff the user has typed.
If the user has already typed something, fgets
uses that,
otherwise fgets
stops and waits for the user
to type a bunch of keys and press the enter key.
If the number of characters is small enough,--
in this case less than 100,-- fgets
will store
all the codes including that of the
newline character. If not less than 100 it will store the first 99.
In any case at the very end fgets
stores a zero to
mark the end of the string.
#include<stdio.h> int main() { char str[100]; fgets(str, 100, stdin);We can print out the first two characters of the string this way:
#include<stdio.h> int main() { char str[100]; fgets(str, 100, stdin); int i=0; while (i<2) { printf("%c\n", str[i]); i += 1; }Here variable
i
is used as an index for str.
If i
has a zero in it, then str[i]
is the first char code in the array and if i
has a 1 in it,
then str[i]
is the second char code in the array.
The conversion %c
in the printf
converts the
code to the corresponding character. The newline sets up the next print to
be down one line on the left.
So this program works if the string has length 2.
But its length could be larger or smaller.
If the user's input is #include<stdio.h> int main() { char str[100]; fgets(str, 100, stdin); int i = 0; while (str[i] != 0) { printf("%c\n", str[i]); i += 1; } }The loop above repeats
while the code at index i is non-zero: print the char at index i and move to a newline (\n) and make index i one largerSo all of the user's string prints provided it has less than 100 characters.
Another version. This is based on the idea that C/C++ treats any non-zero value as true.
#include<stdio.h> int main() { char str[100]; fgets(str, 100, stdin); int i = 0; while (str[i]) { printf("%c\n", str[i]); i += 1; } }Finally we have a version using pointers. Pointer
pc
has
initial address str
. Thus *pc
is the first code
stored in the array. The assignment pc += 1
increases
pc
so it is now the address of the next item in the array.
Each code *pc
is printed as a character until 0 is reached.
#include<stdio.h> int main() { char str[100]; fgets(str, 100, stdin); char *pc = str; while (*pc) { printf("%c\n", *pc); pc += 1; } }next: Decisions