Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The & operator in C serves two distinct syntactic and semantic roles depending on its arity: as a unary operator, it functions as the address-of operator, yielding the memory address of its operand; as a binary operator, it functions as the bitwise AND operator, performing a bit-level logical AND operation between two integer operands.

Unary & (Address-of Operator)

When used with a single operand, & evaluates to a pointer to that operand. Operand Constraints: The operand must be an lvalue (an expression referring to an object that occupies an identifiable location in memory), a function designator, or an array name. The unary & operator cannot be applied to:
  • Variables explicitly declared with the register storage class.
  • Bit-fields within a struct or union.
  • Constants or literal values (excluding compound literals).
  • Expressions that do not yield an lvalue (e.g., the result of a + b).
Type Evaluation: If the operand is of type T, the expression &operand yields a result of type T* (pointer to T). Syntax:
&operand
Example:
int x = 10;
int *ptr = &x; /* & evaluates to the memory address of the lvalue 'x' */

Binary & (Bitwise AND Operator)

When used with two operands, & performs a bitwise AND operation. It compares each bit of the first operand’s binary representation to the corresponding bit of the second operand. The resulting bit is set to 1 if and only if both corresponding bits are 1; otherwise, it is set to 0. Operand Constraints: Both operands must be of an integral type (e.g., char, short, int, long, or their unsigned variants). It cannot be applied to floating-point types or pointers. Type Evaluation: Before the operation is performed, the usual arithmetic conversions are applied to both operands to bring them to a common type. The result of the expression is of this common promoted integral type. Syntax:
operand1 & operand2
Example:
unsigned char a = 0x3C;  /* Binary: 0011 1100 */
unsigned char b = 0x19;  /* Binary: 0001 1001 */
unsigned char c = a & b; /* Binary: 0001 1000 (Evaluates to 0x18) */

Precedence and Associativity

The compiler distinguishes between the two forms based on lexical context, and they occupy different levels in the C operator precedence table:
  • Unary &: Has very high precedence (Level 2, alongside other unary operators like *, ++, sizeof). It evaluates with right-to-left associativity.
  • Binary &: Has lower precedence (Level 8, which is below equality operators like == and != but above logical operators like &&). It evaluates with left-to-right associativity.
/* Precedence visualization */
int val = 5;
int mask = 1;
int *ptr = &val;

/* Binary & evaluates after == due to precedence rules */
/* (ptr == &val) is evaluated first, then the binary & is applied */
int result = mask & ptr == &val; 
Master C with Deep Grasping Methodology!Learn More