Integer Size in C
Every variable in C has it’s corresponding data type. The type of data the variable can store, such as integer, character, floating point, double, etc., is specified. Each data type has a different set of operations that can be performed on it as well as different memory needs. A data collection with fixed values, meaning, and features is referred to as a data type.
- Additionally, the ranges that different data types may store numbers vary. These ranges could differ from one compiler to another.
- The size of an integer type in C programming is platform-dependent and might differ depending on the architecture and compiler being used.
- The minimal ranges that each integer type must be able to represent are provided by the C language, which does not specify the precise size of integer types.
Types | Description |
Primitive Data Types | The most fundamental data types, or primitive data types, are used to represent straightforward things like integers, floats, characters, etc. |
User Defined Data Type | The user himself defines the user-defined data types. |
Derived Data Types | The user himself defines the user-defined data types. |
Integers:
Integers are a primary data types in C that are used to represent entire numbers. Depending on the platform and compiler being used, integer data types can have different sizes.
- Different hardware designs, operating systems, and compiler implementations are blamed for this variety.
- To guarantee a specific range of representable values, the C standard establishes minimum size constraints for each numeric data type.
The most popular integer data types in C, together with the average size ranges on several contemporary systems, are listed below:
- Char:
A character or a tiny integer is represented by this data type. It will always have a minimum size of 8 bits (1 byte).
2.Short:
It also refers to a little integer and is referred to as a short integer. It usually has a minimum size of 16 bits (2 bytes).
3.Int:
Integers are represented using the data type int, which is the industry standard. It normally has a minimum size of 16 bits (2 bytes).
4.Long:
Stands for a bigger integer. It normally has a minimum size of 32 bits (4 bytes).
5.Long long:
Denotes an extremely large integer. It normally has a minimum size of 64 bits (8 bytes).
Data Type | Size(bytes) | Range | Format Specifier |
short int | 2 | -32,768 to 32,767 | %hd |
unsigned short int | 2 | 0 to 65,535 | %hu |
unsigned int | 4 | 0 to 4,294,967,295 | % u |
int | 4 | -2,147,483,648 to 2,147,483,647 | %d |
long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
long long int | 8 | -(2^63) to (2^63)-1 | %lld |
unsigned long int | 4 | 0 to 4,294,967,295 | %lu |
unsigned long, long int | 8 | 0 to 18,446,744,073,709,551,615 | %llu |
- Some simple data types can have their size or length changed by using the datatype modifiers long, short, signed, and unsigned.
You can use the sizeof operator, as shown in the following example, to ascertain the precise size of certain data types on a particular platform:
#include <stdio.h>
int main() {
printf("Size of char: %zu\n", sizeof(char));
printf("Size of short: %zu\n", sizeof(short));
printf("Size of int: %zu\n", sizeof(int));
printf("Size of long: %zu\n", sizeof(long));
printf("long long: %zu\n", sizeof(long long));
return 0;
}
Output:
![Integer Size in C](https://static.tutorialandexample.com/c/integer-size-in-c1.png)
- The size of each integer data type in bytes for your unique platform will be shown when you run this code.
- Writing code that depends on precise integer sizes requires caution, especially when moving code between platforms.
- Use the stdint.h header to get fixed-size integer types like int8_t, int16_t, int32_t, and int64_t if you need integers with fixed sizes independent of the platform.
Example:
// Program to represent Integer Data Type in C
#include <stdio.h>
int main()
{
// Integer value with positive data.
int a = 9;
// integer value with negative data.
int b = -9;
// For Unsigned int in C , U or u is used.
int c = 89U;
// For long int in C L or l is used.
long int d = 99998L;
printf("Int value with positive data: %d\n", a);
printf("Int value with negative data: %d\n", b);
printf("Int value with an unsigned int data: %u\n",
c);
printf("Integer value with long int data: %ld", d);
return 0;
}
Output:
![Integer Size in C](https://static.tutorialandexample.com/c/integer-size-in-c2.png)
Conclusion:
In conclusion, while the minimum size requirements for each integer data type are specified by the C standard, integer sizes in C might vary depending on the platform and compiler. Use fixed-size integer types from the "stdint.h" header to assure portability and particular size guarantees.