YOUR C LEARNING COMPANION

Find your next idea.

Learning path
C foundationsChapter 8 of 73

Data types, ranges, and representations

Choose types by meaning, range, and precision, and distinguish C guarantees from familiar machine sizes.

7 min readFoundationsC17 baseline
Offline reading
In this chapter10 sections

A book count and a measured length describe different kinds of information. C uses types to give values rules: which values fit, which operations apply, and how conversions work. Size is one part of that story.

1. Start with the meaning of the data

Books are normally counted in whole numbers. A length may need a fractional part. A grade may be represented by a character. Choose a type that matches the information, then check its limits. Even a nonnegative count can be mishandled by unsigned subtraction, so meaning and operations must be considered together.

C17 · illustrative fragment
int count = 4;
double length = 2.5;
char grade = 'A';

A value is the information, such as the number four. Its object representation is how storage encodes it. Different implementations can store the same value differently. A type name does not promise one universal pattern of bytes.

2. Integer types have finite ranges

C provides integer types including signed char, short, int, long, and long long, with corresponding unsigned types. short and long here are familiar abbreviated spellings for integer types. The standard sets minimum ranges and ordering requirements, while implementations can provide more range. It does not require a four-byte int.

For example, C17 guarantees that int can represent at least −32767 through 32767. Common desktop implementations provide a wider range. Use INT_MIN and INT_MAX from <limits.h> when the actual limits matter. A table of common hardware sizes is useful only if it remains clearly labeled as a table of examples.

3. Signed and unsigned arithmetic have different boundaries

Signed integer types include negative and nonnegative values. Unsigned types start at zero. Unsigned arithmetic wraps modulo one more than the type’s maximum. Ordinary signed arithmetic has a different rule: a result outside its representable range has undefined behavior. Check limits before an operation when overflow is possible.

C17 · illustrative fragment
unsigned int wrapped = 0u - 1u;

Here both operands are unsigned int, so the result is that type’s maximum, not a negative integer. The u suffix is part of how these small integer constants get their types. Wraparound is a defined rule, but a defined result can still be the wrong answer for a count or a storage-size calculation. Check the problem’s requirements before choosing the representation.

4. Characters and bytes are related, not identical ideas

sizeof(char) is always one. That unit is a C byte. To find how many bits it contains, use CHAR_BIT; the value is at least eight. sizeof reports sizes in C bytes and returns a value of type size_t. One C byte does not universally mean eight bits.

Plain char, signed char, and unsigned char are distinct types. Whether plain char behaves as signed or unsigned is implementation-defined. A plain character constant such as 'A' has type int in C; assigning it to char does not change that fact about the constant. A string literal such as "A" is a different construct: a character array with a terminating null character.

Do not equate one char object with one user-visible character in every text encoding. For example, a UTF-8 character can require several bytes. Character encodings and string handling deserve their own detailed treatment; the first rule is to keep the storage unit, numeric code, encoding, and displayed character distinct.

5. Floating-point types trade range and precision

Floating-point types include float, double, and long double. They can represent fractions, but their range and precision are finite. On common binary implementations, 0.1 is stored approximately. C does not require every implementation to use the same floating-point format.

Range is about how large or small representable values can be; precision is about the information retained in a value. They are related but different properties. <float.h> describes implementation characteristics. A larger-looking type name is not a substitute for deciding acceptable error, comparisons, and rounding for the problem.

6. Boolean and void serve different purposes

C17 · illustrative fragment
_Bool available = 5;

C17 has the Boolean type _Bool. Conversion to it produces zero when the source compares equal to zero and one otherwise. The example stores one. In C17, <stdbool.h> supplies the familiar bool, true, and false macros. Conditions also work with other scalar types; C does not require every condition expression to have Boolean type.

void has specific jobs in C. For example, a void return type says that a function returns no value. You cannot define an ordinary object of type void; it is an incomplete type that cannot be completed. A void * pointer is a separate type and will be explained with pointers.

7. A conversion affects a use, not the object’s declared type

