C Numeric Data Types

1. What are Numeric Data Types?

  • Numeric data types in C are used to store numbers, either whole numbers or decimal numbers.

  • They are broadly divided into:

  1. Integer types → Whole numbers

  2. Floating-point types → Decimal numbers


2. Integer Data Types

  • Used to store whole numbers (no fractions).

  • Memory size may vary depending on the compiler and system (typical for 32-bit system shown below).

Type Size (Typical) Range (Approx) Example
int 4 bytes -2,147,483,648 to 2,147,483,647 int a = 100;
short int 2 bytes -32,768 to 32,767 short b = 10;
long int 4–8 bytes -2,147,483,648 to 2,147,483,647 long c = 100000;
long long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 long long d = 1000000000;
unsigned varies 0 to 4,294,967,295 unsigned int u = 100;

Example:

#include <stdio.h>

int main() {
int a = 100;
short b = 10;
long c = 100000;
unsigned int u = 50;

printf(“a = %d\nb = %d\nc = %ld\nu = %u\n”, a, b, c, u);
return 0;
}


3. Floating-Point Data Types

  • Used to store numbers with fractional parts.

Type Size (Typical) Example
float 4 bytes float x = 3.14;
double 8 bytes double y = 3.141592;
long double 10–16 bytes long double z = 3.1415926535;

Example:

#include <stdio.h>

int main() {
float pi = 3.14;
double largePi = 3.1415926535;

printf(“Float: %.2f\nDouble: %.10lf\n”, pi, largePi);
return 0;
}

Output:

Float: 3.14
Double: 3.1415926535
  • %.nf or %.nlf is used to limit decimal places in output.


4. Key Points

  1. Integer types store whole numbers; floating-point types store decimals.

  2. Memory size affects range and precision.

  3. Use unsigned if only positive numbers are needed.

  4. Use float for approximate decimals and double for higher precision.


5. Combined Example

#include <stdio.h>

int main() {
int a = 10;
float b = 5.75;
double c = 19.987654;
unsigned int d = 100;

printf(“Integer a = %d\n”, a);
printf(“Float b = %.2f\n”, b);
printf(“Double c = %.6lf\n”, c);
printf(“Unsigned d = %u\n”, d);

return 0;
}

Output:

Integer a = 10
Float b = 5.75
Double c = 19.987654
Unsigned d = 100

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *