Question Details

No question body available.

Tags

c

Answers (11)

June 24, 2026 Score: 2 Rep: 159,539 Quality: Low Completeness: 0%

Tip: first use "%g" for formatted output.

June 24, 2026 Score: 1 Rep: 35,535 Quality: Medium Completeness: 60%

The calculator must have the following functions: addition, subtraction, multiplication, and division --

One word of advice -- you better have all of your requirements specified up front for something like this, as this may not be a simple project at first glance.

For example, will the expression contain parentheses? Does the expression follow the usual order of operation, for example 5 + 5 x 3 is actually 20, and not 30?

Why is this important to know exactly what the requirements are up-front? Because you will have a very hard time trying to "add features" later on if you start out coding something simple (for example, starting out without following the order of operations), and decide later to follow the order or operations, or add parantheses processing to an already existing program.

If your program is written in an ad-hoc style instead of using well-known techniques to handle mathematical expresions (techniques such as the shunting-yard algorithm, recursive descent parsing, or similar), you will go crazy trying to add features to the program. You will quickly realize the code will become more convoluted and difficult, if not impossible to maintain.

June 24, 2026 Score: 1 Rep: 18,726 Quality: Low Completeness: 0%

Before coming here to ask, did you actually search the web for C tutorials? What you are asking for is literally a first (Well second to "hello world") exercise in most of them.

June 24, 2026 Score: 1 Rep: 3,114 Quality: Low Completeness: 0%

Your intention is clear. What is your question?

Have you tried solving your problem on your own? This may involve writing code and/or searching the web for existing solutions.

June 24, 2026 Score: 0 Rep: 412,123 Quality: Medium Completeness: 30%

What have you tried? What part are you stuck on? Reading input? Parsing the input? Doing the calculations? Printing the result?

As a general hint: Break down the full program into smaller parts. Then break down those parts into smaller steps. Then break down the steps into single actions. Now begin to implement each action one by one, which then solves the steps, which then solves the parts, which then solves the whole program.

Taken as a whole it might seem intimidating, but once you have broken down everything into the smallest pieces possible, each one of them should be simple and easy. And if there's a piece that you are unsure about, then consult your books, tutorials, notes, class mates, teachers, and as a last resort your favorite search engine.

And as a beginner, take the easy way out. For example don't try to parse full expressions written as input. Read two numbers, then read the operation to perform. As you gain experience you can make it more advanced, like allowing multiple operations chained together, handling precedence, and perhaps even parsing whole full expressions through that could be considered an advanced topic (that's usually how compiler-construction courses introduces parsing).

June 24, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 0%

Sorry if I was misunderstood—let me clarify: I don't want to build a website; that's too complex and not optimal in C. I want to build a calculator.

June 24, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 30%

Sorry if I didn't describe it correctly; it's a mathematical calculator.
June 24, 2026 Score: 0 Rep: 125,205 Quality: Low Completeness: 50%

Building a calculator is a great project that'll teach you a lot, but you'll only learn from it if you figure it out yourself. I'd suggest starting by thinking about what kinds of expressions your calculator can evaluate.

Do you want a "normal" calculator that uses infix notation, where the operator is between the operands, like 4 + 7 = or a postfix "RPN" calculator like HP makes, where you enter the operands first and the operator immediately returns a result, like 4 7 +? Either way, think about how to store the data that you need inside your program, and how the operations will work. Can you do it in such a way that you can add more operators later, like √ or x2 or trig functions?

June 24, 2026 Score: 0 Rep: 109,778 Quality: Low Completeness: 40%

Basic user input is tough in C. I suggest you think of your calculator as a command-line calculator thereby making your calculator very very much simpler.

You'd do something like mathcalc.exe "sin(pi/3)" and obtain 0.866. For that you'd need to isolate and define sin, pi, the parenthesis, and the division... but you don't need to start your first version with anything more than +, and -.

June 24, 2026 Score: 0 Rep: 796 Quality: Low Completeness: 60%

Just have a look for tutorials that involve basic operations, like how to read from standard input or alternatively, how to use the command line parameters of main. Then have a look how to convert strings into numbers (e.g. via scanf or sscanf) and how to do basic string comparisons to select the math operator (e.g. strcmp). Besides that, you only need a little bit of if, else if, else to perform the selected operation and finally output the result (via printf with format specifiers).

You can start to use data type int, though the results after a division rounding can be a bit unexpected for a newcomer (it ignores the fraction instead of a correct mathematical rounding).

Optionally you can switch to data type float or double, to support floating point numbers, though the output could still look a bit unfamiliar in some cases due to the scientific notation and for some floating point numbers that cannot be represented exactly by binary numbers (try 2 / 10).

The entire code will probably be shorter than this post.

June 24, 2026 Score: 0 Rep: 2,253 Quality: Low Completeness: 0%

please delete this response, it's just clutter, besides, it should not be in a code block ... I'll delete mine after