C data types

C data types

In the C programming language, data types refers to an extensive system for declaring variables of different types. The language itself provides basic arithmetic types and syntax to build array and compound types. Several headers in the standard library contain definitions of support types, that have additional properties, such as exact size, guaranteed.[1]

Contents

Basic types

The C language provides a lot of basic types. Most of them are formed from one of the four basic arithmetic type identifiers in C (char, int, float and double), and optional specifiers (signed, unsigned, short, long). All available basic arithmetic types are listed below:

Type Explanation Type Explanation
char smallest addressable unit of the machine that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned depending on implementation signed char same as char, but guaranteed to be signed.
unsigned char same as char, but guaranteed to be unsigned.
short
short int
signed short
signed short int
short signed integer type. At least 16 bits in size. unsigned short
unsigned short int
same as short, but unsigned.
int
signed int
basic signed integer type. At least 16 bits in size. unsigned
unsigned int
same as int, but unsigned.
long
long int
signed long
signed long int
long signed integer type. At least 32 bits in size. unsigned long
unsigned long int
same as long, but unsigned.
long long
long long int
signed long long
signed long long int
long long signed integer type. At least 64 bits in size. Specified only in C99 version of the standard. unsigned long long
unsigned long long int
same as long long, but unsigned. Specified only in C99 version of the standard.
float single precision floating-point type. Actual properties unspecified, however on most systems this is IEEE 754 single precision floating point format.
double double precision floating-point type. Actual properties unspecified, however on most systems this is IEEE 754 double precision floating point format.
long double extended precision floating-point type. Actual properties unspecified. On most systems this is equivalent either to double, 80-bit floating point format, or IEEE 754 quadruple precision floating-point format.

The actual size of integer types varies by implementation. The only guarantee is that the long long is not smaller than long, which is not smaller than int, which is not smaller than short. This allows great flexibility, for example, all types can be 64-bit. However, only several different integer width schemes (data models) are popular and since data model defines how different programs communicate, an uniform data model is used within a given operating system. See here for more information.

The actual size of floating point types also varies by implementation. The only guarantee is that the long double is not smaller than double, which is not smaller than float. Usually, 32-bit and 64-bit IEEE 754 floating point formats are used.

Interface to the properties of the basic types

Information about the actual properties, such as size, of the basic arithmetic types, is provided via macro constants in two headers: limits.h header (climits header in C++) defines macros for integer types and float.h header (cfloat header in C++) defines macros for floating-point types. The actual values depend on the implementation.

Properties of integer types
  • CHAR_BIT - size of the char type in bits
  • SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN(C99) - minimum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long
  • SCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX(C99) - maximum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long
  • UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, ULLONG_MAX(C99) - maximum possible value of unsigned integer types: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long
  • CHAR_MIN - minimum possible value of char
  • CHAR_MAX - maximum possible value of char
  • MB_LEN_MAX - maximum number of bytes in a multibyte character
Properties of floating-point types
  • FLT_MIN, DBL_MIN, LDBL_MIN - minimum value of float, double, long double respectively
  • FLT_MAX, DBL_MAX, LDBL_MAX - maximum value of float, double, long double respectively
  • FLT_ROUNDS - rounding mode for floating-point operations
  • FLT_EVAL_METHOD - evaluation method of expressions involving different floating-point types (only available in C99)
  • FLT_RADIX - radix of the exponent in the floating-point types
  • FLT_DIG, DBL_DIG, LDBL_DIG - number of decimal digits that can be represented without losing precision by float, double, long double respectively
  • FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON - difference between 1.0 and the next representable value of float, double, long double respectively
  • FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG - number of FLT_RADIX-base digits in the floating-point mantissa for types float, double, long double respectively
  • FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP - minimum negative integer such that FLT_RADIX raised to a power one less than that number is a normalized float, double, long double respectively
  • FLT_MIN_10_EXP, DBL_MIN_10_EXP, LDBL_MIN_10_EXP - minimum negative integer such that 10 raised to a power one less than that number is a normalized float, double, long double respectively
  • FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP - maximum positive integer such that FLT_RADIX raised to a power one more than that number is a normalized float, double, long double respectively
  • FLT_MAX_10_EXP, DBL_MAX_10_EXP, LDBL_MAX_10_EXP - maximum positive integer such that 10 raised to a power one more than that number is a normalized float, double, long double respectively
  • DECIMAL_DIG - at least 10 (only available in C99)

Boolean type

The C language did not have a boolean type until the C99 version of the standard. In C99 the boolean variable has been added as _Bool. Additionally, a new header stdbool.h has been added for compatibility reasons. This header allows programmers to use boolean types in the same way, as in C++ language. The missing identifiers are defined as macros - bool is defined as _Bool, true as 1, false as 0. Additionally, __bool_true_false_are_defined is defined as 1.

