C Numeric Data Types
In this page:
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
#include <stdio.h>
int main() {
short s = 10;
int i = 1000;
long l = 100000L;
long long ll = 10000000000LL;
printf("%hd %d %ld %lld", s, i, l, ll);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
float f = 3.14f;
double d = 3.14159265358979;
printf("%.2f %.11f", f, d);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
int quantity = 3;
double price = 19.99;
printf("Total: %.2f", quantity * price);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
int maxVal = 2147483647;
maxVal = maxVal + 1;
printf("%d", maxVal);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <stdio.h>
int main() {
int decimal = 100;
int hex = 0x64;
int octal = 0144;
printf("%d %d %d", decimal, hex, octal);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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