Solved

Simple C question

Posted on 2004-08-05
6
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

717 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