C Program to Demonstrate the long Keyword

C Program to Demonstrate the long Keyword

A district-level count exceeds the minimum guaranteed int range. Choose a type for the required value and use its matching format.

long is an integer type with a minimum range larger than the minimum int range, but its exact width remains implementation-dependent.

Before this exercise, review storing values and reading and checking input. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why use long deliberately uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Add a bounded count of one thousand new students.

Use long Deliberately: follow the data

  1. Given data

    A district-level count exceeds the minimum guaranteed int range. Choose a type for the required value and use its matching format.

  2. Rule

    long is an integer type with a minimum range larger than the minimum int range, but its exact width remains implementation-dependent.

  3. Observed result

    Students: 50000 Minimum range supported: yes

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

Understand the operation

50000L is a long integer constant and %ld expects long. The suffix communicates the chosen type; the format communicates the same type to printf.

long is not guaranteed to be wider than int on every implementation. Check limits rather than infer a universal byte count from the type name.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

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

Expected output

Students: 50000
Minimum range supported: 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) {
  long students = 50000L;
  printf("Students: %ld\n", students);
  printf("Minimum range supported: %s\n", LONG_MAX >= 2147483647L ? "yes" : "no");
  return 0;
}

The declared long stores 50000, which fits the minimum guaranteed long range.

The range comparison prints yes without requiring a particular sizeof(long).

Follow the worked calculation

  1. Start with the sample

    The declared long stores 50000, which fits the minimum guaranteed long range.

  2. Apply the rule

    The range comparison prints yes without requiring a particular sizeof(long).

  3. Check the result

    Students: 50000 Minimum range supported: yes

    Students: 50000
    Minimum range supported: 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) {
  long students = 50000L;
  printf("Students: %ld\n", students);
  printf("Minimum range supported: %s\n", LONG_MAX >= 2147483647L ? "yes" : "no");
  return 0;
}

Add a bounded count of one thousand new students.

Worked answer

Both operands are long and their sum 51000 fits the guaranteed minimum long range. The format remains %ld.

C17 · complete program

#include <stdio.h>
#include <limits.h>
int main(void) {
  long students = 50000L;
  long arriving = 1000L;
  printf("Total: %ld\n", students + arriving);
  return 0;
}

Expected output

Total: 51000
Run in C compiler →

Recognize the important boundaries

Signed long overflow is still undefined behavior; choosing a larger type is not a complete overflow-checking policy.

Use fixed-width or minimum-width integer types when your interface requires a stated width, while respecting their availability and formatting contracts.

Check your understanding

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

Does long always occupy eight bytes?
Choose one answer. You can try again.

Answer and explanation

No; its range has minimum requirements and its exact size varies.

Correct. A type name alone does not establish the host byte count.

Use the idea independently

long is an integer type with a minimum range larger than the minimum int range, but its exact width remains implementation-dependent.

Continue with testing the problem’s boundary cases.

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 Program to Add Two Numbers →