473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

associativity of operators

What does "associativity of operators" mean ?

I am unable to undersatand this from K & R 2nd edition. Kindly explain
with an example.

Thanks

Mar 16 '07 #1
8 2873
su**************@yahoo.com, India said:
What does "associativity of operators" mean ?

I am unable to undersatand this from K & R 2nd edition. Kindly explain
with an example.

Consider the following code fragment:

int x = 4;
int y = 5;
int z = 6;
int r = x - y - z;

printf("r is %d\n", r);

What would you expect to be printed?

Is it:

(a) -7
(b) 5
(c) something else

If you answered (a), try to work out why (b) is also plausible.
If you answered (b), try to work out why (a) is also plausible.
If you answered (c), come back when you're feeling better. :-)

Which answer you get depends on the order in which the subtractions are
done. 4 - (5 - 6) is different to (4 - 5) - 6. Clearly, precedence
cannot answer this question, since subtraction obviously has the same
precedence as subtraction!

So we need another tool for discriminating between operators at the same
precedence level. That tool is 'associativity'. Left-to-right
associativity means "do the stuff on the left first, and use the
calculated result as input for the stuff on the right", and
right-to-left associativity means, of course, the opposite.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 16 '07 #2
On Fri, 16 Mar 2007 07:29:43 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>su**************@yahoo.com, India said:
>What does "associativity of operators" mean ?

I am unable to undersatand this from K & R 2nd edition. Kindly explain
with an example.


Consider the following code fragment:

int x = 4;
int y = 5;
int z = 6;
int r = x - y - z;

printf("r is %d\n", r);

What would you expect to be printed?

Is it:

(a) -7
(b) 5
(c) something else

If you answered (a), try to work out why (b) is also plausible.
If you answered (b), try to work out why (a) is also plausible.
If you answered (c), come back when you're feeling better. :-)

Which answer you get depends on the order in which the subtractions are
done. 4 - (5 - 6) is different to (4 - 5) - 6. Clearly, precedence
cannot answer this question, since subtraction obviously has the same
precedence as subtraction!

So we need another tool for discriminating between operators at the same
precedence level. That tool is 'associativity'. Left-to-right
associativity means "do the stuff on the left first, and use the
calculated result as input for the stuff on the right", and
right-to-left associativity means, of course, the opposite.
Nicely said. However it should be pointed out that in Mathematics
"associative" ordinarily means that it doesn't matter, i.e, if o is an
operator and X, Y, Z are elements, then o is associative if

(X o Y) o Z = X o (Y o Z)

* and + are associative; - and / are not. Left-to-right associativity
and right-to-left associativity are conventions for resolving
unparenthesized expressions.

Mar 16 '07 #3

"Richard Harter" <cr*@tiac.netwrote in message
>
Nicely said. However it should be pointed out that in Mathematics
"associative" ordinarily means that it doesn't matter, i.e, if o is an
operator and X, Y, Z are elements, then o is associative if

(X o Y) o Z = X o (Y o Z)

* and + are associative; - and / are not. Left-to-right associativity
and right-to-left associativity are conventions for resolving
unparenthesized expressions.
And associativity isn't just about notational conventions. It is actually
interesting to mathematicians.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 16 '07 #4
On Fri, 16 Mar 2007 21:50:06 -0000, "Malcolm McLean"
<re*******@btinternet.comwrote:
>
"Richard Harter" <cr*@tiac.netwrote in message
>>
Nicely said. However it should be pointed out that in Mathematics
"associative" ordinarily means that it doesn't matter, i.e, if o is an
operator and X, Y, Z are elements, then o is associative if

(X o Y) o Z = X o (Y o Z)

* and + are associative; - and / are not. Left-to-right associativity
and right-to-left associativity are conventions for resolving
unparenthesized expressions.
And associativity isn't just about notational conventions. It is actually
interesting to mathematicians.
Indeed.

Mar 16 '07 #5
On Mar 16, 3:30 pm, c...@tiac.net (Richard Harter) wrote:
Nicely said. However it should be pointed out that in Mathematics
"associative" ordinarily means that it doesn't matter, i.e, if o is an
operator and X, Y, Z are elements, then o is associative if

(X o Y) o Z = X o (Y o Z)

* and + are associative; - and / are not. Left-to-right associativity
and right-to-left associativity are conventions for resolving
unparenthesized expressions.
In mathematics, + and * are associative. Especially in floating-point
arithmetic, they are not (take x = 1e100, y = -1e100, and z = 1, then
(x + y) + z is not the same as x + (y + z). In C, + and * are left
associative, so x + y + z is (x + y) + z.

Mar 17 '07 #6
"christian.bau" wrote:
>
.... snip ...
>
In mathematics, + and * are associative. Especially in floating-
point arithmetic, they are not (take x = 1e100, y = -1e100, and
z = 1, then (x + y) + z is not the same as x + (y + z). In C, +
and * are left associative, so x + y + z is (x + y) + z.
Not "in mathematics". For a single example, consider vector
multiplication. You need to specify the fields involved, rings,
etc.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 17 '07 #7
In article <45***************@yahoo.comcb********@maineline.net writes:
"christian.bau" wrote:
... snip ...

In mathematics, + and * are associative. Especially in floating-
point arithmetic, they are not (take x = 1e100, y = -1e100, and
z = 1, then (x + y) + z is not the same as x + (y + z). In C, +
and * are left associative, so x + y + z is (x + y) + z.

Not "in mathematics". For a single example, consider vector
multiplication. You need to specify the fields involved, rings,
etc.
In mathematics those are generally not considered standard multiplications.
(Inner product and outer product in 3D, and in higher dimension vectors you
get into tensors when doing outer products.) But '*' is also not associative
in the sedenions. '*' *is* associative in a ring.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 18 '07 #8
On Sun, 18 Mar 2007 00:22:40 GMT, "Dik T. Winter" <Di********@cwi.nl>
wrote:
>In article <45***************@yahoo.comcb********@maineline.net writes:
"christian.bau" wrote:
>
... snip ...
>
In mathematics, + and * are associative. Especially in floating-
point arithmetic, they are not (take x = 1e100, y = -1e100, and
z = 1, then (x + y) + z is not the same as x + (y + z). In C, +
and * are left associative, so x + y + z is (x + y) + z.
Not "in mathematics". For a single example, consider vector
multiplication. You need to specify the fields involved, rings,
etc.

In mathematics those are generally not considered standard multiplications.
(Inner product and outer product in 3D, and in higher dimension vectors you
get into tensors when doing outer products.) But '*' is also not associative
in the sedenions. '*' *is* associative in a ring.
Likewise the octonians.
Mar 18 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chad | last post by:
The following question stems from p.132 in the book "The C Programming Language", second edition by K & R. The have struct { int len; char *str; } *p;
5
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
2
by: bochengnever | last post by:
( ) and -are left to right in the same order . eg: struct foo { int a ; void * p; } main() { struct foo* A= malloc(sizeof(struct foo));
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
5
by: fdmfdmfdm | last post by:
Associativity in C takes two forms: left to right and right to left. I think the K&R book lacks something... For example, *p++, since the associativity is from right to left, do this expression...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.