Well, this is easy if you are familiar with pointers.
Suppose that you have this code to get the input from stdin stream:
#include <stdio.h> int main(void){ char destination[20], *ptr, c; fgets(destination, 20, stdin); return 0; }
Next, you want to output each character line by line:
1 2 3 4 5 6 7 | ptr = destination; // Have the pointer point to the array destination while(*ptr != '\0' && *ptr != '\n'){ c = *ptr; printf("%c \n", c); ptr++; } |
The output will be each single character being printed out line by line, inclusive of white-space character.
Now, lets see what we have done over here. At line 3 of the second code snippet, the condition is set to run if *ptr != \0 and \n. Why? This is because a string terminated with a \0 character. As you get the input from stdin stream, fgets included the \n (newline character) when you press Enter key on the stdin stream before appending the \0.
For line 4, we set *ptr to c, a char variable. As ptr is pointing to the address of the string, we de-reference it to get the value. Since variable c is only able to hold a single character and ptr is pointing to a single character, the de-referenced of ptr will be a single character from the string.
Last but not least, we increment ptr to point to the next character of the string.
I hope this is a clear explanation for you. Do leave a comment if you have any problem

















