A question about expressions...

C

Chad

Given the following..

#include <stdio.h>

int main(void)
{
int i = 9;
printf("%3d%3d%3d\n", i, i+1, i+2);
printf("i is now: %d\n", i);

return 0;
}

I get the following....
9 10 11
i is now: 9

Why is the value of 'i' 9 and not 11? My first guess is that i, i + 1,
and i + 2 are expressions, and hence, the values get discarded after
they are used.

Chad
 
K

Keith Thompson

Chad said:
Given the following..

#include <stdio.h>

int main(void)
{
int i = 9;
printf("%3d%3d%3d\n", i, i+1, i+2);
printf("i is now: %d\n", i);

return 0;
}

I get the following....
9 10 11
i is now: 9

Why is the value of 'i' 9 and not 11?

Because you initialized it to 9 and never changed it.
My first guess is that i, i + 1,
and i + 2 are expressions, and hence, the values get discarded after
they are used.

Of course. Would you expect evaluating ``i + 2'' to change the
value of i?

Given:

int i, j;
i = 9;
j = i + 3;

I'd expect i to be 9 and j to be 12. What would you expect?

Why are "+" and "+=" distinct operators?
 
B

Ben Bacarisse

Chad said:
Given the following..

#include <stdio.h>

int main(void)
{
int i = 9;
printf("%3d%3d%3d\n", i, i+1, i+2);
printf("i is now: %d\n", i);

return 0;
}

I get the following....
9 10 11
i is now: 9

Why is the value of 'i' 9 and not 11? My first guess is that i, i + 1,
and i + 2 are expressions, and hence, the values get discarded after
they are used.

I think you have had a good reply already but I'll add that the fact
that i, i + 1 and so on are expressions is not really the issue. It is
because they are "pure expressions" without any side effects that
matters. i++ and i = 42 are also expressions and their values get
discarded after they are used, but they also have an effect that is
defined to persist.
 
C

Chad

Because you initialized it to 9 and never changed it.


Of course.  Would you expect evaluating ``i + 2'' to change the
value of i?

Given:

    int i, j;
    i = 9;
    j = i + 3;

I'd expect i to be 9 and j to be 12.  What would you expect?

I have no idea.
Why are "+" and "+=" distinct operators?

I can't speak for C, but for some other programming languages it would
make a difference. For example, if I had

char x = 'A';

'A' + 1 would produce a value of 66.

Whereas

x += 1 would produce a value of 'B'.
 
C

Chad

I think you have had a good reply already but I'll add that the fact
that i, i + 1 and so on are expressions is not really the issue.  It is
because they are "pure expressions" without any side effects that
matters.  i++ and i = 42 are also expressions and their values get
discarded after they are used, but they also have an effect that is
defined to persist.

I'm currently taking an Introduction to Computer Science class at the
local extension. Anyways, the professor gave our class a much more
difficult variation on this question on a recent exam. I'd like to
believe the teacher knows what he is talking about since he holds a
Ph.D. in Mathematics from Princeton University. After that, he taught
4 years at MIT and another 20 years at the University of Michigan Ann
Arbor before moving out to the San Francisco Bay Area.
 
B

BartC

Chad said:
On Mar 9, 8:50 am, Keith Thompson <[email protected]> wrote:
I can't speak for C, but for some other programming languages it would
make a difference. For example, if I had

char x = 'A';

'A' + 1 would produce a value of 66.

And so would x+1.

But what is the value of x after that?
Whereas

x += 1 would produce a value of 'B'.

And what is the value of x now?

Perhaps you need to mess around with programs such as this:

#include <stdio.h>

int main(void){
char x = 'A';

printf("Original value of x as int: %d; as char: %c\n",x,x);

printf("Value of x+1 as int: %d; as char: %c\n",x+1,x+1);
printf("Value of x after 'x+1' as int: %d; as char: %c\n",x,x);

x+=1;

printf("Value of x after 'x+=1' as int: %d; as char: %c\n",x,x);
}

