C Expressions

S

sarathy

Hi,
I have doubt in C expressions. The ++, -- ( both post and
pre increment and decrement operators) have similar functionality. The
thing is if the ++ comes before an operand, it is a prefix expression.
If it comes after an operand, it is a postfix expression.

But i see in the precedence table, that

a++ and a-- comes in the category postfix operators (and have high
precedence than ++a,--a), where as
++a and --a comes in the category unary operators( with low precedence)

Why is that distinction ? Both belong to the same category,
right ?

Also what does it mean to say that the result of a++ or ++a
is not an lvalue.

Regards,
Sarathy
 
M

Mark McIntyre

Hi,
But i see in the precedence table, that

a++ and a-- comes in the category postfix operators (and have high
precedence than ++a,--a), where as
++a and --a comes in the category unary operators( with low precedence)

Why is that distinction ?

They have different semantics. postfix ++ doesn't change the value of
what it increments till AFTER it has been read whereas prefix ++ reads
it before the increment.:
Both belong to the same category, right ?

No, since they have different effects.
Also what does it mean to say that the result of a++ or ++a
is not an lvalue.

More or less, it means its not an object you can assign to. If you
think about it
x++ = 34;
can't make any sense.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

Simon Biber

sarathy said:
Hi,
I have doubt in C expressions.

That's interesting. I have great confidence in C expressions. They tend
to do what I ask them to do. They are very reliable.

(Hint: learn what the word "doubt" means. It does not mean the same as
"question". Your sentence would be better expressed as "I have a
question about C expressions.")
The ++, -- ( both post and
pre increment and decrement operators) have similar functionality. The
thing is if the ++ comes before an operand, it is a prefix expression.
If it comes after an operand, it is a postfix expression.

But i see in the precedence table, that

a++ and a-- comes in the category postfix operators (and have high
precedence than ++a,--a), where as
++a and --a comes in the category unary operators( with low precedence)

Why is that distinction ? Both belong to the same category,
right ?

No. As you found, they don't belong to the same category.
Also what does it mean to say that the result of a++ or ++a
is not an lvalue.

lvalues are expressions that refer to an object in memory. They are
called "left values" because they may appear on the left side of an
assignment expression.

The result of a++ or ++a is just a value. It's an ephemeral thing - it
can be used in computation, but it will not be stored anywhere. It does
not refer to an object in memory.
 
C

Chris Dollin

Simon said:
That's interesting. I have great confidence in C expressions. They tend
to do what I ask them to do. They are very reliable.

(Hint: learn what the word "doubt" means. It does not mean the same as
"question". Your sentence would be better expressed as "I have a
question about C expressions.")

I am led to understand that `doubt` is idiomatic Indian English for
`question`.

I trust you will not castigate me for my use of the term `pavement`.

(Yes, it -- the former -- grates on me too.)
 
K

Kenneth Brody

Mark McIntyre wrote:
[... pre- versus post- increment/decrement ...]
They have different semantics. postfix ++ doesn't change the value of
what it increments till AFTER it has been read whereas prefix ++ reads
it before the increment.:

<mode type="nit pick">

Not exactly.

The value of a post-increment is the value of the expression
prior to the increment. Nothing requires that the increment
take place after reading the value. Ditto for the pre-increment.
Nothing requires that the value not be read until after the
increment.

A perfectly valid interpretation of:

a = b++;

could be the equivalent of:

b = b+1;
a = b-1;

Why would one do that? Who knows? Maybe the machine code that
gets generated is more efficient on a particular platform. The
point is, it's legal.

In fact, AFAIK, if "b" is not "volatile", and is never used after
the increment, the compiler is free to not even do the increment
in the first place.

</mode>

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

John Bode

sarathy said:
Hi,
I have doubt in C expressions. The ++, -- ( both post and
pre increment and decrement operators) have similar functionality. The
thing is if the ++ comes before an operand, it is a prefix expression.
If it comes after an operand, it is a postfix expression.

But i see in the precedence table, that

a++ and a-- comes in the category postfix operators (and have high
precedence than ++a,--a), where as
++a and --a comes in the category unary operators( with low precedence)

Why is that distinction ? Both belong to the same category,
right ?

No, because the prefix and postfix forms are evaluated differently.
Also what does it mean to say that the result of a++ or ++a
is not an lvalue.

Basically, it means that it cannot be the target of an assignment, so
expressions like

x++ = 1

are illegal.
 
M

Mark McIntyre

I am led to understand that `doubt` is idiomatic Indian English for
`question`.

It is, but its probably still worth pointing out the idiomatic
International English word too. My experience with my colleagues in
our india dev team is that they welcome anything which helps them
communicate more fluently.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

sarathy

They have different semantics. postfix ++ doesn't change the value of
what it increments till AFTER it has been read whereas prefix ++ reads
it before the increment.:

prefix reads before the incremented ? I think it is the other way
around !!!
 
R

Richard Bos

Chris Dollin said:
I am led to understand that `doubt` is idiomatic Indian English for
`question`.

Just like "handy" is idiomatic German English for a mobile phone, yes;
but that is no reason to use it as if it were English.
I trust you will not castigate me for my use of the term `pavement`.

Not if you use it correctly to mean the paved part of the road, which
people walk on, as opposed to the macadamed or cobbled part of the road,
which vehicled travel on.

Richard
 

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

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top