Fixed width integer types

The C99 standard includes definitions of several new integer types to enhance the portability of programs. The already available basic integer types were deemed insufficient, because their sizes are implementation defined. The new types are especially useful in embedded environments where hardware supports usually only several types and that support varies from system to system. All new types are defined in stdint.h header (cstdint header in C++). The types can be grouped into the following categories:

  • Exact width integer types which are guaranteed to have the the same number N of bits across all implementations. Included only if it is available in the implementation.
  • Least width integer types which are guaranteed to be the smallest type available in the implementation, that has at least specified number N of bits. Guaranteed to be specified for at least N=8,16,32,64.
  • Fastest integer types which are guaranteed to be the fastest integer type available in the implementation, that has at least specified number N of bits. Guaranteed to be specified for at least N=8,16,32,64.
  • Pointer integer types which are guaranteed to be able to hold a pointer
  • Maximum width integer types which are guaranteed to be the largest integer type in the implementation


The following table summarizes the types and the interface to acquire the implementation details (N refers to the number of bits):

Type category Signed types Unsigned types
Type Minimum value Maximum value Type Minimum value Maximum value
Exact width intN_t INTNMIN INTNMAX uintN_t 0 UINTNMAX
Least width int_leastN_t INT_LEASTNMIN INT_LEASTNMAX uint_leastN_t 0 UINT_LEASTNMAX
Fastest int_fastN_t INT_FAST_NMIN INT_FASTNMAX uint_fastN_t 0 UINT_FASTNMAX
Pointer intptr_t INTPTR_MIN INTPTR_MAX uintptr_t 0 UINTPTR_MAX
Maximum width intmax_t INTMAX_MIN INTMAX_MAX uintmax_t 0 UINTMAX_MAX


Printf and scanf format specifiers

The inttypes.h header (cinttypes header in C++) provides features that enhances the functionality of the types defined in stdint.h header. The included things are macros that define printf format string and scanf format string specifiers corresponding to the stdint.h types and several functions for working with intmax_t and uintmax_t types. This header is available only in C99 version of the standard.

Printf format string

All defined macros are in the following format: PRI{fmt}{type}. Here {fmt} defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). {type} defines the type of the argument and is one of N, FASTN, LEASTN, PTR, MAX, where N corresponds to the width of the type.

Scanf format string

All defined macros are in the following format: SCN{fmt}{type}. Here {fmt} defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). {type} defines the type of the argument and is one of N, FASTN, LEASTN, PTR, MAX, where N corresponds to the width of the type.

Functions

Structures

Structures are a way of storing multiple pieces of data in one variable. For example, say we wanted to store the name and birthday of a person in strings, in one variable. We could use a structure to house that data:

struct birthday {
        char name[20];
        int day;
        int month;
        int year;
};

Pointer types

Variables can be declared as being pointers to values of various types, by means of the * type declarator. To declare a variable as a pointer, immediately precede its name with an asterisk.

char *square;
long *circle;

Arrays

An array is a collection of values, all of the same type, stored contiguously in memory.

For example:

int cat[10];


See also

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Data structure alignment — is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized …   Wikipedia

  • Data architecture — in enterprise architecture is the design of data for use in defining the target state and the subsequent planning needed to achieve the target state. It is usually one of several architecture domains that form the pillars of an enterprise… …   Wikipedia

  • Data Format Description Language — (DFDL, often pronounced daff o dil) is a modeling language from the Open Grid Forum for describing general text and binary data. A DFDL model or schema allows any text or binary data to be read (or parsed ) from its native format and to be… …   Wikipedia

  • Data General — Industry Computer Fate Acquired Successor EMC Corporation Founded 1968 …   Wikipedia

  • Data integration — involves combining data residing in different sources and providing users with a unified view of these data.[1] This process becomes significant in a variety of situations, which include both commercial (when two similar companies need to merge… …   Wikipedia

  • Data type — For other uses, see Data type (disambiguation). In computer programming, a data type is a classification identifying one of various types of data, such as floating point, integer, or Boolean, that determines the possible values for that type; the …   Wikipedia

  • Data mining — Not to be confused with analytics, information extraction, or data analysis. Data mining (the analysis step of the knowledge discovery in databases process,[1] or KDD), a relatively young and interdisciplinary field of computer science[2][3] is… …   Wikipedia

  • Data discrimination — Network Neutrality Related issues and topics Automatic telephone exchange Data discrimination End to end principle Internet Protocol Tiered Internet Bandwidth Throttling …   Wikipedia

  • Data structure — In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.[1][2] Different kinds of data structures are suited to different kinds of applications, and some are highly …   Wikipedia

  • Data file — A data file is a computer file which stores data to use by a computer application or system. It generally does not refer to files that contain instructions or code to be executed (typically called program files), or to files which define the… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”