w3resource

C Data Types

Data Types

Each program needs a certain kind of data for displaying a meaningful result. This certain kind of data are known as a data type.

ANSI C supports four classes of data types :

  • Primary data types
  • User-defined data types
  • Derived data types
  • Empty data set

All C compilers support four fundamental data types

Type Range of values Description
char
(Characters)
-128 to 127 a single byte(8 bits) and can store one character type data
int
Integers (whole numbers)
-32768 to 32767 an integer type is used to represent whole numbers within a specified range of values.

float
Floating point (real numbers)

3.4e-38 to 3.4e+38 single-precision floating point

double
(Double)

1.7e-308 to 1.7e+308 double-precision floating point

Character Types :

A single character can be defined as a character type data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 and 255, signed chars have values from -128 to 127.

Integer Types :

C has three classes of integer storage, namely short int, int, and long int, in both signed and unsigned forms.

The keywords signed and unsigned are the two sign qualifiers which specify whether a variable can store positive or negative or both numbers.

The keyword signed uses one bit for a sign and 15 bits for the magnitude of the number in a 16-bit machine.

The keyword unsigned uses to store all the bits for the magnitude of the number and always positive.

Floating Point Types :

Floating point numbers are stored in 32 bits with 6 digits of precision. Floating point numbers are defined in C by the keyword float. When the accuracy provided by a float number is not sufficient, the type double can be used to define the number.

Double Point Types :

A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision numbers. Remember that double type represents the same data type that float represents but with a greater precision. To extend the precision further, we may use long double which uses 80 bits.

The following table shows the size and range of the type-specifiers on most common implementations :

Type Size(bits) Range
char or signed char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to 32767
unsigned int 16 0 to 65535
short int or signed short int 8 -128 to 127
unsigned short int 8 0 to 255
long int or signed long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
float 32 3.4E-38 TO 3.4E+38
double 64 1.7E-308 TO 1.7E+308
long double 80 3.4E-4932 TO 1.1E+4932

The sizes and ranges for each of C's data types in our system:

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
int main( void )
{
  printf( "Size of C data types:\n\n"                                 );
  printf( "Type               Bytes\n\n"                             );
  printf( "--------------------------------\n");
  printf( "char                 %lu\n" , sizeof( char )               );
  printf( "int8_t               %lu\n" , sizeof( int8_t )             );
  printf( "unsigned char        %lu\n" , sizeof( unsigned char )      );
  printf( "uint8_t              %lu\n" , sizeof( uint8_t )            );
  printf( "short                %lu\n" , sizeof( short )              );
  printf( "int16_t              %lu\n" , sizeof( int16_t )            );
  printf( "uint16t              %lu\n" , sizeof( uint16_t )           );
  printf( "int                  %lu\n" , sizeof( int )                );
  printf( "unsigned             %lu\n" , sizeof( unsigned )           );
  printf( "long                 %lu\n" , sizeof( long )               );
  printf( "unsigned long        %lu\n" , sizeof( unsigned long )      );
  printf( "int32_t              %lu\n" , sizeof( int32_t )            );
  printf( "uint32_t             %lu\n" , sizeof( uint32_t )           );
  printf( "long long            %lu\n" , sizeof( long long )          );
  printf( "int64_t              %lu\n" , sizeof( int64_t )            );
  printf( "unsigned long long   %lu\n" , sizeof( unsigned long long ) );
  printf( "uint64_t             %lu\n" , sizeof( uint64_t )           );
  printf( "float                %lu\n" , sizeof( float )              );
  printf( "double               %lu\n" , sizeof( double )             );
  printf( "long double          %lu\n" , sizeof( long double )        );
  printf( "_Bool                %lu\n" , sizeof( _Bool )              );
  printf( "\n" );
return 0;
}

Output:

Size of C data types:

Type               Bytes

--------------------------------
char                 1
int8_t               1
unsigned char        1
uint8_t              1
short                2
int16_t              2
uint16t              2
int                  4
unsigned             4
long                 8
unsigned long        8
int32_t              4
uint32_t             4
long long            8
int64_t              8
unsigned long long   8
uint64_t             8
float                4
double               8
long double          16
_Bool                1

The minimum and maximum values for each of C's data types in our system:

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <float.h>
int main( void )
{
  printf( "Ranges for integer data types in C\n\n" );
  printf( "------------------------------------------------------------\n");
  printf( "int8_t    %20d  %20d\n"     , SCHAR_MIN , SCHAR_MAX  );
  printf( "int16_t   %20d  %20d\n"     , SHRT_MIN  , SHRT_MAX   );
  printf( "int32_t   %20d  %20d\n"     , INT_MIN   , INT_MAX    );
  printf( "int64_t   %20lld  %20lld\n" , LLONG_MIN , LLONG_MAX  );
  printf( "uint8_t   %20d  %20d\n"     , 0         , UCHAR_MAX  );
  printf( "uint16_t  %20d  %20d\n"     , 0         , USHRT_MAX  );
  printf( "uint32_t  %20d  %20u\n"     , 0         , UINT_MAX   );
  printf( "uint64_t  %20d  %20llu\n"   , 0         , ULLONG_MAX );
  printf( "\n" );
  printf( "============================================================\n\n");
  printf( "Ranges for real number data types in C\n\n" );
  printf( "------------------------------------------------------------\n");
  printf( "flaot        %14.7g  %14.7g\n"   , FLT_MIN  , FLT_MAX  );
  printf( "double       %14.7g  %14.7g\n"   , DBL_MIN  , DBL_MAX  );
  printf( "long double  %14.7Lg  %14.7Lg\n" , LDBL_MIN , LDBL_MAX );
  printf( "\n" );
return 0;
}

Output:

Ranges for integer data types in C

------------------------------------------------------------
int8_t                    -128                   127
int16_t                 -32768                 32767
int32_t            -2147483648            2147483647
int64_t   -9223372036854775808   9223372036854775807
uint8_t                      0                   255
uint16_t                     0                 65535
uint32_t                     0            4294967295
uint64_t                     0  18446744073709551615

============================================================

Ranges for real number data types in C

------------------------------------------------------------
flaot          1.175494e-38    3.402823e+38
double        2.225074e-308   1.797693e+308
long double  3.362103e-4932  1.189731e+4932

Previous: C Variables and Constants
Next: C Operators



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What is the difference between exit(0) & exit(1) and exit(0) & exit(1) in C?

exit(0) indicates successful program termination & it is fully portable, While

exit(1) (usually) indicates unsuccessful termination. However, it's usage is non-portable.

Note that the C standard defines EXIT_SUCCESS and EXIT_FAILURE to return termination status from a C program.

0 and EXIT_SUCCESS are the values specified by the standard to indicate successful termination, however, only EXIT_FAILURE is the standard value for returning unsuccessful termination. 1 is used for the same in many implementations though.

Ref : https://bit.ly/3l4TAjG