DEV Community

San Kang
San Kang

Posted on

[Adult Learning Log] C Language - Week 3 Review

○ Key Takeaways from Week 3

  • Learned how to use sizeof to find out the memory size occupied by a variable or data type.
  • Understood the concepts of variables and constants, and the classification, size, and range of data types.
  • Learned about unsigned, signed type specifiers and how to use suffixes to specify data types for integer constants.
  • Studied symbolic constants and the differences between constants and variables.
  • Learned the concept of Underflow, which is the opposite of Overflow.

○ Review Highlights

  • Reviewed the concept of Overflow and the importance of selecting appropriate data types.
  • Revisited how negative numbers are represented in computers using Two's Complement.
  • Reviewed fixed-point vs. floating-point number representations.
  • Refreshed understanding of ASCII codes.

○ Data Types and Sizes: (1) Integer Types

  • short : 16-bit (2 bytes) → Range: -2^15 ~ 2^15 - 1
  • int : 32-bit (4 bytes) → Range: -2^31 ~ 2^31 - 1
  • long : 32-bit (4 bytes) → Range: -2^31 ~ 2^31 - 1
  • long long : 64-bit (8 bytes) → Range: -2^63 ~ 2^63 - 1

Why are there so many integer types in C?

  • Allows programmers to choose appropriate types based on use-case.
  • Increasing the bit size extends the range but consumes more memory.

○ Signed vs Unsigned

  • unsigned: Represents only non-negative values. No sign bit required.

    → For 32 bits: 0 ~ 2^32 - 1

  • signed: Includes both positive and negative values (usually default as int).

    → With 1 bit for sign, 31 bits remain for value: -2^31 ~ 2^31 - 1

○ Integer Constant Suffixes

  • u or U → unsigned (e.g., 123u)
  • l or L → long (e.g., 123L)
  • ul or UL → unsigned long (e.g., 123UL)

○ Overflow

  • Occurs when data exceeds the size limits of its data type.
  • Can lead to incorrect results or system behavior.
  • Happens when the sum requires more bits than available.
  • If the sign bit in the result differs from operands, overflow occurs.
  • Always choose data types with sufficient size to prevent overflow.

○ Symbolic Constants

  • Constants expressed using symbols (e.g., #define) for readability and maintainability.
  • Changing a value in one place instead of multiple occurrences makes code maintenance easier.
#define EXCHANGE_RATE 1120    // preferred
// #define EXCHANGE_RATE = 1120; → incorrect
Enter fullscreen mode Exit fullscreen mode

Symbolic Constant vs Variable

  • #define is a preprocessor directive and is replaced before compilation.
  • It does not occupy memory or have a data type.
  • Variables are stored in memory and can be changed at runtime.
  • Think of symbolic constants as constant variables that don’t exist in memory.

○ Data Types and Sizes: (2) Floating Point Types

  • float: 32-bit (4 bytes) → Single precision, ~7 decimal digits
  • double: 64-bit (8 bytes) → Double precision, ~15~16 decimal digits
  • long double: 64-bit+ → Extended precision (compiler-dependent)

Fixed vs Floating Point

  • Fixed-point: Simple but limited for large numbers
  • Floating-point: Used in science/engineering for representing very large/small numbers

Floating-point Overflow & Underflow

  • Overflow: Too large to represent → often shows inf
  • Underflow: Too small to represent

Why decimal for float range but binary for int?

  • Integers stored in binary → binary ranges (2^n) are precise and meaningful.
  • Floats care about how many decimal digits can be represented accurately, so decimal notation is more informative.

○ Data Types and Sizes: (3) Character Type

  • char type stores characters using ASCII codes.

Control Characters

  • Non-printable characters used for control:
    • \0 → null terminator (marks end of string)
    • \a → bell (beep sound)
    • \b → backspace
    • \t → horizontal tab
    • \n → newline
    • \v → vertical tab
    • \f → form feed
    • \r → carriage return
    • \", \', \\ → literal quote or backslash

\0 Null Terminator

  • Marks end of a string in memory.
  • Strings in C are arrays of characters; they don't store length separately.
  • So \0 acts like a period to signify end of the string.

○ Practice Activities

Practice 1: sizeof

  • Use sizeof to display memory size for various data types and variables.

Image description

Practice 2: Currency Converter

  • Input USD amount → output converted KRW amount.
  • Use Symbolic Constant for exchange rate.

Image description

Practice 3: ASCII & Char Practice

  • Print numeric and character values using ASCII codes.

Image description

Top comments (0)