ProgramGuru

C Data Types, Ranges and sizeof

C Data Types, Ranges and sizeof

Student counts are whole numbers; temperatures may include fractions. Choosing the wrong type can remove information or make an arithmetic result unrepresentable.

A C type determines the values an object can represent and the operations that apply to it. Integer ranges and many type sizes depend on the implementation; sizeof measures sizes in C bytes.

  1. Explain

    Choose integers or floating point deliberately and inspect actual implementation limits.

  2. Trace

    Inspect whether long can represent at least 2147483647 while still printing a portable statement.

  3. Apply

    Recognize when a value cannot be represented by its chosen type.

Types and Ranges: follow the data

  1. Start

    Student counts are whole numbers; temperatures may include fractions. Choosing the wrong type can remove information or make an arithmetic result unrepresentable.

  2. Operation

    A C type determines the values an object can represent and the operations that apply to it. Integer ranges and many type sizes depend on the implementation; sizeof measures sizes in C bytes.

  3. Result

    char bytes: 1 int meets minimum range: yes

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

int is a signed integer type, while double represents approximate floating-point values. char occupies one C byte by definition, but CHAR_BIT gives the number of bits in that byte. Do not assume every type has the same size on every platform.

The headers limits.h and float.h describe implementation limits. sizeof returns size_t, printed with %zu. A C byte is the unit sizeof counts; this is different from claiming that int always occupies four bytes.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
#include <limits.h>
int main(void) {
  printf("char bytes: %zu\n", sizeof(char));
  printf("int meets minimum range: %s\n", INT_MAX >= 32767 ? "yes" : "no");
  return 0;
}

Expected output

char bytes: 1
int meets minimum range: yes
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
#include <limits.h>
int main(void) {
  printf("char bytes: %zu\n", sizeof(char));
  printf("int meets minimum range: %s\n", INT_MAX >= 32767 ? "yes" : "no");
  return 0;
}

sizeof(char) yields one C byte.

The standard minimum signed int maximum is at least 32767; the comparison is true without assuming the exact width.

Follow the worked calculation

  1. Read the starting values

    sizeof(char) yields one C byte.

  2. Perform the operation

    The standard minimum signed int maximum is at least 32767; the comparison is true without assuming the exact width.

  3. Check the observable result

    char bytes: 1 int meets minimum range: yes

    char bytes: 1
    int meets minimum range: yes
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
#include <limits.h>
int main(void) {
  printf("char bytes: %zu\n", sizeof(char));
  printf("int meets minimum range: %s\n", INT_MAX >= 32767 ? "yes" : "no");
  return 0;
}

Inspect whether long can represent at least 2147483647 while still printing a portable statement.

Worked answer

The L suffix gives the constant long type. Comparing with LONG_MAX expresses the minimum-range fact without printing a platform-specific size as a universal guarantee.

C17 · complete program

#include <stdio.h>
#include <limits.h>
int main(void) {
  printf("long meets minimum range: %s\n", LONG_MAX >= 2147483647L ? "yes" : "no");
  return 0;
}

Expected output

long meets minimum range: yes
Run in C compiler →

Recognize the important boundaries

Signed integer overflow is undefined behavior. Unsigned arithmetic wraps modulo one more than its maximum, but wrapping may still violate the application’s requirements.

Floating-point decimal fractions can be approximate. A printed two-decimal result does not mean that every intermediate value was exact.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What does sizeof(char) guarantee?
Choose one answer. You can try again.

Answer and explanation

One C byte.

Correct. The number of bits per byte is a separate property described by CHAR_BIT.

Use the idea independently

A C type determines the values an object can represent and the operations that apply to it. Integer ranges and many type sizes depend on the implementation; sizeof measures sizes in C bytes.

Continue with arithmetic and conversions.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Operators, Integer Division and Type Conversion →