Types in C

In C, types are categorized into several basic categories: integer types, floating-point types, and other types such as void and pointer types. Here's an overview of the types in C, along with sample code snippets and the typical range of values they can represent. Note that the exact range of values can vary by implementation, as the C standard only provides minimum sizes for each data type.

Integer Types:

  1. char

    • Used to store characters.

    • Range: Typically -128 to 127 for signed char, and 0 to 255 for unsigned char.

    • Size: At least 8 bits.

char a = 'A';
unsigned char b = 255;
  1. int

    • The most natural size of integer for the machine.

    • Range: Typically -32,768 to 32,767 for signed int, and 0 to 65,535 for unsigned int.

    • Size: At least 16 bits.

int x = 123;
unsigned int y = 456U;
  1. short

    • Short integer.

    • Range: Typically -32,768 to 32,767 for signed short, and 0 to 65,535 for unsigned short.

    • Size: At least 16 bits.

short s = 1000;
unsigned short t = 2000U;
  1. long

    • Long integer.

    • Range: Typically -2,147,483,648 to 2,147,483,647 for signed long, and 0 to 4,294,967,295 for unsigned long.

    • Size: At least 32 bits.

long l = 100000L;
unsigned long m = 100000UL;
  1. long long

    • Longer integer type.

    • Range: Typically -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 for signed long long, and 0 to 18,446,744,073,709,551,615 for unsigned long long.

    • Size: At least 64 bits.

long long ll = 123456789LL;
unsigned long long ull = 123456789ULL;

Floating-Point Types:

  1. float

    • Single-precision floating-point.

    • Range: Typically 1.2E-38 to 3.4E+38 (6 decimal places).

float f = 123.456f;
  1. double

    • Double-precision floating-point.

    • Range: Typically 2.3E-308 to 1.7E+308 (15 decimal places).

double d = 123.456;
  1. long double

    • Extended precision floating-point.

    • Range and precision can vary, but it is at least as precise as double.

long double ld = 123.456L;

Other Types:

  1. void

    • Type with no values.

    • Often used to specify the return type of functions that do not return a value.

void myFunction() {
    // Do something
}
  1. Pointers

    • A variable that stores the address of another variable.

    • The range depends on the architecture (32-bit or 64-bit).

int z = 10;
int *p = &z;

Sample Code Using Various Types:

#include <stdio.h>

int main() {
    char c = 'A';
    int i = 1;
    short sh = 2;
    long l = 1234567890L;
    long long ll = 1234567890123456789LL;
    float f = 123.456f;
    double d = 123.456789;
    long double ld = 123.45678910111213L;

    printf("char: %c\n", c);
    printf("int: %d\n", i);
    printf("short: %hd\n", sh);
    printf("long: %ld\n", l);
    printf("long long: %lld\n", ll);
    printf("float: %f\n", f);
    printf("double: %lf\n", d);
    printf("long double: %Lf\n", ld);

    return 0;
}

Remember that the actual size and range of these types can vary depending on the system and compiler implementation. To determine the exact size and range for your system, you can use the limits.h and float.h header files which define constants for these properties.