A command-line calculator written in C that performs arithmetic operations — addition, subtraction, multiplication, and division — on integers of any size, far beyond the 64-bit limit of standard C data types.
Each number is stored digit-by-digit in a Doubly Linked List, enabling precise computation through manual digit-level arithmetic.
- Addition of arbitrarily large integers
- Subtraction with correct sign and borrow handling
- Multiplication using long-multiplication with partial products
- Division using repeated subtraction (returns integer quotient)
- Full support for negative numbers (
-sign prefix) - Division-by-zero detection with error message
- Leading zero removal from results
- Complete memory cleanup after every operation
Doubly Linked List (Dlist)
head → [d1] ↔ [d2] ↔ [d3] ↔ ... ↔ [dn] ← tail
MSB LSB
- Each node stores ONE digit
- Arithmetic traversal goes
tail → head(LSB to MSB) - Result digits are inserted at front to maintain correct order
apc/
├── apc.h # Dlist struct definition + all function prototypes
├── main.c # CLI argument parsing, sign detection, operator dispatch
├── apc.c # Core list utilities: insert, delete, compare, print
├── addition.c # Tail-to-head addition with carry
├── subtraction.c # Tail-to-head subtraction with borrow
├── multiplication.c # Long multiplication with partial product accumulation
├── division.c # Repeated subtraction-based integer division
└── Makefile
make./apc <operand1> <operator> <operand2>| Symbol | Operation |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
x |
Multiplication (alternate) |
/ |
Division |
⚠️ Wrap*in quotes to prevent shell expansion:./apc 99 '*' 99
./apc 10 + 20 # → 30
./apc 999 + 1 # → 1000
./apc -10 + -20 # → -30
./apc -10 + 5 # → -5
./apc 0 + 0 # → 0./apc 50 - 20 # → 30
./apc 1000 - 1 # → 999
./apc 20 - 50 # → -30
./apc 123 - 123 # → 0
./apc -50 - 20 # → -70./apc 5 '*' 4 # → 20
./apc 99 '*' 99 # → 9801
./apc 0 '*' 123 # → 0
./apc -10 '*' 5 # → -50
./apc -10 '*' -5 # → 50./apc 100 / 5 # → 20
./apc 7 / 2 # → 3
./apc 5 / 10 # → 0
./apc -20 / 5 # → -4
./apc 10 / 0 # → Division failed- Traverse both lists from
tail → head(LSB first) - At each step:
sum = d1 + d2 + carry, storesum % 10, updatecarry = sum / 10 - Insert each result digit at the front of the result list
- Loop continues while either list has digits OR carry is non-zero (handles
999 + 1 → 1000)
- Assumes
head1 >= head2(caller ensures this viacompare_list()) - Traverse from
tail → headwith borrow tracking - If
d1 < d2: borrow from next digit (d1 += 10,borrow = 1)
- For each digit of operand2 (
tail → head), multiply against all digits of operand1 - Each partial product gets positional zero-padding (place value)
- Partial products are accumulated using the addition function
- Checks for zero divisor before starting
- If
dividend < divisor: quotient is 0 - Repeatedly subtracts divisor from dividend, counting iterations
- Count is converted to a digit list as the quotient
- Signs are detected from the leading
-character before list conversion - Result sign is negative only if signs differ (for
*and/) or as computed (for+and-) -0is printed as0
Every code path (including error paths like division by zero) calls delete_list() on all lists before returning. No memory leaks.
make cleanSuchithra S
B.E. ECE — Gopalan College of Engineering & Management, VTU (2025)
Advanced Embedded Systems Training — Emertxe Information Technologies, Bengaluru
GitHub: github.com/suchithrasr
LinkedIn: linkedin.com/in/suchithrasr