Exploring C Programming Data Types

Sep 8, 2024

Lecture on Data Types in C

Overview

This lecture discusses various data types in C programming, expanding upon topics previously covered, focusing on how they store data and how to display them using format specifiers.

Characters (Chars)

  • Single Character:
    • Stores one character.
    • Display using %c format specifier.
  • Array of Characters:
    • Stores strings or multiple characters.
    • Display using %s format specifier.

Floating Points

  • Float:
    • Stores decimal numbers.
    • Uses 4 bytes, 32 bits of precision.
    • Significant digits: 6-7.
    • Display using %f format specifier.
  • Double:
    • Double precision of a float.
    • Uses 8 bytes, 64 bits of precision.
    • Significant digits: 15-16.
    • Display using %lf or %.15lf for more precision.
    • More accurate than float but uses more memory.

Booleans

  • In C:
    • Include stdbool.h.
    • Stores true (1) or false (0).
    • Display using %d format specifier.

Chars as Integers

  • Range:
    • Stores integers between -128 to 127.
    • Display using %d for decimals or %c for characters using ASCII table.
  • Unsigned Char:
    • Range: 0 to 255.
    • Display using %d.
    • Overflow resets to 0.

Short Integers

  • Short Int:
    • Uses 2 bytes.
    • Range: -32,768 to 32,767.
    • Display using %d.
  • Unsigned Short Int:
    • Range: 0 to 65,535.
    • Overflow resets to minimum value.

Standard Integers

  • Integer:
    • Uses 4 bytes.
    • Range: ~-2 billion to 2 billion.
    • Display using %d.
  • Unsigned Integer:
    • Range: 0 to ~4 billion.
    • Display using %u.
    • Overflow causes reset.

Long Long Integers

  • Long Long Int:
    • Uses 8 bytes.
    • Range: ~-9 quintillion to 9 quintillion.
    • Display using %lld for signed, %llu for unsigned.
  • Unsigned Long Long Int:
    • Range: 0 to ~18 quintillion.
    • Warning for large constants can be avoided by appending u.

Key Points

  • Chars can store single characters or integers within a specified range.
  • Floats and doubles are used for decimal numbers; doubles offer more precision.
  • Booleans represent binary states.
  • Short/long integers extend or narrow the range of standard integers.

Focus

  • Primary focus on chars, arrays of chars, doubles, booleans, and integers.
  • Be aware of other types for specific use cases.