BTW in C, values such as 'A' and 65 are equivalent (unless you're on some
weird computer that doesn't use ASCII); 'A' is another way of writing 65.
 
K

Keith Thompson

Chad said:
I'm currently taking an Introduction to Computer Science class at the
local extension. Anyways, the professor gave our class a much more
difficult variation on this question on a recent exam. I'd like to
believe the teacher knows what he is talking about since he holds a
Ph.D. in Mathematics from Princeton University. After that, he taught
4 years at MIT and another 20 years at the University of Michigan Ann
Arbor before moving out to the San Francisco Bay Area.

Without seeing that that "much more difficult variation" actually is,
we can't possibly guess how it should behave. I can only speculate
that the differences between the code you posted and your professor's
code are critical. (Actually I could make a fairly good guess
about what your professor's code might have looked like, but I'm not
going to.)

The program you posted behaves exactly as it should. Your professor's
credentials have nothing to do with it.

You should have a C textbook or reference of some sort. Look up the "+"
and "+=" operators.
 
K

Keith Thompson

Chad said:
I have no idea.

``i = 9;'' stores the value 9 in the variable ``i''.

The expression ``i + 3'' yields the value 12 (9 + 3). It has no
side effects. It reads the value of ``i''; it does not modify it.
The result of the expression, 12, is then stored in ``j''.
I can't speak for C, but for some other programming languages it would
make a difference. For example, if I had

char x = 'A';

'A' + 1 would produce a value of 66.

Whereas

x += 1 would produce a value of 'B'.

I'm afraid that completely misses the point. (In C, 66 and 'B' are
exactly the same value, assuming the implementation uses a character set
where 'B' has that value; most implementations do.)

``x + 1'' yields the value of x with 1 added to it; it doesn't change the
value of x.

``x += 1'' also yields the value of x with 1 added to it, but it *also*
has the side effect of storing that value in x.

This is *very* elementary stuff. I don't think you're yet at the level
where you can effectively learn much by asking individual questions; you
lack the experience to know what questions to ask. Hopefully the class
you're taking is a decent one.

What textbook(s) are you using?
 
C

Chad

``i = 9;'' stores the value 9 in the variable ``i''.

The expression ``i + 3'' yields the value 12 (9 + 3).  It has no
side effects.  It reads the value of ``i''; it does not modify it.
The result of the expression, 12, is then stored in ``j''.





I'm afraid that completely misses the point.  (In C, 66 and 'B' are
exactly the same value, assuming the implementation uses a character set
where 'B' has that value; most implementations do.)

``x + 1'' yields the value of x with 1 added to it; it doesn't change the
value of x.

``x += 1'' also yields the value of x with 1 added to it, but it *also*
has the side effect of storing that value in x.

This is *very* elementary stuff.  I don't think you're yet at the level
where you can effectively learn much by asking individual questions; you
lack the experience to know what questions to ask.  Hopefully the class
you're taking is a decent one.

What textbook(s) are you using?

The actual language we are using is Java.
 
B

Ben Bacarisse

Chad said:
I'm currently taking an Introduction to Computer Science class at the
local extension. Anyways, the professor gave our class a much more
difficult variation on this question on a recent exam. I'd like to
believe the teacher knows what he is talking about since he holds a
Ph.D. in Mathematics from Princeton University. After that, he taught
4 years at MIT and another 20 years at the University of Michigan Ann
Arbor before moving out to the San Francisco Bay Area.

I don't see the connection -- I posted some clarification and tell about
your professor. Is there some conflict between what you have been told
and what I wrote?
 
J

James Kuyper

The actual language we are using is Java.

And that made it obvious to you that the appropriate place to ask your
question was a newsgroup devoted to C.
OK.
 
J

J. J. Farrell

Chad said:
I'm currently taking an Introduction to Computer Science class at the
local extension. Anyways, the professor gave our class a much more
difficult variation on this question on a recent exam. I'd like to
believe the teacher knows what he is talking about since he holds a
Ph.D. in Mathematics from Princeton University. After that, he taught
4 years at MIT and another 20 years at the University of Michigan Ann
Arbor before moving out to the San Francisco Bay Area.

I had a pizza for tea.
 
C

Chad

If you cant see you're being trolled then there is no hope for you.

I don't try to troll just because I don't to alienate the
knowledgeable people in this group.
 
G

Geoff

Given the following..

#include <stdio.h>

int main(void)
{
int i = 9;
printf("%3d%3d%3d\n", i, i+1, i+2);
printf("i is now: %d\n", i);

return 0;
}

