Back to C Programming
2026-04-177 min read

arithmetic type

Learn arithmetic type step by step with clear examples and exercises.

Title: Mastering Arithmetic Types in C Programming: A full guide

Why This Matters

Understanding arithmetic types is crucial for any C programmer as it forms the backbone of numerical calculations and operations. It's essential to grasp these concepts to write efficient, error-free code, tackle real-world programming problems, and prepare for interviews. Arithmetic types in C provide a robust foundation for performing various mathematical operations, from simple arithmetics to complex calculations.

Mastering arithmetic types will enable you to:

  1. Perform accurate numerical computations with different data types.
  2. Understand the limitations and trade-offs of each arithmetic type in C.
  3. Write more efficient code by choosing the appropriate data type for specific use cases.
  4. Avoid common mistakes that can lead to runtime errors or incorrect results.
  5. Solve complex mathematical problems using various arithmetic types, such as floating-point numbers and complex numbers.

Prerequisites

Before diving into arithmetic types, you should have a good understanding of:

  1. Basic C syntax and variables
  2. Control structures (if-else, loops)
  3. Functions and function prototypes
  4. Input/Output operations using scanf() and printf()
  5. Data structures like arrays and pointers
  6. Understanding of memory allocation and deallocation in C
  7. Basic concepts of algorithms and their time complexity
  8. Familiarity with mathematical concepts such as roots, quadratic equations, and trigonometry.

Core Concept

Arithmetic types in C include Boolean, character, integer, real floating, complex floating, and imaginary floating types. Let's look at into each one, providing examples and explanations to help you better understand them.

Boolean Type

The Boolean type can hold one of two values: 0 (false) or 1 (true). In C, the _Bool data type is used to represent Booleans (until C23), while bool and macros true and false are available since C23.

#include <stdbool.h>

bool myBoolean = true; // Since C23
_Bool yourBoolean = 1; // Until C23

Note that conversion to _Bool (until C23) bool (since C23) does not work the same as conversion to other integer types:

printf("%d\n", ( bool ) 0.5); // Outputs 1 (true)
printf("%d\n", ( int ) 0.5); // Outputs 0

Character Types

Character types in C are used to represent individual characters. char is the default character type, equivalent to either signed or unsigned char (which one is implementation-defined and may be controlled by a compiler commandline switch).

char myChar = 'A'; // ASCII value of 65
unsigned char yourChar = 255; // Maximum value for an unsigned char

Integer Types

C provides several integer types, each with a specific range and storage size. The most common ones are:

  1. short int (also accessible as short)
  2. unsigned short int (also accessible as unsigned short)
  3. int (also accessible as signed int)
  4. long int (also accessible as long)
  5. unsigned long int (also accessible as unsigned long)
  6. long long int (also accessible as long long)
  7. unsigned long long int (also accessible as unsigned long long)

The most optimal integer type for the platform is int, which is guaranteed to be at least 16 bits. Most current systems use 32 bits.

short myShort = 32767; // Maximum value for a short int
unsigned short yourShort = 65535; // Maximum value for an unsigned short int
int myInt = 2147483647; // Maximum value for an int (32-bit system)
long long myLongLong = 9223372036854775807LL; // Maximum value for a long long int

Real Floating Types

Real floating types in C are used to represent real numbers with a fractional part. The most common real floating type is float, which uses approximately 4 bytes of memory and has a precision of about 6 decimal digits.

float myFloat = 3.14159; // A float value
double myDouble = 3.14159265358979323846; // A double value with higher precision
long double myLongDouble = 3.141592653589793238462643383279502884L; // A long double value with even higher precision

Complex Floating Types

Complex numbers consist of a real and an imaginary part. In C, the complex data type is used to represent complex numbers.

#include <complex.h>

complex myComplex = 3 + 4i; // A complex number with real part 3 and imaginary part 4

Imaginary Floating Types

Imaginary floating types are used to represent the imaginary part of a complex number. The imag function can be used to extract the imaginary part of a complex number.

double imagPart = cimag(myComplex); // Extracts the imaginary part of myComplex

Worked Example

Let's write a program that calculates the area of a circle using different arithmetic types for the radius: char, short int, int, long long int, float, double, and long double.

#include <stdio.h>
#include <math.h>

int main() {
char radiusChar = 5;
short int radiusShort = 10;
int radiusInt = 20;
long long int radiusLongLong = 30;
float radiusFloat = 4.0f; // Using the 'f' suffix to explicitly declare a float
double radiusDouble = 5.0; // Explicitly declaring a double using the 'l' suffix is not necessary
long double radiusLongDouble = 6.0L; // Explicitly declaring a long double using the 'L' suffix

printf("Area of circle with char radius: %.2f\n", M_PI * (radiusChar * radiusChar));
printf("Area of circle with short int radius: %.2f\n", M_PI * (radiusShort * radiusShort));
printf("Area of circle with int radius: %.2f\n", M_PI * (radiusInt * radiusInt));
printf("Area of circle with long long int radius: %.2f\n", M_PI * (radiusLongLong * radiusLongLong));
printf("Area of circle with float radius: %.2f\n", M_PI * (radiusFloat * radiusFloat));
printf("Area of circle with double radius: %.2f\n", M_PI * (radiusDouble * radiusDouble));
printf("Area of circle with long double radius: %.2f\n", M_PI * (radiusLongDouble * radiusLongDouble));

return 0;
}

Common Mistakes

  1. Misunderstanding Boolean conversion: Remember that conversion to _Bool (until C23) bool (since C23) does not work the same as conversion to other integer types.
  1. Ignoring character type differences: Be aware that the default character type can be either signed or unsigned, depending on the compiler settings.
  1. Incorrect use of integer types: Ensure you choose the appropriate integer type for your needs, considering the maximum and minimum values they can hold.
  1. Neglecting real floating precision: Keep in mind that float has a lower precision than other arithmetic types, which might lead to rounding errors or inaccurate results in certain calculations.
  1. Not understanding complex numbers: Remember that complex numbers consist of both real and imaginary parts, and use the complex data type in C to represent them.
  1. Misusing floating-point arithmetic: Be aware that floating-point operations can lead to rounding errors due to their finite precision.
  1. Not handling overflow and underflow: Be mindful of potential overflow and underflow situations when working with large or small values, respectively.

Practice Questions

  1. Write a program that calculates the factorial of a number using int, long long int, and float. Compare the results for large numbers.
  2. Write a program that sorts an array of integers using bubble sort, insertion sort, and quicksort algorithms. Measure the time complexity of each algorithm for different input sizes.
  3. Implement a simple calculator in C that performs addition, subtraction, multiplication, division, and modulus operations on floating-point numbers. Handle potential rounding errors gracefully.
  4. Write a program that calculates the roots of a quadratic equation using complex numbers and their properties.
  5. Create a function to find the greatest common divisor (GCD) of two integers using Euclid's algorithm, implementing it for both int and unsigned long long int. Compare the performance and precision differences between the two implementations.
  6. Implement a program that calculates the Fibonacci sequence up to a given number using recursion and iteration, comparing the efficiency of each approach for large numbers.
  7. Write a program that finds all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm, implementing it for both int and unsigned long long int. Compare the performance and precision differences between the two implementations.

FAQ

  1. Why are there multiple integer types in C? Different integer types have varying storage sizes and ranges, allowing for more efficient use of memory and better performance in certain situations.
  1. What is the difference between signed and unsigned character types? Signed character types can hold negative values, while unsigned character types only store non-negative values.
  1. Why should I be careful when using floating-point numbers? Floating-point numbers have a limited precision, which might lead to rounding errors or inaccurate results in certain calculations.
  1. What is the purpose of the complex and imag functions in C? The complex function is used to create complex numbers, while the imag function extracts the imaginary part of a complex number.
  1. Why are there different floating-point types (float, double, long double) in C? Each floating-point type has a different precision and memory size, allowing for more accurate calculations when dealing with larger or more precise numbers.
  1. What is the difference between float and double in terms of precision and memory usage? A double typically offers higher precision (around 15 decimal digits) and uses approximately twice as much memory as a float (8 bytes vs 4 bytes).
  1. How can I determine the size of a specific arithmetic type on my system? You can use the sizeof operator to get the size of an arithmetic type in bytes. For example, printf("%zu\n", sizeof(int)); will print the size of an int on your system.
  1. What is the maximum value that can be stored in a short, int, and long long integer types? The maximum values for these types depend on the platform. On a 32-bit system, the maximum values are:
  • short int: 32767
  • int: 2147483647
  • long long int: 9223372036854775807LL