Solved

Simple C question

Posted on 2004-08-05
6
201 Views
Last Modified: 2010-04-15
hi experts,

below is a very simple C code,
int x=4,y,z;
y=--x;
z=x--
printf ("\n%d %d %d ",x,y,z);

i got the output as 2, 3, 3. I am just wondering how did i get 2 for x. If it is because of --x and x--, then how i got z=3?
Could anyone explain this please. i appreciate the help. and thanks in advance.
considor, novice in C so pls explain in a simple way.
0
Comment
Question by:deepthiji
  • 3
  • 3
6 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11729259
Hi

It's because x-- sets z to equal x which is three, then decreases x by 1 so x is now 2

Regards,
Zyloch
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 11729267
To make it more clear:

y=--x;       //because you're using -- before x, it sets y to x-1, so y equals 3. x also equals 3 now.
z=x--;       //because you're using -- after x, it sets z to equal x, which is 3, then decreases x, so x=2

Regards
0
 
LVL 3

Author Comment

by:deepthiji
ID: 11729332
so are you saying x--  = x and x-1. , what i am wondering is why the above equation did not give the value of x-- to z, it gave the value of x to z right?
thats why z=3,
So could you tell me how the compiler works? it wont check the whole equation?  little bit confused,

thanks for the quick reply
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Author Comment

by:deepthiji
ID: 11729378
if the whole value of (x--) would have gone to z, then z should be 2 right?
as you said it sets z=x first and then decrease x by one. when we write equation like this z=x--. are we supposed to get the whole value  of right had side to z? please explain, Sorry to ask this question . might be a foolish one.
thanks
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11729468
Don't worry.

The reason there is --x and x-- is not just for convenience. If it helps, just think of it from left to right.

z = x--;
z = --x;

The first line does does the expression on that line and THEN after that expression is complete (in this case setting z to x), it decreases x. The second one drecreases x BEFORE it assigns x to z.

This concept can get a little confusing at times. Think about it this way:

z=x --
z= --x
(don't write it this way, heh)

Even though you don't type it this way, it makes it clearer. In the first one, z=x first then does the --. The second one, z = --x

Regards,
${Zyloch}
0
 
LVL 3

Author Comment

by:deepthiji
ID: 11729502
Thanks a lot for the reply, sorry, for the Quick reply.
i started learnning C, couple of days back, hope i will get the hang of it when i learn more.
I understood about --x and x-- now i guess>)
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now