Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `&` used for both binary AND and address-of in C?

Tags:

c

syntax

This question is in response to What is &&& operation in C which got me thinking about & being used both as binary AND and address-of.

Why is the same symbol used for both these very dissimilar tasks? When thinking about it, @ would seem to be a better symbol for address-of.

I don't expect it to cause much problem in practice since the compiler will probably catch most faulty use, but it would probably be possible to create code involving macros that looks like it is doing a binary AND while it is actually doing address-of.

like image 732
Leo Avatar asked Dec 20 '12 09:12

Leo


2 Answers

The reason @ was not used for "address of" (as you suggested would be a good choice) is that that character was not part of the ISO 646 character set, which was actually used back in the early days of C.

While it is true that the designers of C could have used @ and designed a trigraph sequence for it, I suspect they felt it was not really worth the hassle. The fact that &&& can appear in people's code was probably not considered reason enough to choose a different operator symbol.

like image 193
Ray Toal Avatar answered Sep 23 '22 09:09

Ray Toal


I don't think there is any big reason why it's used for 2 different things.

Luckily, there is no ambiguity in the language because one use is a unary operator and the other one is a binary operator (i.e. has two operands). Ditto for * (multiplication or dereference).

like image 38
tom Avatar answered Sep 21 '22 09:09

tom