This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
When i run this code the var "tarkiz" remains 1.
Could anyone please explains to me why this happens? isn't it supposed to perform the assignment first (tarkiz = tarkiz) and then increment the value to be 2 instead of 1?
#include <iostream>
using namespace std;
int main() {
// your code goes here
int tarkiz = 1;
tarkiz = tarkiz++;
cout<<tarkiz<<endl;
return 0;
}
tarkiz = tarkiz++; is undefined behavior. To fix it write tarkiz++; instead.
See this explanation for details.
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
So here's the question. Find the output for the following code.
perform in c++
#include<iostream>
using namespace std;
int main()
{
int i=2;
cout<<i++<<i<<i++<<i;
cout<<i;
}
This concept is based on post/pre increment operator. Based on this concept I predicted the output as 23344. Like I expected it was correct when I tried debugging this code. But without debugging i am getting the output as 34244.
Is this even possible? BTW I tried this on Dev-C++ 5.11. Thanks :)
Your construction invokes Undefined Behavior.
See Undefined behavior in c/c++: i++ + ++i vs ++i + i++ and Why are these constructs (using ++) undefined behavior?
#include<iostream>
using namespace std;
int main()
{
int i=2;
//cout<<i++<<i<<i++<<i; // UB!
cout<<i++;
cout<<i;
cout<<i++;
cout<<i;
return 0;
}
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I know the concept of post increment but how does it apply to the follow? The output of t is 10. How to explain the undefined behavior?
int a = 2;
int b = 3;
int t;
t = a++ * (a+b);
That is undefined behavior you have there.
This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main()
{
const int kiNum = 100;
int* ptr = const_cast<int*>(&kiNum);
*ptr = 200;
cout<<"kiNum: "<<kiNum; // The value still prints 100 on the console??
return 0;
}
output:
kiNum = 100
In the above code snippet , i am trying to change the value of a const integer, after const_cast and then change the value at the address, but the console still prints the old value (i am using visual studio 2012)
Writing to something which is defined as const is undefined (assuming you cast away the const of course).
http://en.cppreference.com/w/cpp/language/const_cast
It's a pretty accurate website. If you have issues with a language feature its always worth looking up there IMHO.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.
This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 8 years ago.
I have a piece of program as shown below:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const int i = 100;
cout<<"const i::"<<i<<endl;
const_cast<int&>(i) = 200;
cout<<"const i after cast::"<<i<<endl;
return EXIT_SUCCESS;
}
But the value of i is still 100. Aren't const_cast supposed to change the value of i?
Constant data is, by its very definition, constant, and attempting to change constant data leads to undefined behavior.