An integer can be converted for a floating-point computation without changing the original integer object’s declared type. Narrow integer types may first undergo integer promotion. Mixing signed and unsigned values can then select unsigned arithmetic or comparison. Learn the conversion rules instead of the slogan that a “bigger” type always wins.

Go deeper: exact-width names are conditional

<stdint.h> supplies exact-width types such as int32_t only when the implementation has a suitable type meeting the required representation and width. Least-width types, such as int_least32_t, express an at-least requirement. Using a fixed-width name does not by itself define file byte order or a portable binary format.

Think it through

A machine reports sizeof(int) == 4. What else must you know before concluding that its int object occupies exactly 32 bits?

Read the explanation

You need the number of bits in a C byte, CHAR_BIT. Four C bytes mean 4 * CHAR_BIT bits of object representation. Even that does not make every representation bit a signed value bit; representation details and range are separate questions.

Primary references and further reading

Return to Floating-point calculations and mathematical functions after the library and error-handling chapters. It develops the range and precision ideas into an application error policy.

Before you move on

  • Types govern values, operations, ranges, and conversions, as well as storage requirements.
  • Unsigned wrapping and ordinary signed overflow follow different rules.
  • C bytes and floating-point formats need implementation-specific properties when exact limits matter.
Keep these ideas separate
Range / Precision
Range concerns supported magnitudes; precision concerns how much detail a value can retain.
Explain it in your own words

Which properties would you check before choosing a type for counts and measured lengths?

Optional: say it aloud or keep a note. You can continue whenever you are ready.

Check your understanding

Optional. Answers are not saved or sent, and reading stays unrestricted.

What can portable C code assume about sizeof(char)?
Read the explanation for every choice
  1. It is always 8.

    Eight is a common number of bits in a byte, not the value of sizeof(char).

  2. It equals sizeof(int) on every implementation.

    The standard does not require those two sizes to be equal.

  3. It is 1, measured in C bytes.

    ✓ Correct reasoning. A C byte is the size of char. CHAR_BIT specifies how many bits that byte contains.

C17

Quick reference from the earlier edition

This compact reference keeps existing links and saved passages available. Read the expanded lesson above for the full context.

C's fundamental types stay deliberately close to the underlying hardware, which is part of why their exact sizes are implementation-defined rather than fixed by the standard.

Integer Types

Type Typical Size Typical Signed Range
char 1 byte -128 to 127 (or 0-255, if unsigned by default)
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 4 or 8 bytes platform-dependent
long long (C99) 8 bytes roughly -9.2x10^18 to 9.2x10^18

The table shows common sizes on systems with eight-bit bytes, not required widths. In C, sizeof(char) == 1, and CHAR_BIT from <limits.h> gives the number of bits in a byte (at least 8). The standard specifies minimum integer ranges and ordered sizes. Exact-width names such as int32_t and uint64_t from <stdint.h> exist only when the implementation provides suitable exact types. The standard least-width types, such as int_least32_t, provide at least the requested width.

Floating-Point Types

On many platforms, float uses IEEE 754 binary32 and double uses binary64, but the C standard does not require those formats. long double may offer extra precision or may use the same representation and precision as double. Check <float.h> limits such as FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG, and DBL_MAX.

Character Type

char is unusual: the standard leaves it implementation-defined whether a plain char behaves as signed or unsigned. Code that depends on the sign of character data should say so explicitly with signed char or unsigned char.

Boolean Type

Before C99, C had no dedicated boolean type — 0 meant false and any nonzero value meant true, a convention still used throughout the language today. C99 added _Bool along with the friendlier <stdbool.h> header, which defines bool, true, and false as macros (C23 later promotes all three to genuine keywords, as explained in the C23 reference).

Type Conversions

Integer promotions convert types such as char and short to int if it can represent every value, otherwise to unsigned int. The usual arithmetic conversions then find a common type for many binary arithmetic operations; mixing signed and unsigned types can change the result unexpectedly. A cast explicitly converts an expression, for example (double)a / b performs floating division when b is an integer. A cast does not prevent later conversions imposed by the surrounding expression or assignment.

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 08 / 73

In this chapter

Data types, ranges, and representations

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping