Difference between short, short int and int data types in C programming

In this tutorial we will learn what is the difference between short, short int and int data types in c programming language?

short or short int

Both data types are same, short int can also be written as short; short occupies 2 bytes in the memory.

Here is the size and value range of short or short int

short or short int or signed short int 2 Bytes -32,768 to 32,767
unsigned short or unsigned short int 2 Bytes 0 to 65,535

Here is the proof

short, signed short or signed short int stores 15 bits of data, last bit represents sign

short size and range in c

unsigned short or unsigned short int stores 16 bits of data

unsigned short size and range

Consider this program:

int main()
{
    printf("size of short : %d\n",sizeof(short));
    printf("size of short int : %d\n",sizeof(short int));
    printf("size of signed short  : %d\n",sizeof(signed short));
    printf("size of signed short int  : %d\n",sizeof(signed short int));
    
    return 0;
}

Output

size of short : 2 
size of short int : 2 
size of signed short: 2 
size of signed short int: 2

int

Data type int (Integer) occupies 4 bytes in the memory.

Here is the size and value range of int

signed int or int 4 Bytes -2,147,483,648 to 2,147,483,647
unsigned int 4 Bytes 0 to 4,294,967,295

Here is the proof

signed int or int stores 31 bits of data, last bit represents sign

int data type size and range in c

unsigned int stores 32 bits of data

unsigned int data type size and range in c

Consider this program:

int main()
{

    printf("size of int : %d\n",sizeof(int));
    printf("size of signed int : %d\n",sizeof(signed int));
    printf("size of unsigned  : %d\n",sizeof(unsigned int));
    
    return 0;
}

Output

size of int : 4 
size of signed int : 4
size of unsigned: 4

Note: Size and data range may vary according to computer architecture, we are writing based on 32 bits computer architecture, compiler Linux GCC.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.