help me! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

help me!

For example, y= 5. What will be the value of x and y after y=++x and y=x++?;

23rd Aug 2020, 4:34 PM
Estiaque A. Evan
Estiaque A. Evan - avatar
26 Answers
+ 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
23rd Aug 2020, 4:47 PM
Rohit
+ 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.
23rd Aug 2020, 5:02 PM
Avinesh
Avinesh - avatar
+ 6
Avinesh Right! Thank you ☺️ (I forgot about the EVIL garbage value) 😅
23rd Aug 2020, 5:06 PM
Rohit
+ 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) 😅
23rd Aug 2020, 5:12 PM
Rohit
+ 4
rkk Ohh yaa! You are ryt! I forgot that they can be initialised 😂
23rd Aug 2020, 4:53 PM
Namit Jain
Namit Jain - avatar
+ 3
rkk thank you so much! you just saved my 10 marks in exam👍❤
23rd Aug 2020, 4:56 PM
Estiaque A. Evan
Estiaque A. Evan - avatar
+ 3
Estiaque A. Evan I would be much happier if you have learnt something new from it. Anyway, you're welcome and happy learning.
23rd Aug 2020, 4:58 PM
Rohit
+ 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👍
23rd Aug 2020, 5:03 PM
Estiaque A. Evan
Estiaque A. Evan - avatar
+ 3
The problem is not properly defined x should be initialised with a specific value 😶
24th Aug 2020, 2:57 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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
24th Aug 2020, 2:48 PM
A Coder
A Coder - avatar
+ 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
25th Aug 2020, 10:24 AM
Bits!
+ 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; }
25th Aug 2020, 10:25 AM
I C
I C - avatar
+ 2
There is no value of x!
23rd Aug 2020, 4:43 PM
Estiaque A. Evan
Estiaque A. Evan - avatar
+ 2
x = 5 x++ = 6 ++ equal to '+1'
23rd Aug 2020, 5:03 PM
</𝚜𝚌𝚘𝚛𝚙𝚒𝚘𝚗>
</𝚜𝚌𝚘𝚛𝚙𝚒𝚘𝚗> - avatar
+ 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;
25th Aug 2020, 10:38 AM
Gordon Okoth Agola
Gordon Okoth Agola - avatar
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 )
25th Aug 2020, 5:38 AM
Raghad Albarghash
Raghad Albarghash - avatar
0
++x pre increment will store the value after increasing x++ post increment will store the value before increasing. That's it.
25th Aug 2020, 8:16 AM
Rajnish Kush
Rajnish Kush - avatar
0
Y will be 5 after ++x and it will be 6 after x++
25th Aug 2020, 8:20 AM
Mayank Negi
Mayank Negi - avatar
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.
25th Aug 2020, 11:43 AM
SRINJOY CHAKRAVARTY
SRINJOY CHAKRAVARTY - avatar
0
1. What is x???
25th Aug 2020, 11:54 AM
Евгений