From WikiChip
Data Types - C
< c(Redirected from Data types - C)

The meaning of a value stored in an object is determined solely by the type of the expression used to access it - for example the type used in the variable declaration. There are two categories of types:

  • object types - types used to describe objects
  • function types - types used to describe functions

Additionally an object may have a complete or incomplete type. An incomplete type is a type that describes an object but lacks information needed to determine its size. For example: an array of unknown size or struct or union types whose members were not specified. A complete type is a type that describes an object with sufficient information needed to determine its size.

Basic Types[edit]

Basic types are the fundamental building blocks of all C programs. All basic types are complete types. Basic types are divided into four sub-categories: char type, signed integer types, unsigned integer types, and floating types.

Character Types[edit]

There are three distinct character types: char, signed char, and unsigned char. Depending on the implementation used, a char might be signed or unsigned.

Type Explanation Min Macro At least Min Value Max Macro At least Max Value
char Either a "signed char" or a "unsigned char". CHAR_MIN CHAR_MAX
signed char Same as char, guaranteed to be signed. SCHAR_MIN -127 SCHAR_MAX +127
unsigned char Same as char, guaranteed to be unsigned. 0 UCHAR_MAX +255

Signed Integer Types[edit]

The standard defines five signed integer types:

  • signed char
  • short int
  • int
  • long int
  • long long intC99

In this list, each type provides at least as much storage as those preceding it.

Type Explanation Min Macro At least Min Value Max Macro At least Max Value since
short
short int
signed short
signed short int
Short signed integer type.
At least 16 bits in size.
SHRT_MIN -32767 SHRT_MAX +32767 C89
int
signed int
An integer type.
At least 16 bits in size.
INT_MIN -32767 INT_MAX +32767
long
long int
signed long
signed long int
A long integer type.
At least 32 bits in size.
LONG_MIN -2147483647 LONG_MAX +2147483647
long long
long long int
signed long long
signed long long int
A long long integer type.
At least 64 bits in size.
LLONG_MIN -9223372036854775807 LLONG_MAX +9223372036854775807 C99

The macros in the table can be found in the <limits.h> header.

Unsigned Integer Types[edit]

For each of the signed integer types defined, there is a corresponding unsigned type. Unsigned types are designated with the keyword unsigned. The unsigned types occupy the same amount of space as their signed counterparts. The range of nonnegative values of a signed integer type is a subrange of the corresponding unsigned type.

Note that unsigned integer types computations can never overflow. Results that cannot be represented by the specified type are reduced modulo the number that is one greater than the largest value that can be represented by that type.


Type Explanation Max Macro At least Max Value since
unsigned short
unsigned short int
Short unsigned integer type.
At least 16 bits in size.
USHRT_MIN +65535 C89
unsigned
unsigned int
An unsigned integer type.
At least 16 bits in size.
UINT_MAX +65535
unsigned long
unsigned long int
An unsigned long integer type.
At least 32 bits in size.
ULONG_MIN +4294967295
unsigned long long
unsigned long long int
An unsigned long long integer type.
At least 64 bits in size.
ULLONG_MIN 18446744073709551615 C99

The macros in the table can be found in the <limits.h> header.

Extended Unsigned/Signed Integer Types[edit]

Extended signed integer types and extended unsigned integer types are implementation-specific integer types that are provided as an extension to the ones defined by the standard. Although the C standard can't define them or describe their underlying types, it does setup a set of rule by which implementations much conform to in order to ensure well-defined behavior.

Floating Types[edit]

Standard C has two types of floating point types: real and complex

Real Floating Types[edit]

Standard C provides three real floating types.

  • float
  • double
  • long doubleC99

Each type is a subset of the smaller type.

Complex TypesC99[edit]

Standard C provides three complex types. Complex types are the same as the real floating types with the addition of the _Complex keyboard. The types are:

Void Type[edit]

The void type comprises an empty set of values. The void type is an incomplete type which cannot be completed.

  • float _Complex
  • double _Complex
  • long double _Complex

Boolean TypeC99[edit]

Starting with C99, the C Standard introduced the _Bool type - an object large enough to store the values 0 and 1.

The standard also introduced a new header: <stdbool.h>. It is recommended that the header be used wherever the use of bool is needed instead of using _Bool directly. The header defines 4 macros:

  • bool - expands to _Bool
  • true - expands to 1
  • false - expands to 0
  • __bool_true_false_are_ - expands to 1

For example:

#include <stdio.h>
#include <stdbool.h>
int main(void)
{
    bool b = true;
    if (b)
        puts("It's true!");
    return 0;
}

Enumerated Type[edit]

An enumerated type is a named integer constant value. An enumeration comprises a set of enumerated types. For example:

enum enemies
{
    ghosts = 1,
    goblins,
    octopi,
    monsters
};

Array Type[edit]

An array type describes a contiguously allocated nonempty set of objects. Each individual element member object is called an element type. The array type is derived from its element type. (For example, if the element type is 'int', the array is said to be an 'array of int'.)

Structure Type[edit]

A structure type describes a set of nonempty member objects with an optional name and type.

struct student
{
    unsigned int student_id;
    char *first_name;
    char *last_name;
    float gpa;
};

Union Type[edit]

A union type describes a nonempty set of overlapping member objects.

Function Type[edit]

A function type describes a function with a specified return type. The number and types of its parameters is used to characterize the function type.

Pointer Type[edit]

A pointer type describes an object whose value provides a reference to an object of a stated type. A pointer type can be derived from both function and object types.

Atomic Type C11[edit]

An atomic type describes the type designated by the atomic type specifier. Array types, function types, and qualified types cannot be atomic types.