'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Dec 19, 2023
Easy

What is Short Int in C Programming?

Author Tashmit
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

C is a high-level language applied in system and application programming. C includes simple syntax and small set of keywords. This provides a low-level access to both memory and hardware. It is a compiled language. This means that the code is compiled into machine language before it is executed. This further results in fast and efficient code.

short int in c programming

The C language contains various data-types such as char, int, float, long, short, short int, double, and so on. In this article, we will focus on learning about what is Short Int in C Programming?

What is Short Int in C Programming?

Short int is a data type in the C programming language and it occupies 2 bytes of memory space. This means that a short int can store integers in the range of -32,768 to 32,767. Short int is a smaller data type than int, which occupies 4 bytes of memory and can store integers in the range of -2,147,483,648 to 2,147,483,647. 

A short int is used in cases where we want to save memory. For example, instead of using int, we can use short int to save space. The point to notice is that a short int can only store integers in a smaller range than an int.

Also read - Bit stuffing program in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Size of Different int Data Types

In C programming, the size of integer data types can change based on the system and compiler you're using. Below is the list of typically used data types and their size essential in C programming.

TypeSize
char1 byte
short2 bytes
int2 or 4 bytes
unsigned char1 byte
unsigned short int2 bytes
unsigned int2 or 4 bytes
unsigned long8 bytes
float4 bytes
double8 bytes
long double10 bytes

Different computers and software may have different sizes for data types. If you want to know the specific size of a data type in your C program, you can use the "sizeof" operator.

Short Data Type 

A short data type is stored within 2 bytes (16 bits) of memory. It is designed to represent smaller values than any other data type, which is represented using 4-16 bytes (32-128 bits) of memory.

In order to declare a short int variable in C, you can use the "short" keyword followed by the variable name, for example:

short ninjaVar;


When we want to store values that have a lesser value like age, or dimensions of a ground, etc then short data types can be used in order to save some memory. The range of values that are stored by a "short" data type is implementation-dependent, but it is guaranteed to be at least -32767 to 32767 in two's complement representation. 
 

To store negative values using two’s complement, we simple follow 3 steps:

  1. Convert number into binary form.
     
  2. Invert the bit values.
     
  3. Add 1 to inverted bit values.
     

We can also calculate the amount of storage a variable is using by using a function called sizeof().

Implementation

  • C

C

#include <stdio.h>

int main() {
short ninjaVar = -89;
printf("Size of short variable is: %lu", sizeof(ninjaVar));
return 0;
}

Output

Output

Int Data Type

The Data Type "int" is a fundamental data type that is used to represent integers. It stands for "integer" and can store both positive and negative whole numbers.

To declare an "int" variable in C, you simply use the "int" keyword followed by the variable name, for example:

int ninjaVar;


We can also initialise an integer value while declaring it like:

Implementation

  • C

C

#include <stdio.h>

int main() {
int ninjaVar = 16;
printf("The int variable ninjaVar has value: %d", ninjaVar);
return 0;
}


Output 

Output

In most implementations of C, "int" is typically represented using 4 bytes (32 bits) of memory. This allows it to store a wide range of values, with a minimum guaranteed range of -32767 to 32767 in two's complement representation.

Short Int in C Programming

In C programming language, a short int is a data type representing an integer number with a smaller range of values than a regular int. As the name suggests, it is a combination of two data types that are short and int. A short int is used to store a small number in memory. The memory consumed by short int to hold numeric value depends on the hardware and operating system. As a short int has features of both short and int data types, it can store positive or negative values. 

You can declare the short int variable as:

short int ninjaVar;


We can declare the value of short int variable like:

Implementation

  • C

C

#include <stdio.h>

int main() {
short int ninjaVar = 12974;
printf("The short int variable ninjaVar has value: %d",ninjaVar);
return 0;
}

Output

Output

In the short int data type, short is the qualifier, and int is the basic data type. The values range of short int is −32,767 to +32,767.

Also  see, Tribonacci Series and Floyd's Triangle in C

Frequently asked questions

What range of values can be stored in a short int in c programming?

The range of values that can be stored in a short int varies depending on the implementation, but it is guaranteed to be in the range of -32767 to 32767 in two's complement representation.

How much memory does a short int occupy?

A short int typically occupies 2 bytes (16 bits) in most systems.

When should I use a short int instead of an int?

It would be best to use a short int instead of an int when you need to store small integers to save memory or when you know that the value of the integer will not exceed a specific limit.

What are the potential problems of using short int in c programming?

Using short int can cause overflow or underflow problems if the value exceeds the allowed range. Additionally, operations involving short int values may require typecasting or conversion to int, which can affect performance.

Conclusion

We learned the following in this article:

  • What is short int in C programming. 
  • Size of different int Data Types.
  • Learned about short and int data types. 
     

We hope this article helps you on your journey. You can solve some problems, like:

You can refer to these articles related to DSA by Coding Ninjas.

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head to our practice platform, Coding Ninjas Studio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Happy coding, Ninja!

Previous article
Bit Stuffing Program in C
Next article
Self Referential Structure in C
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass