0

I will state my problem in a very simplified form, which is:

If I type in C

void main(){
 int a=3+2;
 double b=7/2;
}

When will a and b, be assigned their values of 5 and 3.5 is it when I compile my code or is it when I run the code?

In other words, What will happen when I press compile? and how it is different from the case when I press run, in terms of assigning the values and doing the computations and how is that different from writing my code as:

void main(){
 int a=5;
 double b=3.5;
}

I am asking this because I have heard about compiler optimization but it is not really my area.

Any comments, reviews will be highly appreciated. Thank you.

5
  • 1
    en.wikipedia.org/wiki/Constant_folding Commented Oct 26, 2013 at 3:00
  • 1
    int main(), double b=7/2, b is 3.0 not 3.5. Commented Oct 26, 2013 at 3:00
  • by the way, 7/2 is integer division, so 3, even if it's later converted to a double. You may want to change it to 7/2.0 Commented Oct 26, 2013 at 3:02
  • Ah I missed that, thank you. Now I appreciate working with java. Commented Oct 26, 2013 at 3:53
  • @Anderson: Remember to always compile with warnings on Commented Oct 26, 2013 at 4:04

5 Answers 5

7

Since you are asking about "code optimization" - a good optimizing compiler will optimize this code down to void main(){}. a and b will be completely eliminated.

Also, 7/2 == 3, not 3.5

Sign up to request clarification or add additional context in comments.

Comments

2

Compiling will translate the high-level language into the lower language, such as assembly. A good compiler may optimize, and this can be customizable (for example with -O2) option or so.

Regarding your code, double b=7/2; will yield 3.0 instead of 3.5, because you do an integer and integer operation. If you would like to have 3.5, you should do it like double b=7.0/2.0;. This is a quite common mistake that people do.

Comments

1

What will happen when I press compile?

Nobody knows. The compiler may optimize it to a constant, or it may not. It probably will, but it isn't required to.

You generally shouldn't worry or really even think about compiler optimization, unless you're in a position that absolutely needs it, which very few developers are. The compiler can usually do a better job than you can.

2 Comments

Saying "Nobody knows" is neither helpful nor true. While it depends on the optimization level of the compiler, compilers have deterministic behavior. You can usually guess what will happen for simple cases, and you can always look at the generated assembly if it's important.
Nobody except who got documentation
1

It's compiler-dependent, a good one will do CF and/or DCE

3 Comments

Actually, a good compiler will optimize everything away. There are no side effects from this code, so, in the end, the main function does nothing.
Thank you for your answer. I am asking this because this is part from a real scenario which is far more complicated because the computations are being done by an 8-bit micro-controller (each computation step does make a difference on the total execution time).
@Anderson: You're welcome. To make sure if the compiler perform such optimizations, look at documentation/assembly output(if any)
1

I don't know anything about optimization either, but I decided to give this a shot. Using, gcc -c -S test.c I got the assembly for the function. Here's what the line int a = 3 + 2 comes out as.

movl    $5, -4(%rbp)

So for me, it's converting the value (3+2) to 5 at compile time, but it depends on the compiler and platform and whatever flags you pass it.

(Also, I made the function return a just so that it wouldn't optimize the code out entirely.)

1 Comment

@Anderson, not that austin cares (he may, I don't know), but if his comment was helpful, give him an up-click :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.