What Is the Purpose of the C Function in This Image?

  • Thread starter Thread starter ming2194
  • Start date Start date
  • Tags Tags
    Function
AI Thread Summary
The discussion revolves around understanding the purpose and functionality of a program depicted in a shared image, particularly focusing on keywords like "char" and "while." Participants clarify that "char" is a data type and "while" is a control structure, not functions. The primary function of the while loop in the program is to repeatedly ask the user if they want to run the program again, continuing as long as the user inputs 'y' or 'Y'. However, issues arise due to the way input is handled with scanf, particularly with the newline character and how it affects the input buffer. This can lead to unexpected behavior, such as the loop continuing to run without allowing new input. Suggestions include testing the program to observe its behavior and modifying the scanf call to improve input handling, such as adding a space before "%c" to mitigate issues with leftover characters in the input buffer.
ming2194
Messages
31
Reaction score
0
http://u1.imgupload.co.uk/1258243200/e9fd_image.png

today my friend gave me this picture and ask me what is the purpose ,in terms of function, of

the program shown in the figure. It's really inspired me a lot. The reason is although we always

use some functions, did we really know what is the purpose of that function?

so if you were me, how would you answer my friend?
 
Technology news on Phys.org
I even tried to read your post backward, but I still don't understand what you are asking about :rolleyes:

ming2194 said:
what is the purpose ,in terms of function, of the program shown in the figure.

:eek:

did we really know what is the purpose of that function?

Which one? printf? scanf? Technically main() is a function as well...
 
Borek said:
I even tried to read your post backward, but I still don't understand what you are asking about :rolleyes:
:eek:
Which one? printf? scanf? Technically main() is a function as well...

i also feels confused too.

actually my friend's question is " what is the purpose (in terms of function) of

the program shown in the figure ( picture)."

i tried to guess what he is asking and my guess is, he is asking about the the purpose of function "char" and "while".
 
I think you should ask your friend for further clarification of what he/she is asking about.
 
ming2194 said:
i also feels confused too.

actually my friend's question is " what is the purpose (in terms of function) of

the program shown in the figure ( picture)."

i tried to guess what he is asking and my guess is, he is asking about the the purpose of function "char" and "while".

char and while aren't functions. char and while are keywords, with char being a data type, and while being used to control a loop of a certain kind.

The apparent purpose of the while loop in the code you showed is to ask whether the user would like to run the program again. Presumably the loop is supposed to stop when the user enters a character other than 'y' or 'Y'. However, because of the control string that is used in the call to scanf, the loop probably won't work as expected.

You could write a short program and test this out for yourself...
 
Mark44 said:
char and while aren't functions. char and while are keywords, with char being a data type, and while being used to control a loop of a certain kind.

The apparent purpose of the while loop in the code you showed is to ask whether the user would like to run the program again. Presumably the loop is supposed to stop when the user enters a character other than 'y' or 'Y'. However, because of the control string that is used in the call to scanf, the loop probably won't work as expected.

You could write a short program and test this out for yourself...

just as you said, i wrote the program above and try to test it.

PHP:
#include<stdio.h>
main()
{
	char again;
	again='y';
	while (again=='y'||again=='Y')
	{printf("would you like to ?");
	scanf("\n%c", &again);
}
}

but when i enters a character other than 'y' or 'Y', it really stop.
why?
 
The key is that "while (again== 'y' || again== 'Y')"
The || is, of course, "or". While "again" is either of those, the program runs the lines inside the "while" block.

The program checks to see whether the character "again" is either y or Y. If it is, it runs the lines inside the "while", which involves getting another character for "again", then rechecks. If not, if "again" is any character other than y or Y, it exits the block, ending the program.
 
I think this is more subtle than HallsOfIvy's explanation. The key really is in the call to scanf(), with its control string of "\n%c". There are some real problems when you try to input a single character using scanf, namely that you actually enter two characters: the character in question and ENTER. Since you're storing the result in a memory location that can hold only one character, the input buffer still holds the second character you typed, so in loops like the one you have, the next iteration of the loop uses a character that's still left over in the input buffer, which can produce results you might not have expected.
 
There are also problems if you enter yyyyyyy<ENTER>. The while loop will continue looping as long as there are characters in the input buffer, without letting you enter a new choice.
 
  • #10
I don't think this loop really repeats unless you flush the buffer.
 
  • #11
is the /n really necessary in the scanf? if you are having problems with the iterations after the first time through the loop i'd just try adding a space in the scanf before the %c
 
Back
Top