Back to C Programming
2026-02-025 min read

12.2 Integer Constant Data Types

Learn 12.2 Integer Constant Data Types step by step with clear examples and exercises.

Title: Mastering Integer Constant Data Types in C Programming - Exam and Interview Ready

Why This Matters

In this comprehensive lesson, we delve into the world of integer constant data types in C programming. Understanding these concepts is crucial for writing efficient and error-free code, as well as avoiding common bugs that beginners often encounter. Mastering integer constants can help you create reliable and robust programs. By the end of this tutorial, you will have a solid understanding of integer constant data types, their usage, and potential pitfalls to avoid — making you better prepared for exams, interviews, and real-world programming scenarios.

Prerequisites

Before diving into the core concept, it's important to ensure you have a solid understanding of the following:

  1. Basics of C programming syntax
  2. Variables and their declaration
  3. Basic arithmetic operations in C
  4. Understanding data types (int, char, float, etc.)
  5. Familiarity with control structures such as loops and conditional statements
  6. Knowledge of functions and function prototypes
  7. Understanding pointers and memory management
  8. Awareness of bitwise operators and their usage in C

Core Concept

Definition of Integer Constant Data Types

In C programming, an integer is a whole number without a fractional part. The integer constant data types include int, char (which can be used as integers in some contexts), and other specific integer types like short, long, and long long.

Let's explore each of these data types:

int

The most common integer type is int. It represents a 32-bit signed integer on most systems, which can store numbers between -2147483648 and 2147483647.

char

Though primarily used for storing individual characters, a char data type can also be used to represent small integer values within the range of -128 to 127 on most systems. When a char variable is assigned a value outside this range or an ASCII character with a code greater than 127, it will be treated as an unsigned char and may result in unexpected behavior.

short, long, and long long

The short, long, and long long data types provide options for storing integers of different sizes. The exact size depends on the system, but generally:

  • short is a 16-bit signed integer, which can store numbers between -32768 and 32767.
  • long is a 32-bit signed integer, which can store numbers between -2147483648 and 2147483647 (the same range as an int on most systems).
  • long long is a 64-bit signed integer, which can store numbers between -9223372036854775808 and 9223372036854775807.

Declaring Integer Variables

To declare an integer variable in C, use the following syntax:

int variable_name;

You can also initialize a variable at the time of declaration by assigning it a value:

int variable_name = value;

Integer Constants

Integer constants are values that are assigned to variables or used directly in expressions. They can be written in decimal, octal (prefix with 0), or hexadecimal (prefix with 0x) notation.

Example:

int a = 123; // Decimal notation
int b = 0145; // Octal notation
int c = 0x7B; // Hexadecimal notation

Worked Example

In this worked example, we'll create a simple program that demonstrates the usage of integer constant data types and their ranges.

#include <stdio.h>

int main() {
int i = -2147483648; // Minimum value for an int
unsigned int u = 4294967295; // Maximum value for an unsigned int
short s = -32768; // Minimum value for a short
long l = 2147483647; // Maximum value for a long (same as int on most systems)
long long ll = 9223372036854775807; // Maximum value for a long long

printf("Minimum value for an int: %d\n", i);
printf("Maximum value for an unsigned int: %u\n", u);
printf("Minimum value for a short: %hd\n", s);
printf("Maximum value for a long (same as int on most systems): %ld\n", l);
printf("Maximum value for a long long: %lld\n", ll);

return 0;
}

Common Mistakes

  1. Forgetting to include the header file ` for printf()` function.
  2. Assigning an integer value outside the range of a specific data type (e.g., assigning 4294967296 to an int variable).
  3. Incorrectly using char variables for large integer values or vice versa.
  4. Mixing decimal, octal, and hexadecimal notation in the same expression without proper understanding of their values.
  5. Performing arithmetic operations that result in overflow or underflow, causing unexpected behavior.
  6. Failing to handle overflow or underflow by using appropriate data types or implementing checks.
  7. Not being aware of the difference between signed and unsigned integer variables.
  8. Forgetting to cast a value to an appropriate data type when necessary (e.g., casting a large decimal value to a long long).
  9. Using variable-length arrays without proper understanding of their memory allocation and potential issues.
  10. Not properly understanding the behavior of integer division and modulo operations in C.
  11. Misusing bitwise operators, leading to unexpected results or logic errors.
  12. Failing to consider endianness when dealing with multi-byte integers across different systems.

Practice Questions

  1. What is the minimum value that can be stored in a short integer on most systems?
  2. What is the maximum value that can be stored in an unsigned char on most systems?
  3. Write a program that calculates the sum of two integers using int, long, and long long data types and demonstrates overflow behavior.
  4. Explain the difference between signed and unsigned integer variables in C.
  5. How can you determine the size of an integer data type on your system?

FAQ

What is the difference between signed and unsigned integer types?

  • Signed integers can store both positive and negative numbers, while unsigned integers only store non-negative values.

Why do some systems have a different range for char than others?

  • The range of a char depends on the system's character encoding. For example, on ASCII systems, a char can represent values from 0 to 127, while on EBCDIC systems, it can represent values from 0 to 255.

How do I determine the size of an integer data type on my system?

  • You can use the sizeof operator in C to get the size of a specific data type in bytes:
printf("Size of int: %zu\n", sizeof(int));

What are some common pitfalls when working with integer constants in C?

  • Common pitfalls include overflow and underflow errors, incorrect usage of signed and unsigned integers, and mixing decimal, octal, and hexadecimal notation without proper understanding.

How can I handle overflow or underflow issues in my code?

  • You can use appropriate data types for your variables, implement checks to ensure values stay within the valid range, or use libraries that provide arithmetic operations with overflow handling.