C Extended Data Types
In this page:
unsigned data_type variable_name = value;
short variable_name = value;
long variable_name = value;
long long variable_name = value;
What are Extended Data Types?
Extended data types combine a base type like int or char with modifiers -- short, long, signed, and unsigned -- that adjust its size, range, or whether it can represent negative values.
Example: What are Extended Data Types?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare small and set it to 100
unsigned short small = 100;
// Print formatted text to the screen
printf("%hu", small);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
short and long Modifiers
The short and long modifiers adjust an integer type's typical size: short usually uses fewer bytes for a smaller range, while long usually uses more bytes to accommodate a wider range of values.
Example: short and long Modifiers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare s and set it to 100
short s = 100;
// Declare l and set it to 1000000L
long l = 1000000L;
// Print formatted text to the screen
printf("%hd %ld", s, l);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
signed and unsigned Modifiers
The signed and unsigned modifiers control whether an integer type can represent negative numbers: signed splits its range between negative and positive values, while unsigned dedicates the entire range to positive values and zero.
Example: signed and unsigned Modifiers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare s and set it to -5
signed int s = -5;
// Declare u and set it to 5
unsigned int u = 5;
// Print formatted text to the screen
printf("%d %u", s, u);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
long long for Very Large Numbers
long long, introduced in C99, guarantees at least 64 bits of storage, making it the standard choice when a value might exceed what int or long can reliably hold across different systems.
Example: long long for Very Large Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare big and set it to 9000000000LL
long long big = 9000000000LL;
// Print formatted text to the screen
printf("%lld", big);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Choosing the Right Extended Type
Choosing the right extended type means balancing memory usage against the range of values actually needed: a small always-positive count fits in unsigned char, while a value that could exceed a billion needs long long.
Example: Choosing the Right Extended Type
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare smallCount and set it to 200
unsigned char smallCount = 200;
// Declare largeValue and set it to 5000000000LL
long long largeValue = 5000000000LL;
// Print formatted text to the screen
printf("%u %lld", smallCount, largeValue);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Storing a value larger than
intcan hold, such as 3000000000, in a plainint, causing overflow. - Printing a
long longwith%dinstead of%lld, which prints garbage. - Assuming
longhas the same size on every platform, when it is 4 bytes on some systems and 8 on others.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers