Sizeof and Limits of Data Type in C

Sizeof and Limits of Data Type in C Language

In this article, I will discuss the Sizeof and Limits of Data Types in C Language with Examples. In our last article, we discussed Character Data Type in C Language briefly. In this article, we will discuss two more concepts related to data type. The first one is the sizeof method, or we can also call it sizeof operator, and the second one is the limits of each data type.

Sizeof() Function in C Language

In C programming, the sizeof operator is used to determine the size of a data type or a variable in bytes. The size of data types can vary depending on the architecture and compiler used. Here’s a general guide to the sizes and limits of common data types in C:

char:
  • Size: Typically 1 byte.
  • Range: -128 to 127 for signed, 0 to 255 for unsigned.
int:
  • Size: Typically 4 bytes (can vary, sometimes 2 bytes).
  • Range: -2,147,483,648 to 2,147,483,647 for signed, 0 to 4,294,967,295 for unsigned.
short int:
  • Size: Typically 2 bytes.
  • Range: -32,768 to 32,767 for signed, 0 to 65,535 for unsigned.
long int:
  • Size: Typically 4 bytes (8 bytes on some 64-bit systems).
  • Range: -2,147,483,648 to 2,147,483,647 for signed, 0 to 4,294,967,295 for unsigned.
long long int:
  • Size: Typically 8 bytes.
    Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 for signed, 0 to 18,446,744,073,709,551,615 for unsigned.
float:
  • Size: Typically 4 bytes.
  • Precision: Approximately 7 decimal digits.
double:
  • Size: Typically 8 bytes.
  • Precision: Approximately 15 decimal digits.
long double:
  • Size: Typically 12, 16, or 16+ bytes (varies).
  • Precision: Varies, but usually more than double.
pointer types:
  • Size: Depends on the architecture. 4 bytes on 32-bit systems, 8 bytes on 64-bit systems.

To get the exact size of these data types on your specific system and compiler, you can use the sizeof operator or function in C. The sizeof function is a predefined function just like the printf and scanf() functions.

#include <stdio.h>

int main() {
    printf("Size of char: %lu byte\n", sizeof(char));
    printf("Size of int: %lu bytes\n", sizeof(int));
    printf("Size of short int: %lu bytes\n", sizeof(short int));
    printf("Size of long int: %lu bytes\n", sizeof(long int));
    printf("Size of float: %lu bytes\n", sizeof(float));
    printf("Size of double: %lu bytes\n", sizeof(double));
    printf("Size of long double: %lu bytes\n", sizeof(long double));
    printf("Size of pointer: %lu bytes\n", sizeof(void*));
    return 0;
}

This program will output the size of each data type in bytes on your system. Keep in mind that the actual sizes and limits might differ based on your compiler and the architecture of the machine you’re using. 

What is the use of sizeof function in C?

The sizeof function in C Language returns the size of different things. So, what are the different things? The sizeof function returns the size of the following four things.

  1. Size of a variable.
  2. Size of a data type
  3. Size of an expression
  4. Size of a pointer

So, we can pass either variable, data type, expression, or pointer as an argument to the sizeof function. The sizeof function is a predefined function that will return the size of different types of things. So, if you want to find out the size of any data type, we can go for a sizeof function. For a better understanding, please have a look at the following diagram.

Sizeof and Limits of Data Type in C Language

Predefined sizeof function example in c language:

Please have a look at the following example, which uses the predefined sizeof function.

#include<stdio.h>
#include<conio.h>
int main()
{
    char C;
    short S;
    printf("size of char : %d bytes(s)\n", sizeof(C));
    printf("size of short : %d bytes(s)\n", sizeof(S));
    printf("size of float : %d bytes(s)\n", sizeof(float));
    return 0;
}

In the above program, first, I am declaring two variables. One is of a type character, i.e. C, and the second one is of type short i.e. s. Then I print the size of these two variables using the sizeof function.

What is the size? The size representation is always in the form of integers. So, always use the %d format specifier to show the size. Here, we are passing the variable names to the sizeof function. When we pass the variable name to the sizeof function, it will return the character’s size. The size of the character is one byte. so it will print that value one.

Next, it will send the control to the next line. In the next line, we are printing the size of the short, and it is also %d because size is always in integers. And we know the short size is 2 bytes, so it will print 2 and send the control to the next line.

Whenever we are the calling sizeof function, we can pass either variable name, data type name, expression, or pointer. In the next line, we are passing the data type float to the sizeof function. The size of the float is 4 bytes. So here, it will print the value as 4 bytes.

So, when you run the above program, you will get the following output.

Predefined sizeof function example in c language

In our upcoming articles, we discuss how to pass expression and pointer to the sizeof function in C Language.

Limits of Data Type in C Language

Now we will understand the limits of a data type. What are the limits of a data type means we have one header file i.e. limits.h. The Limits.h header file contains n number of predefined variables and all these predefined variables are global variables. Global variable means we can access these variables from anywhere in any c application. These variables are also called constant variables. A constant variable means we cannot modify the values of these variables. We cannot modify limits.h header file. For a better understanding, please have a look at the following diagram.

Limits of data type in C Language

Now we will see some programs on how to use a sizeof function, and then we will see how to work with the limits.h variables.

Example to understand limits of data type in C Language:

Now, we will see how to print the limits of each data type. The Limits.h header file contain so many predefined constant variables i.e global variables. The limits.h header contains many predefined variables as shown in the below image,

Example to understand limits of data type in C Language

In any programming language, if you want to represent a constant variable. Then mostly, we are using capital letters only. All these variables belong to the signed type and unsigned type. The minimum range of every unsigned datatype starts with a zero. That is why they didn’t provide that info. All these variables are available in a limits.h, which are global variables. you can access anywhere in a C application, which are constants.

Example:

Please have a look at the below program.

#include<stdio.h>
#include<limits.h>
int main()
{
    printf("Signed short MIN Value %d\n", SHRT_MIN);
    printf("Signed short Max Value %d\n", SHRT_MAX);
    printf("Unsigned short Max Value %d\n", USHRT_MAX);
    printf("Signed char MIN Value %d\n", SCHAR_MIN);
    printf("Signed char Max Value %d\n", SCHAR_MAX);
    printf("Unsigned char Max Value %d\n", UCHAR_MAX);
    return 0;
}

In the above program, we include the limits.h header file. This is because we are using some of the variables related to limits.h header file. Or else you will get an error message. When you run the above program, you will get the following output.

Sizeof and Limits of Data Type in C Language

In the next article, I will discuss Type Casting in C Language with Examples. Here, in this article, I try to explain the size of and Limits of Data Types in C Language with Examples, and I hope you enjoy this Sizeof and Limits of Data Types in the C language article.

Leave a Reply

Your email address will not be published. Required fields are marked *