ArticleslgStudy

science

C data types

C data types is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand C data types rather than just read about it. In short: In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables.

Key takeaways

  • C data types belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect C data types to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of C data types from memory before moving on to harder problems.

Reference excerpt

In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements. The C language provides basic arithmetic types, such as integer and real number types, and syntax to build array and compound types. The C standard library contains additional definitions of support types, that have additional properties, such as providing storage with an exact size, independent of the language implementation on specific hardware platforms.

Primary types

Main types The C language provides the four basic arithmetic type specifiers char, int, float and double (as well as the Boolean type bool), and the modifiers signed, unsigned, short, and long. The following table lists the permissible combinations in specifying a large set of storage size-specific declarations.

The actual size of the integer types varies by implementation. The standard requires only size relations between the data types and minimum sizes for each data type: The relation requirements are that the long long is not smaller than long, which is not smaller than int, which is not smaller than short. As char's size is always the minimum supported data type, no other data types (except bit-fields) can be smaller. The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits. The type int should be the integer type that the target processor is most efficiently working with. This allows great flexibility: for example, all types can be 64-bit. However, several different integer width schemes (data models) are popular. Because the data model defines how different programs communicate, a uniform data model is used within a given operating system application interface. In practice, char is usually 8 bits in size and short is usually 16 bits in size (as are their unsigned counterparts). This holds true for platforms as diverse as 1990s SunOS 4 Unix, Microsoft MS-DOS, modern Linux, and Microchip MCC18 for embedded 8-bit PIC microcontrollers. POSIX requires char to be exactly 8 bits in size. Various rules in the C standard make unsigned char the basic type used for arrays suitable to store arbitrary non-bit-field objects: its lack of padding bits and trap representations, the definition of object representation, and the possibility of aliasing. The actual size and behavior of floating-point types also vary by implementation. The only requirement is that long double is not smaller than double, which is not smaller than float. Usually, the 32-bit and 64-bit IEEE 754 binary floating-point formats are used for float and double respectively. The C99 standard includes new real floating-point types float_t and double_t, defined in <math.h>. They correspond to the types used for the intermediate results of floating-point expressions when FLT_EVAL_METHOD is 0, 1, or 2. These types may be wider than long double. C99 also added complex types: float _Complex, double _Complex, long double _Complex. C11 added imaginary types (which were described in an informative annex of C99): float _Imaginary, double _Imaginary, long double _Imaginary. Including the header <complex.h> allows all these types to be accessed with using complex and imaginary respectively.

Boolean type C99 added a Boolean data type _Bool. Additionally, the <stdbool.h> header defines bool as a convenient alias for this type, and also provides macros for true and false. _Bool functions similarly to a normal integer type, with one exception: any conversion to a _Bool gives 0 (false) if the value equals 0; otherwise, it gives 1 (true). This behavior exists to avoid integer overflows in implicit narrowing conversions. For example, in the following code:

Variable b evaluates to false if unsigned char has a size of 8 bits. This is because the value 256 does not fit in the data type, which results in the lower 8 bits of it being used, resulting in a zero value. However, changing the type causes the previous code to behave normally:

The type _Bool also ensures true values always compare equal to each other:

In C23, bool (and its values true and false) became a core functionality of the language (making the contents of <stdbool.h> obsolescent), allowing for the following examples of code:

Bit-precise integer types Since C23, the language allows the programmer to define integers that have a width of an arbitrary number of bits. Those types are specified as _BitInt(N), where N is an integer constant expression that denotes the number of bits, including the sign bit for signed types, represented in two's complement. The maximum value of N is provided by BITINT_MAXWIDTH and is at least ULLONG_WIDTH. Therefore, the type _BitInt(2) (or signed _BitInt(2)) takes values from −2 to 1 while unsigned _BitInt(2) takes values from 0 to 3. The type unsigned _BitInt(1) also exists, being either 0 or 1 and has no equivalent signed type. A proposal for C2Y proposes to lift this restriction and allow signed _BitInt(1) which then has the possible values 0 and -1, removing the special case for unsigned _BitInt(1).

Size and pointer difference types The C language specification includes the typedefs size_t and ptrdiff_t to represent memory-related quantities. Their size is defined according to the target processor's arithmetic capabilities, not the memory capabilities, such as available address space. Both of these types are defined in the <stddef.h> header. size_t is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. The operator sizeof yields a value of the type size_t. The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the <stdint.h> header. size_t is guaranteed to be at least 16 bits wide. Additionally, POSIX includes ssize_t, which is a signed integer type of the same width as size_t. ptrdiff_t is a signed integer type used to represent the difference between pointers. It is guaranteed to be valid only against pointers of the same type; subtraction of pointers consisting of different types is implementation-defined.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with C data types

Start with the simplest possible case. Write down what C data types claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to C data types before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about C data types ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of C data types

In research
C data types appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses C data types in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
C data types is common in secondary-school and first-year university syllabi. It links to neighbouring topics C (programming language), C standard library, Data types, so understanding it makes those chapters shorter.
In everyday life
Look for C data types outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “C data types” →

How to study C data types in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what C data types means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain C data types out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is C data types in simple terms?

In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables.

Why does C data types matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study C data types?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on C data types.

Tags

  • C (programming language)
  • C standard library
  • Data types

Keep exploring