Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C Header Files

C Library – limits.h

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁️ 90 - Views
⏳ 4 mins
💬 1 Comment
C Library - limits.h

Photo Credit to CodeToFun

🙋 Introduction

The limits.h header in C defines the characteristics of various fundamental data types. It provides a set of macros that specify the limits of integer types such as char, int, long, and their unsigned counterparts. This information is crucial for writing portable code that can run on different platforms without encountering overflow or underflow issues.

💡 Syntax

To use the limits.h library, you need to include it in your program:

Syntax
Copied
Copy To Clipboard
#include <limits.h>

🤖 Macros Defined in limits.h

The limits.h header defines numerous macros, each corresponding to the limits of a specific data type. Below are some of the most commonly used macros:

Signed Integer Types

  1. CHAR_BIT: Number of bits in a char.

    example.c
    Copied
    Copy To Clipboard
    #define CHAR_BIT 8
  2. SCHAR_MIN: Minimum value for a signed char.

    example.c
    Copied
    Copy To Clipboard
    #define SCHAR_MIN -128
  3. SCHAR_MAX: Maximum value for a signed char.

    example.c
    Copied
    Copy To Clipboard
    #define SCHAR_MAX 127
  4. SHRT_MIN: Minimum value for a short int.

    example.c
    Copied
    Copy To Clipboard
    #define SHRT_MIN -32768
  5. SHRT_MAX: Maximum value for a short int.

    example.c
    Copied
    Copy To Clipboard
    #define SHRT_MAX 32767
  6. INT_MIN: Minimum value for an int.

    example.c
    Copied
    Copy To Clipboard
    #define INT_MIN -2147483648
  7. INT_MAX: Maximum value for an int.

    example.c
    Copied
    Copy To Clipboard
    #define INT_MAX 2147483647
  8. LONG_MIN: Minimum value for a long int.

    example.c
    Copied
    Copy To Clipboard
    #define LONG_MIN -9223372036854775808L
  9. LONG_MAX: Maximum value for a long int.

    example.c
    Copied
    Copy To Clipboard
    #define LONG_MAX 9223372036854775807L

Unsigned Integer Types

  1. UCHAR_MAX: Maximum value for an unsigned char.

    example.c
    Copied
    Copy To Clipboard
    #define UCHAR_MAX 255
  2. USHRT_MAX: Maximum value for an unsigned short int.

    example.c
    Copied
    Copy To Clipboard
    #define USHRT_MAX 65535
  3. UINT_MAX: Maximum value for an unsigned int.

    example.c
    Copied
    Copy To Clipboard
    #define UINT_MAX 4294967295U
  4. ULONG_MAX: Maximum value for an unsigned long int.

    example.c
    Copied
    Copy To Clipboard
    #define ULONG_MAX 18446744073709551615UL

📄 Example

Below is a simple example demonstrating how to use some of the macros defined in limits.h to print the range of various data types.

example.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <limits.h>

int main() {
    printf("Range of signed char: %d to %d\n", SCHAR_MIN, SCHAR_MAX);
    printf("Range of unsigned char: 0 to %u\n", UCHAR_MAX);
    printf("Range of short int: %d to %d\n", SHRT_MIN, SHRT_MAX);
    printf("Range of unsigned short int: 0 to %u\n", USHRT_MAX);
    printf("Range of int: %d to %d\n", INT_MIN, INT_MAX);
    printf("Range of unsigned int: 0 to %u\n", UINT_MAX);
    printf("Range of long int: %ld to %ld\n", LONG_MIN, LONG_MAX);
    printf("Range of unsigned long int: 0 to %lu\n", ULONG_MAX);

    return 0;
}

💻 Output

Output
Range of unsigned int: 0 to 4294967295
Range of long int: -9223372036854775808 to 9223372036854775807
Range of unsigned long int: 0 to 18446744073709551615

🧠 How the Program Works

  • Include limits.h: The program includes the limits.h header to access the macro definitions.
  • Print Ranges: It uses the printf function to display the minimum and maximum values for various signed and unsigned data types.

🚧 Common Pitfalls

  1. Platform Dependency: The actual values of the limits can vary between platforms. Relying on specific values without including limits.h can lead to non-portable code.
  2. Overflow and Underflow: Failing to account for the limits of data types can result in overflow or underflow, causing unexpected behavior or security vulnerabilities.
  3. Incorrect Macros: Using the wrong macros for the type being checked can lead to incorrect assumptions about the data type's range.

✅ Best Practices

  1. Use limits.h Macros: Always use the macros defined in limits.h to check the limits of data types instead of hardcoding values.
  2. Type Casting: Be mindful of type casting, especially when dealing with mixed data types, to avoid overflow or underflow.
  3. Documentation: Document any assumptions made about the range of data types to ensure that the code is understandable and maintainable.
  4. Validation: Validate inputs and outputs to ensure they fall within the expected ranges, preventing overflow or underflow.

🎉 Conclusion

The limits.h header is an essential tool for C programmers, providing crucial information about the limits of various fundamental data types.

By utilizing the macros defined in this header, you can write more portable and robust code. Being aware of common pitfalls and following best practices can help you avoid errors and ensure that your programs handle data types safely and efficiently.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy