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

  • Thread starter Thread starter ming2194
  • Start date Start date
  • Tags Tags
    Function
Click For Summary

Discussion Overview

The discussion revolves around understanding the purpose of specific functions and keywords in a C program, particularly focusing on the use of the "while" loop and the "char" data type. Participants explore the implications of the program's structure and behavior, as well as the nuances of input handling in C.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants express confusion about the original question regarding the purpose of the program, with different interpretations of what "function" refers to.
  • One participant clarifies that "char" and "while" are not functions but keywords, with "char" being a data type and "while" controlling loop execution.
  • Another participant suggests that the purpose of the "while" loop is to prompt the user to run the program again, indicating it should stop when a character other than 'y' or 'Y' is entered.
  • Concerns are raised about the control string used in the "scanf" function, which may lead to unexpected behavior in the loop.
  • A participant shares a code snippet and reports that the loop behaves as expected when a character other than 'y' or 'Y' is entered, prompting further inquiry into why this occurs.
  • Discussions highlight issues with input handling in "scanf", particularly regarding the need to manage the input buffer and the effects of pressing ENTER after inputting a character.
  • Some participants suggest modifications to the "scanf" control string to address the input issues, such as adding a space before "%c".

Areas of Agreement / Disagreement

Participants do not reach a consensus on the original question's clarity or the best approach to handle input in the program. Multiple competing views on the functionality and behavior of the code remain present.

Contextual Notes

There are unresolved issues regarding the handling of input in C, particularly with the "scanf" function and its interaction with the input buffer, which may affect the loop's behavior.

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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K