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

  • Thread starter ming2194
  • Start date
  • Tags
    Function
In summary: 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.
  • #1
ming2194
32
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
  • #2
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...
 
  • #3
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".
 
  • #4
I think you should ask your friend for further clarification of what he/she is asking about.
 
  • #5
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...
 
  • #6
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?
 
  • #7
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.
 
  • #8
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.
 
  • #9
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
 

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

1. What is a C function?

A C function is a block of code that performs a specific task and can be called or invoked from other parts of a C program. It is a fundamental building block of any C program and allows for code reusability and modularity.

2. How do you declare a C function?

To declare a C function, you need to specify its return type, name, and any parameters it takes. For example: int addNumbers(int num1, int num2); This declares a function named "addNumbers" that takes two integer parameters and returns an integer value.

3. What is the syntax for calling a C function?

To call a C function, you need to use its name followed by opening and closing parentheses. If the function has parameters, you need to pass them inside the parentheses. For example: int result = addNumbers(5, 6); This calls the "addNumbers" function with the values 5 and 6 as its parameters and stores the returned value in the "result" variable.

4. How do you define a C function?

To define a C function, you need to provide the code that will be executed when the function is called. This includes the function's name, return type, and parameters, followed by opening and closing curly braces. For example: int addNumbers(int num1, int num2) {
    int sum = num1 + num2;
    return sum;
}
This defines the "addNumbers" function that takes two integer parameters and returns their sum.

5. Can a C function return multiple values?

No, a C function can only return a single value. However, this value can be a complex data structure such as a struct or an array, which can contain multiple values. Alternatively, you can use pointers as parameters to a function and modify their values within the function to effectively return multiple values.

Similar threads

  • Programming and Computer Science
Replies
2
Views
959
Replies
4
Views
985
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
32
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Calculus
Replies
25
Views
1K
Replies
17
Views
2K
Back
Top