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

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.

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 can we use only four operators (+,-,++,--) in pointer arithmetic? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Why can we use only four operators (+, -, ++, --) in pointer arithmetic? Does it have any relation with inbuilt hardware since it is dealing with addresses?
Since an array uses relative addressing, in that we increment the address to perform that operation, I think this might be one of the reasons for using those 4 operators alone.
Why can we use only four operators (+, -, ++, --) in pointer arithmetic?
Because the language was built that way. The most common operation is iteration, which is probably why those operators are allowed.
As for rationales behind why the C language was designed this way, there's not much to be found. I checked the C99 rationale regarding pointer arithmetic, but it is mostly concerned about the result of pointer arithmetic overflowing and does not mention why/when pointer arithmetic makes sense.
Does it have any relation with inbuilt hardware since it is dealing with addresses?
No, it has nothing to do with that. Addresses are just numbers. You can always cast any pointer to uintptr_t and then use it as any other integer.
You can use any operator, as long as the result will be int. For example:
int A[10], i=5;
int *p;
p = A + (i/2);
Simple because C was designed that way.
+,- can be simply used to add or subtract two pointers respectively (value at address only). While on the other hand increment and decrement operators i.e ++ and -- can be used to increment or decrement the pointer.Remember the data type of pointer plays an important role in these two(++,--) operators.
For example if you define an pointer of type *int then ++ will increment the memory address by 2 (since the size of int in most compilers is 2 ) and so same is with the -- operator as it will decrease it by a 2.

Is unary minus equivalent to binop minus? [duplicate]

This question already has answers here:
C: unary minus operator behavior with unsigned operands
(4 answers)
Closed 7 years ago.
My C compiler gave a warning when using unary minus on an unsigned value, so I fixed the warning by doing a subtraction from 0 instead.
Now I wonder if the current code is equivalent to the original one:
uint32_t a, b; // assume b is initialized and non-zero
a = -b % b; // old code
a = (0-b) % b; // current code
My question is: for the same values of b will both lines of code yield the same result for a?
Usually, yes, unless on your platform uint32_t would be a narrow type. Then it would first be promoted to int and the negation would be made in that type.

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.

Using ++ inside the sizeof keyword [duplicate]

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.

Resources