+ 2
help me!
For example, y= 5. What will be the value of x and y after y=++x and y=x++?;
26 Respuestas
+ 13
Estiaque A. Evan
so there is no value for x which means
x = 0
y = 5
y = ++x; // ++x(means increment before assigning) makes x = 1 and then we assign it to y
(so now y = 1 and x = 1)
y = x++; // x++ makes the increment afterward so 1 is assign to y and later x itself becomes 2
(now y = 1 and x = 2)
________________________
this code is fine without x being initialize to anything here in SL C compiler .(by default x becomes 0 here in SoloLearn c compiler)
#include <stdio.h>
int main() {
int x, y = 5;
y = ++x;
printf("x=%d, y=%d\n",x,y);
y = x++;
printf("x=%d, y=%d",x,y);
return 0;
}
________________________
Read this to understand well.
https://www.sololearn.com/discuss/96907/?ref=app
+ 12
rkk just a small correction, if a variable is declared but not initialized then the value it stores is indeterminate. It is not 0 and depends completely on the compiler you are running the code on. It generally gets the value from the memory where the variable is stored.
+ 6
Avinesh Right! Thank you ☺️
(I forgot about the EVIL garbage value) 😅
+ 5
~ swim ~ I tried to oversimplified it mathematically. (when there's no apples in the basket, it's means 0 apples are there) 😅😁
You're right buddy. ☺️
(I forgot about the EVIL garbage value) 😅
+ 4
rkk Ohh yaa! You are ryt!
I forgot that they can be initialised 😂
+ 3
rkk thank you so much! you just saved my 10 marks in exam👍❤
+ 3
Estiaque A. Evan I would be much happier if you have learnt something new from it.
Anyway, you're welcome and happy learning.
+ 3
rrk, I'm a newbie to programming world! That's why I'm getting confused!
but, I promise I will learn it very quickly👍
+ 3
The problem is not properly defined x should be initialised with a specific value 😶
+ 3
since there is no value assign to x .
default value 0 is assign to x .
and value 5 assign to y(given)
++x => pre increment means first increment variable value by 1 then use the value.
x++ => post increment means first use the value then increment value by 1.
so y =++x here y value become 1.
and in y=x++ here y value is 1.
and value of x is 2.
for better understanding you can also see the code :)
https://code.sololearn.com/c34FdeG2zs36/?ref=app
+ 3
Nice question ✌🏽 BTW, I already had made an example on incremental operator types prefix and postfix. I represent my answer a web developer. Here it is⬇️⤵️
https://code.sololearn.com/WBlp4B5Go100/?ref=app
+ 3
Let y=5 and x =0
1, cout<<++x;
it increment and print the value by one that is #1
2, x++
The value is printed and then x is incremented.
Now, lets take one example in c++
(NB: the way it works is the same in many lags!)
//check and execute each one by one
#include<iostream>
using namespace std;
int x=0;
int y=5;
int main(){
cout<<x
<<x++
<<++x
<<endl;
return 0;
}
+ 2
There is no value of x!
+ 2
x = 5
x++ = 6
++ equal to '+1'
+ 1
Integer default value is 0, so if x is declared and not initialize we will have this in the memory
x=0;
y=5;
Pre-incrementing x the assigning it value to y, we will have
x=1;
y=1;
Post- incrementing x then assigning the value to y will give us
x=2;
y=1;
0
y = ++x then x = 4 (first add 1 to x the assign the value to y and since y = 5 then x mush be 4 )
y = x++ then x = 5 (first assign the value to why then add which means its already 5 )
0
++x pre increment will store the value after increasing
x++ post increment will store the value before increasing. That's it.
0
Y will be 5 after ++x and it will be 6 after x++
0
In y=++x, x's value, whatever value it has will be incremented by 1 first and that value will be stored in y as well as in x. And if u are writing in c then x will possess garbage value as there is no value of x mentioned above
And in y=x++, x's original value will be stored first in y(whatever value of y may be) and x's value will be incremented by 1 which will be stored in x.
0
1. What is x???