Can anyone help me to figure out how these functions work.
There are two pieces of code - with and without a while loop.
#include <stdio.h>
int main(void)
{
char z;
z = getchar();
putchar (z);
}
The second one is
#include <stdio.h>
int main(void)
{
char z;
while (z != '.')
{
z = getchar();
putchar(z);
}
}
The problem is that the first one is working properly, while the second one returns all the characters it gets (e.g. if the input was 2222, the function returns 2222). Why didn't it return 2?
.period in your claimed input that terminated the loop? Please be precise. Why was the first example "proper" when you entered2222? - Weather Vanezvariable to'.'. It also has UB when the input does not contain the period character. It is also unclear why you expect it to print (not "return", return is a term with its own separate meaning) just one2character. - n. 1.8e9-where's-my-share m.