Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Short Int in C Programming?
2.1.
Syntax of Short int
3.
Size of Different int Data Types
4.
Short Data Type 
4.1.
Implementation
4.2.
C
4.3.
Output
5.
Int Data Type
5.1.
Implementation
5.2.
C
5.3.
Output 
6.
Short Int in C Programming
6.1.
Implementation
6.2.
C
6.3.
Output
7.
Frequently asked questions
7.1.
What range of values can be stored in a short int in c programming?
7.2.
How much memory does a short int occupy?
7.3.
When should I use a short int instead of an int?
7.4.
What are the potential problems of using short int in c programming?
7.5.
What is short and long int in C?
7.6.
How many numbers are in a short int?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

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

In C programming, data types play a pivotal role in defining the nature and scope of variables within a program. Among these fundamental data types lies the 'short int', a compact yet powerful entity that holds a unique position in the language's hierarchy. Understanding what a short int represents, how it differs from other data types, and its practical applications is essential for any programmer delving into the intricacies of C.

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.

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

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.

Type Size
char 1 byte
short 2 bytes
int 2 or 4 bytes
unsigned char 1 byte
unsigned short int 2 bytes
unsigned int 2 or 4 bytes
unsigned long 8 bytes
float 4 bytes
double 8 bytes
long double 10 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.

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 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