Home » C#.Net

Difference between int, Int16, Int32 and Int64 in C#

C# differences between int, Int16, Int32 and Int64: Here, we are going to learn what are the differences between int, Int16, Int32 and Int64 in C#?
Submitted by IncludeHelp, on February 13, 2019

int, Int16, Int32 and Int64 are used to represent signed integers with values ranging based on their capacities/occupied size in the memory. These types are able to work with negative and positive values. All these types are the same in nature but different based on the value ranges.

Difference between int, Int16, Int32 and Int64

1) Int16

  • Int16 represents 16-bits (2-bytes) signed integer.
  • Int16 occupies 16-bits (2-bytes) space in the memory.
  • As per the 2-bytes data capacity, an Int16's value capacity is -32768 to +32767.

Example:

Consider the code – Here, we are printing required size, type, minimum & maximum value, variable declaration, and assignment of an Int16.

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //printing Int16 capacity, type, MIN & MAX value
            Console.WriteLine("Int16 occupies {0} bytes", sizeof(Int16));
            Console.WriteLine("Int16 type is: {0}", typeof(Int16));
            Console.WriteLine("Int16 MIN value: {0}", Int16.MinValue);
            Console.WriteLine("Int16 MAX value: {0}", Int16.MaxValue);
            Console.WriteLine();

            //Int16 variables
            Int16 a = 12345;
            Int16 b = -12345;
            Console.WriteLine("a = {0}, b = {1}", a, b);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

Int16 occupies 2 bytes
Int16 type is: System.Int16
Int16 MIN value: -32768
Int16 MAX value: 32767

a = 12345, b = -12345
Advertisement

2) Int32/int

int is an alias of Int32, so, int and Int32 are the same type.

  • Int32 represents 32-bits (4-bytes) signed integer.
  • Int32 occupies 32-bits (4-bytes) space in the memory.
  • As per the 4-bytes data capacity, an Int32's value capacity is -2147483648 to +2147483647.

Example:

Consider the code – Here, we are printing required size, type, minimum & maximum value, variable declaration, and assignment of an Int32 or int.

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //printing Int32 capacity, type, MIN & MAX value
            Console.WriteLine("Int32 occupies {0} bytes", sizeof(Int32));
            Console.WriteLine("Int32 type is: {0}", typeof(Int32));
            Console.WriteLine("Int32 MIN value: {0}", Int32.MinValue);
            Console.WriteLine("Int32 MAX value: {0}", Int32.MaxValue);
            Console.WriteLine();

            //Int32 variables
            Int32 a = 289812345;
            Int32 b = -290012345;
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine();

            //printing int capacity, type, MIN & MAX value
            Console.WriteLine("int occupies {0} bytes", sizeof(int));
            Console.WriteLine("int type is: {0}", typeof(int));
            Console.WriteLine("int MIN value: {0}", int.MinValue);
            Console.WriteLine("int MAX value: {0}", int.MaxValue);
            Console.WriteLine();

            //int variables
            int x = 289812345;
            int y = -290012345;
            Console.WriteLine("x = {0}, y = {1}", x, y);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

Int32 occupies 4 bytes
Int32 type is: System.Int32
Int32 MIN value: -2147483648
Int32 MAX value: 2147483647

a = 289812345, b = -290012345

int occupies 4 bytes
int type is: System.Int32
int MIN value: -2147483648
int MAX value: 2147483647

x = 289812345, y = -290012345
Advertisement

3) Int64

  • Int64 represents 64-bits (8-bytes) signed integer.
  • Int64 occupies 64-bits (8-bytes) space in the memory.
  • As per the 8-bytes data capacity, an Int64's value capacity is -9223372036854775808 to +9223372036854775807.

Example:

Consider the code – Here, we are printing required size, type, minimum & maximum value, variable declaration, and assignment of an Int64.

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //printing Int64 capacity, type, MIN & MAX value
            Console.WriteLine("Int64 occupies {0} bytes", sizeof(Int64));
            Console.WriteLine("Int64 type is: {0}", typeof(Int64));
            Console.WriteLine("Int64 MIN value: {0}", Int64.MinValue);
            Console.WriteLine("Int64 MAX value: {0}", Int64.MaxValue);
            Console.WriteLine();

            //Int64 variables
            Int64 a = 289818278722345;
            Int64 b = -290287827012345;
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine();

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

Int64 occupies 8 bytes
Int64 type is: System.Int64
Int64 MIN value: -9223372036854775808
Int64 MAX value: 9223372036854775807

a = 289818278722345, b = -290287827012345
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.