Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Setting accumulator with "do while" loop and inner switch statement |
| Date | 2015-07-13 11:54 -0400 |
| Organization | Self |
| Message-ID | <55A3DF37.7080403@verizon.net> (permalink) |
| References | <21fa90b1-084e-4fa9-85eb-33dbc9ec7a47@googlegroups.com> <55A2A459.7020808@verizon.net> <f2dc3e82-42f7-46b1-a75e-644e51b12f22@googlegroups.com> |
On 07/13/2015 09:35 AM, Alla _ wrote:
...
> Sorry, it is still not clear.
> for example: the case '+' is true, then the result is
> temp += number;
> break;
> the case is closed, and given 'e' hasn't been entered, the program
> gets back to the scanf statement, which is outside the switch statement.
>
> do you say that in this case the temp value that has been computed
> within case '+' is stored, and after the user enters next number and
> operator (next scanf execution) the following switch statement remembers
> the temp value from the previous switch round and works with it?
Correct.
> ... How does
> that happen technically, if the case is ended with 'break'?
Every object in a C program has a lifetime. During it's lifetime,
whenever the program reads the value stored in the object, the value
read will be the same as the last value stored in it (unless the
object's definition declares it to be 'volatile'). I can only make sense
of your question by assuming that you believe that the lifetime of
'temp' ends when the 'break;' statement is executed. That would only be
true if 'temp' were declared inside the switch block. You've defined
temp in the main() block. As a result, the lifetime of 'temp' ends only
when the main() block is exited. That won't happen until after the
'return 0;' statement at the end of main().
Here's a modified version of your program where your concerns would be
reasonable:
#include <stdio.h>
int main (void)
{
float number;
char operator;
printf ("Please, enter a number and an operator:\n");
do
{
scanf ("%f %c", &number, &operator);
switch (operator)
{
float temp;
case 's':
temp = number;
printf ("= %f\n", temp);
break;
case '+':
temp = temp + number;
printf ("= %.2f\n", temp);
break;
case '-':
temp = temp - number;
printf ("= %.2f\n", temp);
break;
case '*':
temp = temp * number;
printf ("= %.2f\n", temp);
break;
case '/':
if (number == 0)
printf ("Division by zero\n");
else
temp = temp / number;
printf ("= %.2f\n", temp);
break;
case 'e':
printf ("= %.2f\nThe end of calculations\n", temp);
break;
} // End of life time for 'temp'
}
while (operator != 'e');
return 0;
}
In this version of the program, the lifetime of 'temp' ends whenever a
'break;' is executed. Since 'temp' in uninitialized, that means that
whenever 'operator' is '+', '-', '*', '/', or 'e', the behavior of the
program is undefined.
Note: the behavior of your original program is also undefined, if the
first value of operator is one of those. You should change your code to
keep track of whether or not 'temp' has been set. If it hasn't been, any
operation that requires the previous value of 'temp' to be set should be
handled as an error.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Setting accumulator with "do while" loop and inner switch statement Alla _ <modelling.data@gmail.com> - 2015-07-12 09:46 -0700
Re: Setting accumulator with "do while" loop and inner switch statement James Kuyper <jameskuyper@verizon.net> - 2015-07-12 13:31 -0400
Re: Setting accumulator with "do while" loop and inner switch statement Alla _ <modelling.data@gmail.com> - 2015-07-13 06:35 -0700
Re: Setting accumulator with "do while" loop and inner switch statement James Kuyper <jameskuyper@verizon.net> - 2015-07-13 11:54 -0400
Re: Setting accumulator with "do while" loop and inner switch statement raltbos@xs4all.nl (Richard Bos) - 2015-07-13 21:50 +0000
Re: Setting accumulator with "do while" loop and inner switch statement Barry Schwarz <schwarzb@dqel.com> - 2015-07-12 11:35 -0700
Re: Setting accumulator with "do while" loop and inner switch statement Alla _ <modelling.data@gmail.com> - 2015-07-13 06:29 -0700
csiph-web