K&R C operators and expressions
last modified December 5, 2026 K&R C 1978
Most C operators have not changed since 1978: the arithmetic, comparison, logical, bitwise, increment, and conditional operators are covered in the C operators tutorial. This article covers what is particular to K&R C: integers as truth values, the rules for division and for mixing types, the old assignment operators, and the order of evaluation.
Truth values are integers
K&R C has no boolean type. A relational or logical operator yields the
int 1 when the relation is true and 0 when it is false. In a
condition, any nonzero value counts as true and 0 as false.
#include <stdio.h>
int main()
{
int a, b;
a = 7;
b = 0;
printf("a > b: %d\n", a > b);
printf("a == b: %d\n", a == b);
printf("!a: %d\n", !a);
printf("!b: %d\n", !b);
printf("!!a: %d\n", !!a);
printf("a && b: %d\n", a && b);
printf("a || b: %d\n", a || b);
printf("(a > b) + (a > 1) + (a > 2): %d\n", (a > b) + (a > 1) + (a > 2));
return 0;
}
!a is 0 because a is nonzero, and
!!a turns any nonzero value into exactly 1. &&
and || also yield 0 or 1, not the value of an operand. Since the
results are ordinary integers, they can be added: the last line counts how
many of three conditions hold.
$ gcc -std=c89 -pedantic -Wall -Wextra -o truth truth.c $ ./truth a > b: 1 a == b: 0 !a: 0 !b: 1 !!a: 1 a && b: 0 a || b: 1 (a > b) + (a > 1) + (a > 2): 3
Integer and floating division
When both operands of / are integers, the division is an
integer division and the fraction is dropped. When either operand is a
floating-point number, the other one is converted and the result keeps the
fraction. The % operator gives the remainder of an integer
division; it is not defined for floating-point operands.
#include <stdio.h>
int main()
{
int minutes;
minutes = 135;
printf("7 / 2 = %d\n", 7 / 2);
printf("7 %% 2 = %d\n", 7 % 2);
printf("7.0 / 2 = %f\n", 7.0 / 2);
printf("7 / 2.0 = %f\n", 7 / 2.0);
printf("1 / 3 * 3 = %d\n", 1 / 3 * 3);
printf("%d minutes = %d h %d min\n", minutes, minutes / 60, minutes % 60);
printf("-7 / 2 = %d\n", -7 / 2);
printf("-7 %% 2 = %d\n", -7 % 2);
printf("7 / -2 = %d\n", 7 / -2);
printf("7 %% -2 = %d\n", 7 % -2);
return 0;
}
In a printf format, %% prints a percent sign.
1 / 3 * 3 is 0, because 1 / 3 is already 0. Integer
division and remainder split a number of minutes into hours and minutes.
The last four lines divide negative numbers. The book says that the direction
of truncation for / and the sign of the result of
% are machine-dependent for negative operands. GCC and the
PDP-11 both truncate toward zero, so -7 / 2 is -3 and
-7 % 2 is -1, but another machine may give -4 and 1. Portable
K&R code avoids dividing negative numbers.
$ gcc -std=c89 -pedantic -Wall -Wextra -o division division.c $ ./division 7 / 2 = 3 7 % 2 = 1 7.0 / 2 = 3.500000 7 / 2.0 = 3.500000 1 / 3 * 3 = 0 135 minutes = 2 h 15 min -7 / 2 = -3 -7 % 2 = -1 7 / -2 = -3 7 % -2 = 1
Character arithmetic
A char is a small integer. In an expression, every
char and short is converted to int
before the operation, so character arithmetic is integer arithmetic. The book
uses this for case conversion and for tests on characters.
#include <stdio.h>
int main()
{
char c, upper;
int is_lower, is_digit;
c = 'k';
is_lower = c >= 'a' && c <= 'z';
is_digit = c >= '0' && c <= '9';
upper = c - 'a' + 'A';
printf("%c lower: %d, digit: %d\n", c, is_lower, is_digit);
printf("upper case: %c\n", upper);
printf("position in the alphabet: %d\n", c - 'a' + 1);
printf("'a' - 'A' = %d\n", 'a' - 'A');
printf("sizeof c: %d\n", (int) sizeof c);
printf("sizeof (c + 1): %d\n", (int) sizeof (c + 1));
printf("sizeof (c + c): %d\n", (int) sizeof (c + c));
return 0;
}
A character is a lower-case letter when its code lies between the codes of
'a' and 'z'. Subtracting 'a' and adding
'A' converts it to upper case; the code works for ASCII, where
the letters are contiguous, but not for EBCDIC, which the book mentions as
the other common character set of the time. sizeof (c + 1) is 4:
the sum is an int, not a char. Whether a plain
char is signed is left to the machine; the book notes that
on the PDP-11 characters with the high bit set became negative integers,
while other machines of the time kept them positive.
$ gcc -std=c89 -pedantic -Wall -Wextra -o char_arith char_arith.c $ ./char_arith k lower: 1, digit: 0 upper case: K position in the alphabet: 11 'a' - 'A' = 32 sizeof c: 1 sizeof (c + 1): 4 sizeof (c + c): 4
Floating arithmetic in double
K&R C converts every float in an expression to
double, so all floating-point arithmetic is done in double
precision. A float variable saves memory, not time: its value
is widened each time it is used.
#include <stdio.h>
int main()
{
float third;
double d;
third = 1.0 / 3;
d = 1.0 / 3;
printf("sizeof third: %d\n", (int) sizeof third);
printf("sizeof (third * 3): %d\n", (int) sizeof (third * 3));
printf("sizeof (third * 3.0): %d\n", (int) sizeof (third * 3.0));
printf("third * 3 - 1: %e\n", third * 3 - 1);
printf("d * 3 - 1: %e\n", d * 3 - 1);
return 0;
}
third holds one third rounded to float precision,
d one third in double precision. The sizeof lines
show the type of each product. A float argument is passed as a
double, which is why %f and %e serve
both types.
GCC follows the later ANSI rule here, which allows float
arithmetic: third * 3 is a four-byte float, the
rounding errors cancel, and the result is exactly 0.
$ gcc -std=c89 -pedantic -Wall -Wextra -o float_arith float_arith.c $ ./float_arith sizeof third: 4 sizeof (third * 3): 4 sizeof (third * 3.0): 8 third * 3 - 1: 0.000000e+00 d * 3 - 1: 0.000000e+00
On the PDP-11 the K&R rule holds. third * 3 is an
eight-byte double: the float value of one third is
multiplied in double precision, and its rounding error appears in the
result.
$ cc float_arith.c $ a.out sizeof third: 4 sizeof (third * 3): 8 sizeof (third * 3.0): 8 third * 3 - 1: 2.980232e-08 d * 3 - 1: 0.000000e+00
Unsigned arithmetic
Unsigned arithmetic is done modulo 2n, where n is the number of
bits in the type, so it never overflows: it wraps around. When an
unsigned and an int meet in an expression, the
int is converted to unsigned, so a negative value
becomes a large positive one.
#include <stdio.h>
int main()
{
unsigned u;
unsigned short us;
int i;
u = 0;
printf("u - 1 = %u\n", u - 1);
u = 3;
i = -5;
printf("u + i = %u\n", u + i);
printf("(int) (u + i) = %d\n", (int) (u + i));
us = 1;
printf("us - 2 < 0: %d\n", us - 2 < 0);
printf("us - 2 = %d\n", us - 2);
printf("u - 4 > 0: %d\n", u - 4 > 0);
return 0;
}
0 - 1 in unsigned is the largest unsigned value.
u + i converts -5 to unsigned, and the sum wraps to a large
number, which turns back into -2 when it is cast to int.
u - 4 > 0 is true although u is 3, a classic
source of bugs in loops that count down.
The two lines with unsigned short show the
unsigned-preserving rule of the K&R compilers: an unsigned
operand makes the whole operation unsigned, so us - 2 wraps
around and can never be less than 0. The book has only unsigned
int, but the PDP-11 compiler accepts unsigned short as
well. GCC follows the later ANSI rule, which turns an unsigned
short into an int, so it prints 1 and -1.
$ gcc -std=c89 -pedantic -Wall -Wextra -o wraparound wraparound.c $ ./wraparound u - 1 = 4294967295 u + i = 4294967294 (int) (u + i) = -2 us - 2 < 0: 1 us - 2 = -1 u - 4 > 0: 1
On the PDP-11 an unsigned has 16 bits, so u - 1 is
65535. us - 2 < 0 is 0, because the subtraction is
unsigned. Its result, 65535, prints as -1 with %d.
$ cc wraparound.c $ a.out u - 1 = 65535 u + i = 65534 (int) (u + i) = -2 us - 2 < 0: 0 us - 2 = -1 u - 4 > 0: 1
Assignment operators
An expression such as total = total + 5 can be written
total += 5. Most binary operators have such an assignment form:
+=, -=, *=, /=,
%=, <<=, >>=,
&=, ^= and |=.
#include <stdio.h>
int main()
{
int total, x, n;
total = 10;
total += 5;
printf("total += 5: %d\n", total);
total -= 3;
printf("total -= 3: %d\n", total);
total *= 2;
printf("total *= 2: %d\n", total);
total /= 4;
printf("total /= 4: %d\n", total);
total %= 4;
printf("total %%= 4: %d\n", total);
x = 8;
x = -1; /* assign minus one */
printf("x = -1: %d\n", x);
x = 8;
x -= 1; /* subtract one */
printf("x -= 1: %d\n", x);
n = 6;
n <<= 2;
printf("n <<= 2: %d\n", n);
return 0;
}
Each assignment operator changes the variable and yields the new value. The
lines with x = -1 and x -= 1 look alike but do
different things: the first assigns minus one, the second subtracts one. In
old programs the spaces matter, as the next section shows.
$ gcc -std=c89 -pedantic -Wall -Wextra -o assignment_ops assignment_ops.c $ ./assignment_ops total += 5: 15 total -= 3: 12 total *= 2: 24 total /= 4: 6 total %= 4: 2 x = -1: -1 x -= 1: 7 n <<= 2: 24
The old assignment operators
In the C of the early 1970s the assignment operators were written the other
way round: =+, =-, =*. The book
already uses += and lists the old form among the anachronisms.
The old form made x=-1 ambiguous: it could mean
x =- 1, subtract one, as well as x = -1.
#include <stdio.h>
int main()
{
int x, y;
x = 10;
x =+ 5;
printf("x =+ 5 gives %d\n", x);
x =- 3;
printf("x =- 3 gives %d\n", x);
y = 10;
y=-1;
printf("y=-1 gives %d\n", y);
return 0;
}
The program uses the old operators. GCC does not know them: it reads
x =+ 5 as an assignment of +5 and x =- 3 as an
assignment of -3. The program compiles without a warning, and each line
simply assigns a value.
$ gcc -std=c89 -pedantic -Wall -Wextra -o old_ops old_ops.c $ ./old_ops x =+ 5 gives 5 x =- 3 gives -3 y=-1 gives -1
The PDP-11 compiler still knows the old operators. x =+ 5 adds
5 and x =- 3 subtracts 3. For y=-1 the compiler
warns that it assumes the old operator, and it subtracts 1 from 10. The same
source gives different results on the two compilers; spaces around the
operators avoid the question.
$ cc old_ops.c old_ops.c:13: Warning: -= operator assumed $ a.out x =+ 5 gives 15 x =- 3 gives 12 y=-1 gives 9
Bits and octal constants
The bitwise operators &, |, ^,
~, << and >> work on
integers. K&R programs write masks in octal, with a
leading zero, as UNIX file permissions still are. A hexadecimal constant
starts with 0x.
#include <stdio.h>
#define LOW7 0177
#define MODE 0644
int main()
{
int n, neg;
n = 0x1C5;
neg = -16;
printf("n & LOW7 = %d\n", n & LOW7);
printf("MODE = %o (octal), %d (decimal)\n", MODE, MODE);
printf("owner bits: %o\n", (MODE >> 6) & 07);
printf("other bits: %o\n", MODE & 07);
printf("~0 = %d\n", ~0);
printf("~0 as unsigned: %u\n", ~0);
printf("neg >> 2 = %d\n", neg >> 2);
printf("neg as unsigned >> 2 = %u\n", (unsigned) neg >> 2);
return 0;
}
n & LOW7 keeps the low seven bits of n. The
%o conversion prints a number in octal, so the permission bits of
MODE are easy to read. ~0 has all bits set; as an
int it is -1. Shifting a negative number to the right is
machine-dependent: the vacated bits may be filled with copies of the sign bit
or with zeros. GCC and the PDP-11 copy the sign bit, so -16 >> 2 is -4,
while the same bits shifted as unsigned give a large positive
number. The C bitwise operators tutorial covers
the operators in detail.
$ gcc -std=c89 -pedantic -Wall -Wextra -o bits bits.c $ ./bits n & LOW7 = 69 MODE = 644 (octal), 420 (decimal) owner bits: 6 other bits: 4 ~0 = -1 ~0 as unsigned: 4294967295 neg >> 2 = -4 neg as unsigned >> 2 = 1073741820
Order of evaluation
C does not specify the order in which the operands of most operators, or the
arguments of a function, are evaluated. An expression that modifies a
variable and also uses it elsewhere can therefore give different results on
different compilers. The book warns against such code; GCC warns about it
with -Wall.
#include <stdio.h>
int main()
{
int n, m;
n = 5;
printf("%d %d\n", n, n++);
m = 1;
m = m++ + 10;
printf("m = %d\n", m);
return 0;
}
GCC reports both lines. On this machine, the arguments of
printf are evaluated from right to left, so n++ runs
first and the call prints 6 and 5. m = m++ + 10 modifies
m twice in one expression, which is undefined. The cure is to
split such expressions into separate statements. The
&&, ||, ?: and comma operators
are the exceptions: they evaluate their left operand first.
$ gcc -std=c89 -pedantic -Wall -Wextra -o eval_order eval_order.c
eval_order.c: In function ‘main’:
eval_order.c:8:27: warning: operation on ‘n’ may be undefined [-Wsequence-point]
8 | printf("%d %d\n", n, n++);
| ~^~
eval_order.c:11:7: warning: operation on ‘m’ may be undefined [-Wsequence-point]
11 | m = m++ + 10;
| ~~^~~~~~~~~~
$ ./eval_order
6 5
m = 11
The PDP-11 compiler gives no warning. It also evaluates the arguments of
printf from right to left, but it increments m
after the assignment, so m ends up as 12.
$ cc eval_order.c $ a.out 6 5 m = 12
Source
Dennis Ritchie: C Reference Manual (1975)
GCC: Integers implementation-defined behavior
In this article we have covered the parts of C expressions that are
particular to K&R C: integers as truth values, integer and negative
division, the promotion of char and float,
unsigned arithmetic and the unsigned-preserving rule, the old assignment
operators, bits in octal, and the order of evaluation. The next article,
K&R C control flow, uses these
expressions as conditions. The shared operators are described in
C operators.
Author
List all K&R C tutorials.