DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 16 of 64
←PreviousPrevNextNext→

Numbers in C

Integer Types

Integers are whole numbers that can be positive, negative, or zero. They come in multiple sizes depending on memory and range requirements.

C supports signed (default) and unsigned versions of integers. Exact sizes and ranges are implementation-defined; use `sizeof` and the limits in `` to discover them on your system.

ℹ️ Note: On LP64 systems (Linux/macOS), `long` is typically 8 bytes; on LLP64 (Windows), `long` is typically 4 bytes.
TypeTypical SizeRange (typical)
short int2 bytes-32,768 to 32,767
unsigned short int2 bytes0 to 65,535
int4 bytes-2,147,483,648 to 2,147,483,647
unsigned int4 bytes0 to 4,294,967,295
long int4 or 8 bytesSystem-dependent
unsigned long int4 or 8 bytesSystem-dependent, non-negative
long long int8 bytes≈ −9.22e18 to 9.22e18
unsigned long long int8 bytes0 to ≈ 1.84e19

Floating-Point Types

Floating-point numbers represent real numbers with decimal parts.

`float` provides single precision (about 6–7 digits), while `double` provides double precision (about 15–16 digits).

`long double` may offer higher precision, but its exact precision and size are platform/compiler dependent.

Example
#include <stdio.h>
int main(void) {
    float pi = 3.14159f;
    double g = 9.80665;
    printf("pi=%.2f g=%.5f\n", pi, g);
    return 0;
}
Output
pi=3.14 g=9.80665
ℹ️ Note: In `printf`, `%f` expects a `double`. A `float` is promoted to `double` when passed to `printf`.

Finding Sizes and Ranges

Use `sizeof` to get sizes, and `` / `` for ranges and precision constants.

Example
#include <stdio.h>
#include <limits.h>
#include <float.h>

int main(void) {
    printf("short: %zu bytes, range [%d, %d]\n", sizeof(short), SHRT_MIN, SHRT_MAX);
    printf("int: %zu bytes, range [%d, %d]\n", sizeof(int), INT_MIN, INT_MAX);
    printf("long: %zu bytes, range [%ld, %ld]\n", sizeof(long), LONG_MIN, LONG_MAX);
    printf("long long: %zu bytes, range [%lld, %lld]\n", sizeof(long long), LLONG_MIN, LLONG_MAX);
    printf("float: %zu bytes, ~%d digits, max %.0e\n", sizeof(float), FLT_DIG, FLT_MAX);
    printf("double: %zu bytes, ~%d digits, max %.0e\n", sizeof(double), DBL_DIG, DBL_MAX);
    return 0;
}
Output
short: 2 bytes, range [-32768, 32767]
int: 4 bytes, range [-2147483648, 2147483647]
long: 8 bytes, range [-9223372036854775808, 9223372036854775807]
long long: 8 bytes, range [-9223372036854775808, 9223372036854775807]
float: 4 bytes, ~6 digits, max 3e+38
double: 8 bytes, ~15 digits, max 1e+308
ℹ️ Note: Example output shown for a typical 64-bit Unix-like system; values vary by platform.

Integer & Floating Literal Suffixes

You can control literal types using suffixes:

LiteralType
42Uunsigned int
42Llong int
42LLlong long int
1.0ffloat
1.0double (default)
1.0Llong double

Choosing the Right Number Type

• Use `int` for counting items, loop indices, and most whole numbers.

• Use `long long` (or an unsigned variant) for very large integers.

• Use `double` by default for real-number calculations; reserve `float` for memory-sensitive cases.

• Use fixed-width types from `` (e.g., `int32_t`, `uint64_t`) when you need exact sizes (files, networks, hardware).

• Be aware of overflow: signed integer overflow is undefined behavior; unsigned overflow wraps modulo 2ⁿ.

Test your knowledge: Numbers in C
Quiz Configuration
4 of 8 questions
Random
Previous allowed
Review enabled
Early close allowed
Estimated time: 8 min
C BasicsTopic 16 of 64
←PreviousPrevNextNext→