I get the following....
9 10 11
i is now: 9

Why is the value of 'i' 9 and not 11? My first guess is that i, i + 1,
and i + 2 are expressions, and hence, the values get discarded after
they are used.

Chad

You are confusing the expression i + 1 with i += 1 or i = i + 1.
 
H

Hans Vlems

Given the following..

#include <stdio.h>

int main(void)
{
  int i = 9;
  printf("%3d%3d%3d\n", i, i+1, i+2);
  printf("i is now: %d\n", i);

  return 0;

}

I get the following....
  9 10 11
i is now: 9

Why is the value of 'i' 9 and not 11? My first guess is that i, i + 1,
and i + 2 are expressions, and hence, the values get discarded after
they are used.

Chad

You did answer your own question.
The concept that an expression yields a value is obviously clear to
you.
Then you write "... the values get discarded after they are used"
Discarded means "thrown away", right, not to be used for something
else.
Hence, if the value of an expression is not assigned then it gets
discarded.
There is no assignment operator alongside the expressions so the
results are
not stored.
Hans
 
S

Stefan Ram

Hans Vlems said:
The concept that an expression yields a value is obviously clear to
you.

Sometimes, one also uses the verb »has« instead of »yields«,
for example:

6.5.2.2 Function calls (...) [#5] (...) the function call
expression has the value determined as specified in 6.8.6.4.«

I like »to have« the most for the relationship between an
expression and its value, while I do not like to use
»return« in this case. »Yields« still is acceptably, too.

Expressions, usually, are evaluated at run-time. But there
is at least one case where ISO/IEC 9899:1999 (E) say that
an expression is »executed«!
Then you write "... the values get discarded after they are used"
Discarded means "thrown away", right, not to be used for something
else.
Hence, if the value of an expression is not assigned then it gets
discarded.

Well, in »2 + x« the value of »x« is not assigned, yet it
also is not discarded. To discard a value of an expression,
we have the famous discardure statement:

<expression> ;

For example, »5;« will quickly and effectively discard the
value of »5«. This usually takes less than a nanosecond.
 
M

Mark Bluemel

I'm currently taking an Introduction to Computer Science class at the
local extension.

I'm currently taking large quantities of drugs in my loft conversion.
Anyways, the professor gave our class a much more
difficult variation on this question on a recent exam. I'd like to
believe the teacher knows what he is talking about since he holds a
Ph.D. in Mathematics from Princeton University. After that, he taught
4 years at MIT and another 20 years at the University of Michigan Ann
Arbor before moving out to the San Francisco Bay Area.

What is his hat-size? I need to ascertain his cranial capacity before I
recognise his authority.

[Aside: I think Richard has it right - is your surname Cunningham by any
chance?]
 
S

Stefan Ram

I like »to have« the most for the relationship between an
expression and its value, while I do not like to use
»return« in this case. »Yields« still is acceptably, too.

(Just a note to myself:)

Actually, values are properties of evaluations and not
of expressions. Surely,

5+1

has a value, but

time( 0 )

does not have a value - each evaluation might yield another
value! So the value belongs to the evaluation not to the
expression. In comparison: mathematical terms have values.

Well, the expression »time( 0 )« and »5 + 1« both have one
thing, and this is meaning: While »5 + 1« means the sum of 5
and 1, »time( 0 )« means the operation »time( 0 )«. This
meaning does not depend on the evaluation. But in a context
like, for example, during the evaluation of a
superexpression, an expression represents the value of its
evaluation (during the evaluation of its superexpression),
not its meaning. For example, when »1+time(0)« is evaluated,
the sum is taken of 1 and the value of an evaluation of
»time(0)«.

When »f(x)« is an expression and »x« is a subexpression, I
think it is save to say that for each evaluation of »f(x)«,
the subexpression »x« is evaluated once, and this
subexpression evaluation does not start before the whole
expression evaluation starts and does not end after the
whole expression evaluation ends. The value of this
evaluation of the subexpression then is used as the
value of this subexpression for this evaluation of the
whole expression.

(For simplicity, in the previous paragraph, I ignored
conditional expressions like »0&&x«, which do not evaluate
»x«.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top