79656418

Date: 2025-06-06 20:02:05
Score: 0.5
Natty:
Report link

When you write code in a programming language like C/C++, actually you are working with a compiler (I'm gonna talk about gcc, which is a C/C++ compiler) which takes your file (myfile.c), read it and "elaborate" what you wrote, doing optimizations when is possible.

Now, for the code you wrote, actually the compiler is working on int, so integers. Integers, in languages like C/C++, are just normal numbers, nothing more (just don't think that integers are a bunch of 1 and 0, just thinking at an high level) nothing less.

Actually, the code you wrote is saying "I want to create an integer n, and an integer index, where n has an x value and index has y value". Now, since you are working with integers (remember, just numbers), the compiler can actually evaluate the right-hand side of your expression (remember this term, also known as RHS) and assign the value to the LHS (left-hand side), where RHS is 54 and LHS is n. So, n will have a value, evaluated from the calculations of the RHS, and index will have another value, evaluated from the calculation of the RHS 2 * n. BUT, since n is an integer (just a number) there is no need to remember the fact that index is evaluated starting from n, because n = 54, so the compiler just substitute n with 54 and return the value 108.

You may asking "Why, why can't the compiler just think that like a math function". The answer is "C/C++ is not a mathematical language, but a programming language, so it's optimized to be a programming language". Actually, since you are interested in evaluating an int, the compiler just substitute the value of the variable with the actual value. For love of clarity, the compiler actually has to produce an assembly code. In assembly, to evaluate a variable you can only use basic math (addition, subtraction, multiplication and "division" (not exactly right, but fine for now)). If you just substitute the value of n with 54, as the compiler does, when evaluating index you just have to do a basic multiplication (one operation), meanwhile if you don't, you must use an instruction to recover the value in n (a load operation), then you have to store it in a new register, do the multiplication and store the result in the register of index, four operations.

Since C/C++ is not thinked to "work like math", compiler just does the substitution in order to optimize the code, so instead of four operations in assembly you do just one (if you are interested)

Notice that this is not an universal rule, if you use something like Python, where actually an int is not a number but a class, all what I said won't work, because actually Python is thinked not to create and optimize code, but for syntax clarity and a 'non-violent' approach for programming.

Since the compiler does the constant folding operation (the substitution), index won't be linked to n, so if you modify n this won't affect index. If you want to change this, I suggest you to study what a struct is and play with it

Greetings

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (0.5):
Posted by: Roci49