# a simple question

This is a discussion on a simple question within the C Programming forums, part of the General Programming Boards category; hello friends, I have a question, Let there be a expression g = a/2 + b*4/a - abc/3 the value ...

1. ## a simple question

hello friends,

I have a question,

Let there be a expression

g = a/2 + b*4/a - abc/3

the value of variables is given, let a =10, b = 9, abc = 6

Now I want to know when this expression is calculated in a C program,

the computer has to access the values of the variables, does it replaces all the variables by their values in one go before solving the expression

or

it first take a particular operator ( like the first /) and accesses the values of the operands, then takes second operator and its operands values is accessed.

Thanks a lot.

2. Huh,If you're trying to parse it http://en.wikipedia.org/wiki/Binary_tree
Otherwise http://catb.org/~esr/faqs/smart-questions.html

Although you haven't actually asked anything!?

3. Ehm, I'd say that it's "both" or "neither", depending on how you look at it.

If the compiler can figure out that the variable isn't changing, it will calculate the result then and there - e.g. in the below code, it will assign the constant 19 to g[i].

Code:
```int a = 10;
int b = 9;
int i;
int g[10];
for(i = 0; i < 10; i++)
g[i] = a + b;```
In a case where the compiler can't determine that the variables are staying the same value, it will read the value from the memory location that belongs to that variable. There is no strict rule about which order it FETCHES the memory content (the compiler may decide to pre-load the variable a into register R1, then use it for both the divide and add operation from that register. Or it may fetch it at the divide, and then fetch it again when it adds the variable - if you want to GUARANTEE that the variable is accessed each time, you need to declare it using "volatile".

In some processors, data can be accessed by for example add operations IN the memory location, so it doesn't need to be fetched into a register. Other processors the data MUST be in a register when it's being used for math operations. x86 (AMD and Intel processors) can do both memory and register based operations, but if the same variable is used multiple times, it will be slightly faster to run the code if it's in a register (and the code will be a little bit smaller that way, which isn't a bad thing).

--
Mats

4. Some of those decisions are left up to the compiler as to how to evaluate an expression. You know the result and how it must get there from a mathematical and a C standpoint, though.

This is how LCC broke down your line of code into assembly without optimizations:

Code:
```;    7  g = a/2 + b*4/a - abc/3;
.line   7
movl    -8(&#37;ebp),%edi
movl    %edi,%eax
movl    \$2,%ecx
cdq
idivl   %ecx
movl    %eax,-20(%ebp)
movl    -12(%ebp),%esi
movl    %esi,%eax
sall    \$2,%eax
movl    %edi,%ecx
cdq
idivl   %ecx
movl    -20(%ebp),%edi
movl    -16(%ebp),%eax
movl    \$3,%ecx
cdq
idivl   %ecx
subl    %eax,%edi
movl    %edi,-4(%ebp)```
1. It loads a first, divides it by 2, and stores the result in a temp variable.
2. It loads b into memory, fast multiplies by 4, divides by a, stores the result into the temp variable, and then adds the two temp values together.
3. It moves the last variable, abc, divides by 3, subtracts it from the result of the addition of the previous two temp values, and finally sets G to the value.

This is all without optimizations, and it's rather a silly way to write it into assembly, but it works, and it's easy enough to explain.

5. aww pretty colours, but I don't think that's what he's looking for. Well guesswork really

6. Originally Posted by zacs7
aww pretty colours, but I don't think that's what he's looking for. Well guesswork really
Who cares what he's looking for? I had a chance to post color-coded assembly code.