Table of contents
1.
Syntax of Short int
2.
Size of Different int Data Types
3.
Short Data Type 
3.1.
Implementation
3.2.
C
3.3.
Output
4.
Int Data Type
4.1.
Implementation
4.2.
C
4.3.
Output 
5.
Short Int in C Programming
5.1.
Implementation
5.2.
C
5.3.
Output
6.
Frequently asked questions
6.1.
What range of values can be stored in a short int in c programming?
6.2.
How much memory does a short int occupy?
6.3.
When should I use a short int instead of an int?
6.4.
What are the potential problems of using short int in c programming?
6.5.
What is short and long int in C?
6.6.
How many numbers are in a short int?
7.
Conclusion
Last Updated: Dec 26, 2024
Easy

What is Short int in C programming?

Author Tashmit
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

short int in c programming

Syntax of Short int

In C programming, the syntax of declaring a variable with the short int data type is straightforward. Here's the syntax:

short int variable_name;

In this syntax, short int indicates the data type of the variable. variable_name is the name assigned to the variable, which follows the rules for naming identifiers in C programming.

Also, read - Bit stuffing program in c

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;
}
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

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 

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.

What is short and long int in C?

In C programming, short int and long int are data types used to declare integer variables with varying ranges of values. short int typically occupies less memory, while long int can represent larger integers.

How many numbers are in a short int?

A short int in C typically stores integers within the range of -32,768 to 32,767 on systems where the size of a short int is 2 bytes. This range may vary depending on the platform and compiler used.

Conclusion

In this article, we have discussed Short int in C programming. Exploring the nuances of short int in C programming reveals its significance as a fundamental data type. While compact in memory usage, short int serves as a versatile tool for managing integer values within a defined range. Understanding its characteristics, such as memory allocation and value range, empowers programmers to optimize their code for efficiency and portability. 

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

You can refer to these articles related to DSA.

Live masterclass