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

C Numeric Data Types

C provides several integer types (short, int, long, long long) and floating-point types (float, double), each with a different range or precision, chosen based on what kind of number needs to be stored.
Syntax
c
integer_type variable_name = whole_number;
floating_type variable_name = decimal_number;

// integer_type: short, int, long, long long
// floating_type: float, double

Integer Types

C provides several integer types -- short, int, long, and long long -- each offering a different range of whole numbers, with int being the standard default choice for most everyday counting and arithmetic.

Example: Integer Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare s and set it to 10
	short s = 10;
	// Declare i and set it to 1000
	int i = 1000;
	long l = 100000L;
	long long ll = 10000000000LL;
	// Print formatted text to the screen
	printf("%hd %d %ld %lld", s, i, l, ll);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Floating-Point Types

Floating-point types, float and double, store numbers with a fractional part, with double offering roughly twice the precision of float at the cost of using more memory.

Example: Floating-Point Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare f and set it to 3.14f
	float f = 3.14f;
	// Declare d and set it to 3.14159265358979
	double d = 3.14159265358979;
	// Print formatted text to the screen
	printf("%.2f %.11f", f, d);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Choosing int vs float vs double

Choosing between int, float, and double comes down to whether a value is always a whole number, needs decimal precision with limited memory use, or needs the higher precision double provides for most general decimal math.

Example: Choosing int vs float vs double

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare quantity and set it to 3
	int quantity = 3;
	// Declare price and set it to 19.99
	double price = 19.99;
	// Print formatted text to the screen
	printf("Total: %.2f", quantity * price);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Number Ranges and Overflow

Every numeric type has a fixed range of representable values, and exceeding that range causes overflow, where the value silently wraps around to an unexpected result instead of raising an error.

Example: Number Ranges and Overflow

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare maxVal and set it to 2147483647
	int maxVal = 2147483647;
	maxVal = maxVal + 1;
	// Print formatted text to the screen
	printf("%d", maxVal);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Numeric Literals

Numeric literals can be written in decimal, hexadecimal (with a 0x prefix), or octal (with a leading 0) form, and suffixes like L, U, or F tell the compiler which specific type to treat the literal as.

Example: Numeric Literals

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare decimal and set it to 100
	int decimal = 100;
	// Declare hex and set it to 0x64
	int hex = 0x64;
	int octal = 0144;
	// Print formatted text to the screen
	printf("%d %d %d", decimal, hex, octal);
	// 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. Dividing two ints like 5 / 2 and expecting 2.5, when integer division gives 2.
  2. Overflowing an int, such as adding 1 to INT_MAX, which is undefined behavior for signed types.
  3. Using the wrong format specifier, such as %d for a double, which prints garbage.

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.