Breaking News

C BASIC (DATA) TYPES


C (Basic) Data Types
-different data representations need different types in programming-






In C, data type categorized as:
  1. Primitive Types in ANSI C (C89)/ISO C (C90) - char, short, int, float and double.
  2.  Primitive Types added to ISO C (C99) - long long
  3.  User Defined Types – struct, union,  enum and typedef .
      4.  Derived Types – pointer, array and function pointer .



    Data Tyle Details :--

    Primitive Types added to ISO C (C99)

Type                                   Size in Bits   Comments                       Other Names
long long ≥ 64, ≥ size of long Can store integers in the range: -9223372036854775807 ~ 9223372036854775807 (264 / 2) portably. long long int, signed long long, signed long long int
unsigned long long Same as long long, but unsigned. Can store integers in the range: 0 ~ 18446744073709551615 (264) portably. unsigned long long int
short ≥ 16, ≥ size of char Can store integers in the range: -32767 ~ 32767 (216 / 2) portably.
Reduce memory usage though the resulting executable may be larger and probably slower as compared to using int.
short int,
signed short,
signed short int
unsigned short Same as short but unsigned Can store integers in the range: 0 ~ 65535 (216) portably.
Used to reduce memory usage though the resulting executable may be larger and probably slower as compared to using int.
signed,
signed int
int ≥ 16, ≥ size of short Basic signed integer type.
Represent a typical processor’s data size which is word-size
An integral data-type.
Can store integers in the range: -32767 ~ 32767 (216 / 2) portably.
signed,
signed int
unsigned int Same as int but unsigned. Can store integers in the range: 0 ~ 65535 (216) portably. unsigned
long ≥ 32, ≥ size of int long signed integer type.
Can store integers in the range: -2147483647 ~ 2147483647 (232 / 2) portably.
long int,
signed long,
signed long int
unsigned long Same as long but unsigned Can store integers in the range: 0 ~ 4294967295 (232) portably. unsigned long int
float ≥ size of char Used to reduce memory usage when the values used do not vary widely.
The format used is implementation defined and unnecessarily obeys the IEEE 754 single-precision format.
unsigned cannot be specified.
--
double ≥ size of float Typical floating-point data type used by processor.
The format used is implementation defined and unnecessarily obeys the IEEE 754 double-precision format.
unsigned cannot be specified.
--
long double ≥ size of double unsigned cannot be specified. --
long long ≥ 64, ≥ size of long Can store integers in the range: -9223372036854775807 ~ 9223372036854775807 (264 / 2) portably. long long int, signed long long, signed long long int
unsigned long long Same as long long, but unsigned. Can store integers in the range: 0 ~ 18446744073709551615 (264) portably. unsigned long long int
intmax_t Signed integer types capable of representing any
value of any signed integer type.
It is a typedef represents the signed integer type with largest possible range.
If you want an integer with the widest range possible on the platform on which it is being used.
--
uintmax_t Unsigned integer types capable of representing any
value of any unsigned integer type
It is a typedef represents the unsigned integer type with largest possible range.
If you want an integer with the widest range possible on the platform on which it is being used.
--
  • Unfortunately this is not supported by MSVC++ < 2012
  • inttypes.h vs. stdint.h: The C99 standard says that inttypes.h includes stdint.h, so there's no need to include stdint.h separately in a standard environment.
  • Some implementations have inttypes.h but not stdint.h.
  • VS/VC++ users may want to use msinttypes
  • Actual size of integer types varies by implementation: Windows, Linux, BSD etc. 
  • 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.
   long long > long > int > short
  •  int should be the integer type that the target processor is most efficient working with. For example, all types can be 64-bit.
  •  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.
long double > double > float 
  • The 32-bit and 64-bit IEEE 754 floating point formats should be used. 
  • The boolean (true/false) type is _Bool defined in stdbool.h
  • The stdbool.h type also defines a few useful identifiers as macros: bool is defined as _Bool, true as 1, false as 0.
  • Additionally, __bool_true_false_are_defined is defined as 1.
  • The _Bool type and stdbool.h header did not exist in pre-1999 versions of the standard. 
  •  Separate size_t and ptrdiff_t types to represent memory-related quantities.
  •  Existing types were inadequate, because their size is defined according to the target processor's arithmetic capabilities, not the memory capabilities, such as the address space availability.
  •  Both of these types are defined in the stddef.h header file (cstddef in C++).
  •  size_t is used to represent the maximum size of any object (including arrays) in the particular implementation.
  •  An unsigned integer type used to represent the sizes of objects
  • Used as the return type of the sizeof() operator.
    The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the stdint.h header file (cstdint in C++).
  •  It is guaranteed to be at least 65535.
  •  ptrdiff_t is used to represent the difference between pointers.
  • Is the signed integer type of the result of subtracting two pointers.
  • The type's size is chosen so that it could store the maximum size of a theoretically possible array of any type.
  • On a 32-bit system ptrdiff_t will take 32 bits and on a 64-bit one - 64 bits and it is portable. 
  •  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 Min value Max value Type Min value Max value
Exact width intN_t INTN_MIN INTN_MAX uintN_t 0 UINTN_MAX
Least width int_leastN_t INT_LEASTN_MIN INT_LEASTN_MAX uint_leastN_t 0 UINT_LEASTN_MAX
Fastest int_fastN_t INT_FASTN_MIN INT_FASTN_MAX uint_fastN_t 0 UINT_FASTN_MAX
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
   

No comments