Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple C question

Posted on 2004-08-05
6
Medium Priority
?
224 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

971 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