Using ++ inside the sizeof keyword [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what's the mechanism of sizeof() in C/C++?
Hi,
I'm a TA for a university, and recently, I showed my undergraduate students the following C code from a C puzzle I found:
int i = 5;
int j = sizeof(i++);
printf("%d\n%d\n", i, j);
I just have one question: why is the output for i equal to 5, not 6? Is the ++ simply disregarded? What's going on here? Thanks!

The expression in a sizeof is not evaluated - only its type is used. In this case, the type is int, the result of what i++ would produce if it were evaluated. This behavior is necessary, as sizeof is actually a compile-time operation (so its result can be used for things like sizing arrays), not a run-time one.

The sizeof operator is evaluated at compile-time. sizeof(i++) basically reads to the compiler as sizeof(int) (discarding the ++).
To demonstrate this you can look at the assembly view of your little program:
As you can see in the marked line, the size of the integer (4) is already there and just loaded into i. It is not evaluated or even calculated when the program runs.

Yes, inside sizeof is only evaluated for the type.

The main reason is that sizeof is not a function, it is an operator. And it is mostly evaluated at compile-time unless there is a variable length array. Since int's size can be evaluated at compile-time, therefore, it returns 4.

why is the output for i equal to 5, not 6?
sizeof does not evaluate the expression inside it, only the type.
What we have keep in mind is that sizeof is not a function but a compile time operator, so, it is impossible for it evaluate its content.

Related

Why is the output of the following program showing compile time error? [duplicate]

This question already has answers here:
Why does increment operation ++a++ not work, at least in C?
(3 answers)
Closed 6 months ago.
Why is the output of the following program showing compile time error?
Please explain "lvalue required"
#include<stdio.h>
int main()
{
int a=5;
printf("%d", ++a++);
return 0;
}
A language-lawyer proof explanation is a bit lengthy, here's an attempt at a simplified explanation:
The "l" in "lvalue" comes from "left" as in a value that can appear on the left hand side of an assignment operation, which includes many named variables:
int a;
a = 42; // a is an lvalue and can be assigned to
Now due to operator precedence the expression ++a++ is parsed as ++(a++). And since a++ modifies a "later" but, as expression, evaluates to the current value of a it "returns" a copy of this current value of a, a temporary value.
This temporary value is unnamed (it is not a) and it's not an lvalue, wherefore it can't be assigned to.
You can't write a++ = 42 because you'd be assigning to the temporary value, rather than a variable, and for the same reason you can't write ++a++.
Again, you'll have to dive much deeper, and also give yourself some time to develop an intuitive feeling for what an lvalue is, then this will become much clearer.

Why the current macro will return the value of 14? [duplicate]

This question already has answers here:
Macros and postincrement
(8 answers)
Closed 4 years ago.
Why the printed value is 14 ?
when I try to calculate the current macro I get this .
NOTE** this question was asked in a test.
option 1:
2+3*4++ then 2+12++ then 2+13 = 15
option 2:
2+3*4++ then 2+12++ then 2+12++ = 14 (and after print the value will raise to 15?)
The code:
#include <stdio.h>
#define MACRO(x,y) x*y++
void main()
{
int a=2,b=3,c=4;
printf("%d\n",MACRO(a+b,c));
}
moreover I tried to put the result in a var and got the same result of 14 meaning the value stayed 14 after print. can some explain to me why the op ++ not happening at all?
Macros perform direct substitution of the given tokens . So this:
MACRO(a+b,c)
Becomes:
a+b*c++
Because multiplication has higher precedence that addition, it gets performed first.
Best practice when working with macros is to parenthesize all arguments:
#define MACRO(x,y) ((x)*(y)++)
Then you'll get:
((a+b)*(c)++)
Which is probably what you expect.
As for the prefix ++ operator, this operator evaluates to the current value of the variable, then increments the variable. Since the ++ operator is attached to c, that's what gets incremented. The value of the expression doesn't change as a result, but the value of c is now 5.
MACRO(a+b,c) is expanded to a+b*c++
That is 2 + 3 * 4 which is 14.
At the end of the printf, c will be incremented to 5 (i.e. a post-increment), which you don't observe. It would have been a different matter had you written x*++y.
Using macros that mutate variables is a really bad idea: your code is undefined if you write, say MACRO(a, a), even if you attempt to beautify your macro by enclosing the arguments in parentheses.
It's a post increment operator. That means that the current values is retrieved and used in computations and then the variable's value is incremented. If you look at the value of c after that statement, it will be 5. (Note the operator applies to the variable, not the result of the computation).

Is the assignment expression after "sizeof" never executed? [duplicate]

This question already has answers here:
Why does sizeof(x++) not increment x?
(10 answers)
Closed 9 years ago.
e.g.
int a = 3;
int b = sizeof(++a);
int c = a;
Is c equal to 3 or 4 as the result?
Does the result depend on the specific compiler?
The specification states that the increment operator does NOT take affect when used within a sizeof operator.
This also makes sense from an abstract viewpoint. Specifically, the sizeof operator returns the number of bytes used by an object. While incrementing an integer or even a pointer does NOT change the size of that integer, the ++ operator would mis-lead a new programmer into thinking the size does change.
If interested look-up the topic "side-effects" for more discussion about this subject.

Confused with pre and post increment operator [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
hello I am learning basics of C programming language, recently i have studied about post and pre increment/decrement operators and also about lvalue and rvalue, the following program shows an error, lvalue required, according to me it should give a value of 6, Can anyone please explain why?
int main(){
int x = 8, y;
y = --x--;
printf("y=%d",y);
return 0;
}
Please explain, why is it so?
Well, let's see what is happening in --x--.
At first, post-decrement executes: --(x--).
(x--) = 7.
After that result of this operation is placed to the original structure:
--7 - doesn't make sense - thus you get lvalue required error
The statement y = --x--; will give you the following error on compilation in C. lvalue required. This is because the post decrement operator -- will return an rvalue after operating on the variable x. So there are no lvalue to perform the pre decrement operator -- afterwards.
But this is one point where C and C++ differs. In C the following statement will also give you the same error lvalue required.
y = (--x)--;
But in C++ the statement y = (--x)--; will compile fine and the value of y is 7. Because unlike C, C++ returns an lvalue after performing the pre decrement operator on variable x.
L Value is left operand of assignment operator which should refer to memory location.As explained by #Pavel your Lvalue is becoming a value not object so you are getting error.
--x means x = x-1 but in your case it is becoming --7 which will be equivalent to 7 =7-1 which is definitely not valid expression.
Other than this more than one operation on same variable without any sequence point in between, results undefined behaviour.
C order of operations specifies that postfix operators have precedence over the prefix operators. The -- postfix operator returns the current value (an rvalue) of the operand and then decrements the operand. Then the prefix decrement operator would be applied...but the decrement/increment operators need lvalue operands since they by definition modify their operands. So, as the compiler says, an lvalue is required.
You should not use it at a time because you will not understand the behavior of compiler. So, you need to guide your code so that they will forced to do what you like.
Now come to your point. If you want to decrease the value by one you can use a-- or --a. They will do the same. If a = 5 and you use b=a-- you will get b = 5 and a = 4 where if you use b=--a you will get b = 4 and a = 4 in --a the value is assigned immediately and in a-- value will be assigned after statement is complete. Hope you are clear.
L value required error shown when it doesn't find any suitable variable where it can be assigned.

Output of C program [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
int a[]={10,20,30,40};
int x=0;
int v=a[++x]+ ++x + a[--x];
printf("%d",v);
What will be the output of this program??
Completely confused with the output. No way it is going to be done according to my operator precedence knowledge.
According to me, in this expression Array subscripting [] has highest precendence and should be executed first. so both [] should be executed first from left to right. In this case value of x will increment first, then decrement and finally come back to 0. so expression will become int v=a[0] + ++x + a[0]. Then the pre increment is having highest precedence and it will be incremented to 1. so our expression will become int v=a[0]+1+a[0]. so final output will be 21.
But this is not the case. I have checked on different compiler implementations and no one prints 21.
I am much surprised because the value printed is 43, which is no where understandable to me. That's why I want someone to help me understand and come to the result 43.
The link which others have suggested is using only increment and same rvalue and lvalue cases. But this is somewhat different and not clear. I tried to contruct expression tree for this and solve but 43 is no where in scope.
Output of this code:
int v=a[++x]+ ++x + a[--x];
is undefined and it depends on the compiler implementation.

Resources