In the C programming language, a short int is a data type that contains integers as values and will contain values from the range of -32,768 to 32,767. It occupies 2-byte memory space. Short int can not contain values beyond -32,768 to 32,767. In this blog, we will learn a short int in C Programming language in detail.
Table of Contents:
Short Int in C Programming
In C Programming language, Short int is a data type that takes 2 bytes of memory space and can hold values from -32768 to 32768. We can use short int when we need to contain smaller values like age, temperature, weight, and count, etc. It works the same as int data type, but instead of taking 4-byte memory, short int takes 2 bytes of memory.
Syntax of Short int
short int var;
Here, short int is the data type, and var is the name of the variable.
If you are a fresher and want to learn C Programming language in depth, please refer:
Practical Examples of Short Int in C Programming
Let’s learn this using some practical examples:
1. Print the sum of two numbers using Short int in C programming language:
Code:
#include
int main() {
short int intellipaat1 = 10;
short int intellipaat2 = 20;
printf("Sum = %d",intellipaat1 + intellipaat2 );
return 0;
}
Output:
Sum = 30
Explanation: Here, we have declared two variables and printed the sum of them.
2. Print the multiplication of two numbers using short int in C programming language:
Code:
#include
int main() {
short int intellipaat1 = 10;
short int intellipaat2 = 20;
printf("Product = %d",intellipaat1 * intellipaat2 );
return 0;
}
Output:
Product = 200
Explanation: Here, we have declared two variables, and printed the product of them.
If you want to practice some Basic Programming examples of C Language, Please refer: Basic C Programming Examples
Size of Different int Data Types
In C Programming language, the size of different int data types depends on the system and compiler. Let’s have a look at the below table:
Data type |
Memory taken(in bytes) |
Range (Signed) |
char | 1 | -128 to 127 |
short | 2 | -32,768 to 32,767 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long | 4 or 8 | -2,147,483,648 to 2,147,483,647 |
long long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Conclusion
In Conclusion, short int is an int data type, that contains the integer values and contains 2 bytes of memory. So far in this article, we have learned what is short int, its syntax, code, size of different data types, etc. If you want to learn more about C Programming Language, Please refer C Programming Language Course.
FAQs
What range of values can be stored in a short int in c programming?
The values in the range of -32,768 to 32,767 can be stored in a short int in C Programming language.
How much memory does a short int occupy?
A short int occupies 2 bytes of memory.
What are short and long int in C?
Short and long int are used to represent integer variables. A short int is used to store smaller variables by taking 2 bytes of memory, and long int is used to store larger integer variables by taking 8 bytes of memory.