← Back to C Course | Chapter 1: Introduction & Basics | Lesson 14 of 21

C Extended Data Types

Extended data types combine a base type with modifiers -- short, long, signed, and unsigned -- that adjust its size, range, or ability to represent negative numbers.
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Storing a value larger than int can hold, such as 3000000000, in a plain int, causing overflow.
  2. Printing a long long with %d instead of %lld, which prints garbage.
  3. Assuming long has the same size on every platform, when it is 4 bytes on some systems and 8 on others.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.