c-calc is organized into three internal modules —Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Project516/c-calc/llms.txt
Use this file to discover all available pages before exploring further.
calc, input, and logic — each declared in a header under include/ and implemented in a corresponding file under src/. The functions below make up the full internal API of the program. They are not exposed as a library; they are called from src/main.c to drive the interactive calculator loop.
calc module
Thecalc module (include/calc.h, src/calc.c) provides the four basic arithmetic operations. Each function takes two double arguments and returns a double result.
add
add
Returns the sum of two numbers.Parameters
Return value:
| Name | Type | Description |
|---|---|---|
a | double | The first operand |
b | double | The second operand |
a + b as a double.No side effects.subtract
subtract
Returns the difference of two numbers.Parameters
Return value:
| Name | Type | Description |
|---|---|---|
a | double | The minuend |
b | double | The subtrahend |
a - b as a double.No side effects.multiply
multiply
Returns the product of two numbers.Parameters
Return value:
| Name | Type | Description |
|---|---|---|
a | double | The first factor |
b | double | The second factor |
a * b as a double.No side effects.divide
divide
Returns the quotient of two numbers. Handles division by zero explicitly.Parameters
Return value:
| Name | Type | Description |
|---|---|---|
a | double | The dividend |
b | double | The divisor |
a / b as a double, or 0 if b == 0.input module
Theinput module (include/input.h, src/input.c) handles reading user input from stdin. Both functions use scanf internally and print an error message if parsing fails.
input_number
input_number
Prompts the user to enter a number and reads it from stdin.Parameters
Return value: The parsed
| Name | Type | Description |
|---|---|---|
number_place | char[] | A label inserted into the prompt, e.g. "first" or "second" |
double value entered by the user, or 1 on invalid input.Side effects:- Prints
"Enter your {number_place} number: "to stdout before reading. - On a
scanfparse failure, prints"Invalid input\n"to stdout and returns1.
"first":input_op
input_op
Prompts the user to enter an operator character and reads it from stdin.Parameters: None.Return value: The ASCII value of the operator character cast to Side effects:
double, or 1 on invalid input.Although the function signature returns
double, the actual value stored is a single char read by scanf(" %c", &result). The leading space in the format string skips any whitespace left in the input buffer.- Prints
"Enter operator: "to stdout before reading. - On a
scanfparse failure, prints"Invalid input\n"to stdout and returns1.
calculate): +, -, *, /, %.logic module
Thelogic module (include/logic.h, src/logic.c) contains a single dispatch function that routes a calculation to the appropriate calc function based on the operator character.
calculate
calculate
Evaluates a binary arithmetic expression given two operands and an operator character.Parameters
Return value: The result of the operation as a
Side effects:
| Name | Type | Description |
|---|---|---|
first | double | The left-hand operand |
op | char | The operator: +, -, *, /, or % |
second | double | The right-hand operand |
double, or -1 if op is not a recognized operator.Operator dispatch| Operator | Behaviour |
|---|---|
+ | Calls add(first, second) |
- | Calls subtract(first, second) |
* | Calls multiply(first, second) |
/ | Calls divide(first, second) — returns 0 and prints an error if second == 0 |
% | Truncates both operands to int and computes the integer remainder |
- For an unknown
op, prints"Unknown operator\n"to stdout and returns-1. - For
op == '/'withsecond == 0, thedividefunction prints"Divide by 0 error"to stdout.
The
% (modulo) operator truncates first and second to int before computing the remainder. Floating-point fractional parts are discarded silently. For example, calculate(7.9, '%', 3.1) computes 7 % 3 = 1.