Skip to main content

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.

c-calc is organized into three internal modules — 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

The calc module (include/calc.h, src/calc.c) provides the four basic arithmetic operations. Each function takes two double arguments and returns a double result.
Returns the sum of two numbers.
double add(double a, double b);
Parameters
NameTypeDescription
adoubleThe first operand
bdoubleThe second operand
Return value: a + b as a double.No side effects.
Returns the difference of two numbers.
double subtract(double a, double b);
Parameters
NameTypeDescription
adoubleThe minuend
bdoubleThe subtrahend
Return value: a - b as a double.No side effects.
Returns the product of two numbers.
double multiply(double a, double b);
Parameters
NameTypeDescription
adoubleThe first factor
bdoubleThe second factor
Return value: a * b as a double.No side effects.
Returns the quotient of two numbers. Handles division by zero explicitly.
double divide(double a, double b);
Parameters
NameTypeDescription
adoubleThe dividend
bdoubleThe divisor
Return value: a / b as a double, or 0 if b == 0.
If b is 0, the function prints "Divide by 0 error" to stdout and returns 0 instead of performing the division. No exception or error code is propagated to the caller beyond the 0 return value.

input module

The input 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.
Prompts the user to enter a number and reads it from stdin.
double input_number(char number_place[]);
Parameters
NameTypeDescription
number_placechar[]A label inserted into the prompt, e.g. "first" or "second"
Return value: The parsed 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 scanf parse failure, prints "Invalid input\n" to stdout and returns 1.
Example prompt when called with "first":
Enter your first number:
Prompts the user to enter an operator character and reads it from stdin.
double input_op();
Parameters: None.Return value: The ASCII value of the operator character cast to 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.
Side effects:
  • Prints "Enter operator: " to stdout before reading.
  • On a scanf parse failure, prints "Invalid input\n" to stdout and returns 1.
Supported operators (interpreted by calculate): +, -, *, /, %.

logic module

The logic 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.
Evaluates a binary arithmetic expression given two operands and an operator character.
double calculate(double first, char op, double second);
Parameters
NameTypeDescription
firstdoubleThe left-hand operand
opcharThe operator: +, -, *, /, or %
seconddoubleThe right-hand operand
Return value: The result of the operation as a double, or -1 if op is not a recognized operator.Operator dispatch
OperatorBehaviour
+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
Side effects:
  • For an unknown op, prints "Unknown operator\n" to stdout and returns -1.
  • For op == '/' with second == 0, the divide function 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.

Build docs developers (and LLMs) love