• Home
  • 1.3 Basic C Data Types

1.3 Basic C Data Types

View Categories

1.3 Basic C Data Types

2 min read

C provides several fundamental data types for representing characters, integers, and floating-point values. The size and range of a type can vary between implementations, so sizeof is used here to show the storage size of each type on the system running the program.

#include <stdio.h>

int main()
{
    char ch = 'A';
    short s = 100;
    int i = 1000;
    long l = 100000L;
    long long ll = 1000000000LL;
    float f = 3.14f;
    double d = 3.141592;
    long double ld = 3.141592L;
    _Bool flag = 1;

    printf("char        : %zu byte\n", sizeof(ch));
    printf("short       : %zu bytes\n", sizeof(s));
    printf("int         : %zu bytes\n", sizeof(i));
    printf("long        : %zu bytes\n", sizeof(l));
    printf("long long   : %zu bytes\n", sizeof(ll));
    printf("float       : %zu bytes\n", sizeof(f));
    printf("double      : %zu bytes\n", sizeof(d));
    printf("long double : %zu bytes\n", sizeof(ld));
    printf("_Bool       : %zu byte\n", sizeof(flag));

    return 0;
}

Example Output #

The exact sizes are implementation-dependent. A common result is:

char        : 1 byte
short       : 2 bytes
int         : 4 bytes
long        : 8 bytes
long long   : 8 bytes
float       : 4 bytes
double      : 8 bytes
long double : 16 bytes
_Bool       : 1 byte

Explanation #

The fundamental data types used in the example are:

Type Purpose
char Character and small integer values
short Small signed integer values
int Integer values
long Larger integer values
long long Large integer values
float Single-precision floating-point values
double Double-precision floating-point values
long double Extended-precision floating-point values
_Bool Boolean values, 0 or 1

The integer types can be modified with signed, unsigned, short, and long. The exact ranges depend on the implementation.

The suffixes used in the initializers specify the intended literal type:

100000L
1000000000LL
3.14f
3.141592L

sizeof returns the size of an object in bytes and has type size_t, which is why %zu is used with printf().

Minimum Size Requirements #

C specifies minimum ranges and relative size requirements rather than requiring the same size on every platform.

Type Minimum size requirement
char 1 byte
short 2 bytes
int 2 bytes
long 4 bytes
long long 8 bytes
float Implementation-defined
double Implementation-defined
long double Implementation-defined

A byte in C is the size of a char, and sizeof(char) is always 1. The number of bits in a byte is implementation-defined and can be obtained from CHAR_BIT in <limits.h>.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *