# order of operations in C

Page 1 of 1

## 4 Replies - 647 Views - Last Post: 01 September 2012 - 08:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=290664&amp;s=acf6cb28c2eeb88882965266af037d41&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Weisguy216

Reputation: 0
• Posts: 3
• Joined: 01-September 12

# order of operations in C

Posted 01 September 2012 - 08:01 AM

Hi all this was a lab I had to do for school. The lab is complete and the code works as it should but I'm assuming there is a proper way to do the math that I am missing.
I could not figure out the order of operations so created 2 more variables to do the math for (2b+c) and (d-e).

So is there a "better" way to write the math part of this or is how I did it correct?
This project has already been turned in, I just want to know for future problems as I'm sure the math will get more intense.

```// MRW Lab_1b.cpp
// Solve the equation a(2b+c)+(d-e) from user input
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
float vara, varb, varc, vard, vare, bcsum, desum, ans;
printf("Please enter the values for variables to be used while solving a(2b+c)+(d-e)\n\n");

//****User input****
printf("a=");scanf(" %f",&vara);
printf("b=");scanf(" %f",&varb);
printf("c=");scanf(" %f",&varc);
printf("d=");scanf(" %f",&vard);
printf("e=");scanf(" %f",&vare);

//****math****
bcsum = 2*varb+varc;
desum = vard-vare;
ans = vara *bcsum+desum;

printf("The result for a(2b+c)+(d-e) is %f\n", ans );

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: order of operations in C

### #2 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: order of operations in C

Posted 01 September 2012 - 08:08 AM

Yes, there was another way. By using brackets. Something like
```ans = vara * (2*varb+varc) + (vard-vare);

```

### #3 sepp2k

• D.I.C Lover

Reputation: 2549
• Posts: 4,072
• Joined: 21-June 11

## Re: order of operations in C

Posted 01 September 2012 - 08:09 AM

I'm not entirely sure what you mean when you say that you couldn't figure out the order of operations.

There's nothing wrong with creating additional variables (in fact it is often preferable for readability if the variables have meaningful names), but doing it on one line as in your formula would have worked fine, too:

```ans = vara * (2 * varb + varc) + (vard - vare);
```

### #4 jimblumberg

Reputation: 5425
• Posts: 16,912
• Joined: 25-December 09

## Re: order of operations in C

Posted 01 September 2012 - 08:13 AM

Use parentheses. But, sometimes using multiple variables make troubleshooting easier.

I usually make sure of my precedence by separating the addition/subtraction and division/multiplication.

```ans = (vara * ((2 * varb) + varc))) + (vard - vare);
```

Jim

This post has been edited by jimblumberg: 01 September 2012 - 08:16 AM

### #5 Weisguy216

Reputation: 0
• Posts: 3
• Joined: 01-September 12

## Re: order of operations in C

Posted 01 September 2012 - 08:44 AM

Thank you very much for the help. I probably made some kind of typo when I first did this because I could not get the correct answer when using the brackets. I went back and tried what was said and it works fine.