K&R C types and declarations
last modified December 5, 2026 K&R C 1978
Every variable in C has a type, and every variable must be declared before it
is used. This article covers the basic types of K&R C, how variables are
declared and initialized, symbolic constants, character constants, the
conversions between types, and the rule that a missing type means
int.
Basic types
K&R C has four basic types. char holds one character of the
machine's character set, int an integer in the natural size of
the machine, float a single-precision floating-point number, and
double a double-precision one. There is no boolean type and no
string type; the operators and
strings articles show what C uses instead.
#include <stdio.h>
int main()
{
char grade;
int year;
float price;
double distance;
grade = 'B';
year = 1978;
price = 22.5;
distance = 384400.0;
printf("grade: %c\n", grade);
printf("year: %d\n", year);
printf("price: %f\n", price);
printf("distance: %f\n", distance);
return 0;
}
A declaration names the type and then the variable. In K&R C all
declarations of a block come first, before any statement. The
%f conversion prints a float or a
double with six digits after the decimal point.
$ gcc -std=c89 -pedantic -Wall -Wextra -o basic_types basic_types.c $ ./basic_types grade: B year: 1978 price: 22.500000 distance: 384400.000000
Declarations and initialization
One declaration may list several variables of the same type, separated by commas. A variable can be given a value in its declaration; this is called initialization. A local variable may be initialized with any expression whose values are known at that point.
#include <stdio.h>
int main()
{
int width = 12, height = 5;
int area = width * height;
int perimeter;
char unit = 'm';
perimeter = 2 * (width + height);
printf("area: %d square %c\n", area, unit);
printf("perimeter: %d %c\n", perimeter, unit);
width = 20;
printf("new width: %d\n", width);
printf("area is still %d\n", area);
return 0;
}
area is initialized with an expression that uses the two
variables declared before it. An initialization happens once: changing
width later does not change area. A local variable
without an initializer has an undefined value until it is assigned, so
perimeter must not be read before the assignment.
$ gcc -std=c89 -pedantic -Wall -Wextra -o declarations declarations.c $ ./declarations area: 60 square m perimeter: 34 m new width: 20 area is still 60
Initializers without the equals sign
The C of the early 1970s wrote an initializer without the equals sign:
int count 10;. The book lists this form among the
anachronisms, old constructions that compilers still accept but that
programs should no longer use.
#include <stdio.h>
int count 10;
int year 1978;
int main()
{
printf("count: %d\n", count);
printf("year: %d\n", year);
return 0;
}
The two external variables are initialized in the old style. GCC does not
know the form: it reports a syntax error at each declaration, and then
reports the variables as undeclared where main uses them.
$ gcc -std=c89 -pedantic -Wall -Wextra -o old_init old_init.c
old_init.c:3:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before numeric constant
3 | int count 10;
| ^~
old_init.c:4:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before numeric constant
4 | int year 1978;
| ^~~~
old_init.c: In function ‘main’:
old_init.c:8:27: error: ‘count’ undeclared (first use in this function)
8 | printf("count: %d\n", count);
| ^~~~~
old_init.c:8:27: note: each undeclared identifier is reported only once for each function it appears in
old_init.c:9:26: error: ‘year’ undeclared (first use in this function)
9 | printf("year: %d\n", year);
| ^~~~
The PDP-11 compiler still accepts the old form, and the program runs.
$ cc old_init.c $ a.out count: 10 year: 1978
short, long, and unsigned
The qualifiers short and long give integers of
other sizes, and unsigned gives integers that are never
negative. With a qualifier the word int may be dropped:
short means short int and unsigned
means unsigned int.
#include <stdio.h>
int main()
{
short int temperature;
long int population;
unsigned int count;
short delta; /* short int */
long seconds; /* long int */
unsigned mask; /* unsigned int */
temperature = -12;
population = 2100000L;
count = 40000;
delta = 3;
seconds = 86400L * 365;
mask = 0177;
printf("temperature: %d\n", temperature);
printf("population: %ld\n", population);
printf("count: %u\n", count);
printf("delta: %d\n", delta);
printf("seconds in a year: %ld\n", seconds);
printf("mask: %u\n", mask);
return 0;
}
An L after an integer constant makes it long, so
86400L * 365 is computed in long arithmetic. A
constant with a leading zero is octal: 0177 is 127. A
long is printed with %ld and an
unsigned with %u; a short is printed
with %d, because it becomes an int when it is
passed to printf.
The book only promises that short is no longer than
int and long no shorter; the
sizeof section shows the real sizes. In K&R C,
unsigned applies only to int: there is no
unsigned char and no unsigned long.
$ gcc -std=c89 -pedantic -Wall -Wextra -o qualifiers qualifiers.c $ ./qualifiers temperature: -12 population: 2100000 count: 40000 delta: 3 seconds in a year: 31536000 mask: 127
Symbolic constants
In K&R C a named constant is a #define line, handled by the
preprocessor before the compiler sees the code: every later occurrence of the
name is replaced by the text that follows it.
#include <stdio.h>
#define MINUTE 60
#define HOUR (60 * MINUTE)
#define DAY (24 * HOUR)
#define WEEK (7 * DAY)
#define TITLE "Seconds in a time unit"
int main()
{
int days;
days = 3;
printf("%s\n", TITLE);
printf("minute: %d\n", MINUTE);
printf("hour: %d\n", HOUR);
printf("day: %d\n", DAY);
printf("week: %d\n", WEEK);
printf("%d days: %d\n", days, days * DAY);
printf("a week has %d hours\n", WEEK / HOUR);
return 0;
}
By convention, symbolic constants are written in capitals. A
#define line does not end with a semicolon, because the
semicolon would become part of the replacement text. The definitions build on
each other, and each one is enclosed in parentheses: without them,
WEEK / HOUR would expand to 7 * 24 * 60 * 60 / 60 *
60, which is not 168. A constant can also stand for a string. The
preprocessor article covers macros in
detail.
$ gcc -std=c89 -pedantic -Wall -Wextra -o constants constants.c $ ./constants Seconds in a time unit minute: 60 hour: 3600 day: 86400 week: 604800 3 days: 259200 a week has 168 hours
On the PDP-11 an int has 16 bits and holds values up to 32767.
MINUTE and HOUR fit, but DAY is 86400,
and the multiplication overflows without any message. Every value computed
from it is wrong, and the week has only 4 hours. A program for the PDP-11
writes (24L * HOUR), which makes the arithmetic
long, and prints the values with %ld.
$ cc constants.c $ a.out Seconds in a time unit minute: 60 hour: 3600 day: 20864 week: 14976 3 days: -2944 a week has 4 hours
Character constants
A character in single quotes is a character constant. Its value
is the number of the character in the machine's character set, so characters
take part in arithmetic like small integers. Special characters are written
as escape sequences, and any character can be given by its octal code, as in
'\101'.
#include <stdio.h>
int main()
{
char c, next, digit;
int value;
c = 'A';
next = c + 1;
digit = '7';
value = digit - '0';
printf("%c has the code %d\n", c, c);
printf("the next letter is %c\n", next);
printf("'%c' - '0' is %d\n", digit, value);
printf("octal escape \\101 is %c\n", '\101');
printf("newline is %d, tab is %d, null is %d\n", '\n', '\t', '\0');
return 0;
}
In ASCII, 'A' has the code 65. Adding 1 gives the next letter,
and subtracting '0' from a digit character gives the digit's
value, because the digits have consecutive codes. '\0', the
character with the code 0, is the null character that ends every
string. A character constant is an int, not a
char, which is why it prints with %d as well as
%c.
$ gcc -std=c89 -pedantic -Wall -Wextra -o characters characters.c $ ./characters A has the code 65 the next letter is B '7' - '0' is 7 octal escape \101 is A newline is 10, tab is 9, null is 0
Conversions
When a value of one type is assigned to a variable of another type, it is
converted. An integer becomes a floating-point number without loss. A
floating-point number becomes an integer by dropping the fraction. A longer
integer assigned to a char keeps only its low-order bits. A
cast, a type name in parentheses, converts a value explicitly.
#include <stdio.h>
int main()
{
int i, n;
float f;
double d;
char c;
i = 7;
f = i; /* int to float */
printf("f = %f\n", f);
d = 3.99;
n = d; /* double to int: the fraction is dropped */
printf("n = %d\n", n);
n = -3.99;
printf("n = %d\n", n);
c = 'z';
i = c; /* char to int */
printf("i = %d\n", i);
i = 321;
c = i; /* int to char: the high bits are lost */
printf("c = %d (%c)\n", c, c);
d = (double) 7 / 2;
printf("d = %f\n", d);
printf("(int) 2.75 = %d\n", (int) 2.75);
return 0;
}
3.99 and -3.99 become 3 and -3: the fraction is dropped toward zero, not
rounded. 321 does not fit in a char; only its low eight bits
remain, which are 65, the code of A. The compiler does not
warn about these conversions. In (double) 7 / 2 the cast turns
7 into a double before the division, so the result keeps its
fraction. The operators article explains
integer and floating division.
$ gcc -std=c89 -pedantic -Wall -Wextra -o conversions conversions.c $ ./conversions f = 7.000000 n = 3 n = -3 i = 122 c = 65 (A) d = 3.500000 (int) 2.75 = 2
sizeof
The sizeof operator gives the size of a type or a variable in
bytes. A type name must be enclosed in parentheses; a variable may be written
without them. The sizes depend on the machine and the compiler.
#include <stdio.h>
int main()
{
long big;
char letter;
int n;
printf("char: %d\n", (int) sizeof(char));
printf("short: %d\n", (int) sizeof(short));
printf("int: %d\n", (int) sizeof(int));
printf("long: %d\n", (int) sizeof(long));
printf("float: %d\n", (int) sizeof(float));
printf("double: %d\n", (int) sizeof(double));
printf("big: %d\n", (int) sizeof big);
printf("letter: %d\n", (int) sizeof letter);
printf("'A': %d\n", (int) sizeof 'A');
n = 40000;
printf("40000 in an int: %d\n", n);
return 0;
}
With GCC, sizeof yields an unsigned value wider than an
int, so the program casts each size to int before
it prints it with %d. On this 64-bit Linux system an
int has four bytes and a long eight.
sizeof 'A' is the size of an int, which confirms
that a character constant is an int. The last line stores 40000
in an int.
$ gcc -std=c89 -pedantic -Wall -Wextra -o sizes sizes.c $ ./sizes char: 1 short: 2 int: 4 long: 8 float: 4 double: 8 big: 8 letter: 1 'A': 4 40000 in an int: 40000
On the PDP-11, the machine of the book, an int has two bytes and
a long four. 40000 does not fit in 16 bits: the assignment keeps
the low 16 bits, and read as a signed number they are -25536.
$ cc sizes.c $ a.out char: 1 short: 2 int: 2 long: 4 float: 4 double: 8 big: 4 letter: 1 'A': 2 40000 in an int: -25536
Implicit int
In K&R C, a declaration without a type means int. A
function defined without a return type returns an int, so the
book wrote main() instead of int main() and often
left out the return statement.
#include <stdio.h>
main()
{
printf("Hello there!\n");
}
GCC compiles the program but warns twice: once for the missing return type
and once because main ends without returning a value. The
program runs, but its exit status is undefined. Here it is 13, the number of
characters that printf printed: the return value of
printf was left in the register where the caller looks for the
result of main. A different compiler or a different program
gives a different number. That is why the series writes
int main() and ends it with return 0;. The same
rule makes short, long and unsigned
mean short int, long int and unsigned
int.
$ gcc -std=c89 -pedantic -Wall -Wextra -o implicit_int implicit_int.c
implicit_int.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
3 | main()
| ^~~~
implicit_int.c: In function ‘main’:
implicit_int.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]
6 | }
| ^
$ ./implicit_int
Hello there!
$ echo $?
13
The PDP-11 compiler accepts the program without a word, as the compilers of its time did. There the exit status happens to be 0, but it is just as undefined.
$ cc implicit_int.c $ a.out Hello there! $ echo $? 0
Source
GCC: Options to Request or Suppress Warnings
Dennis Ritchie: C Reference Manual (1975)
In this article we have covered the basic types of K&R C, declarations
and initialization, the old initializers, the short, long and
unsigned qualifiers, symbolic constants, character constants,
conversions, sizeof, and implicit int. The next
article, K&R C operators and expressions,
shows how these types combine in expressions. Modern C types are covered in
C data types.
Author
List all K&R C tutorials.