Solved

Simple C question

Posted on 2004-08-05
6
202 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
Are your end users making ugly email signatures?

Have you left it up to your end users to create their own email signatures? Are they forgetting to add the company logo or using garish font colors? Take control and ensure all users have the same email signature.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use pointers 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.

911 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

17 Experts available now in Live!

Get 1:1 Help Now