Computing.Net > Forums > Programming > - - prefix in C??

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

- - prefix in C??

Reply to Message Icon

Name: Dado
Date: October 5, 2003 at 13:23:21 Pacific
OS: Me
CPU/Ram: 512MB
Comment:

I have a question about -- prefix. It subtracts 1 from the variable, but when I put --sub_total for a new variable (newsub=--sub_total), it subtracts 1 for newsub and my sub_total also. Why does it do this? When I take the line newsub=--sub_total, my sub_total is good, and as soon as I introduce a --prefix in another equation, it affects the original sub_total too.
Help appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: October 5, 2003 at 22:33:25 Pacific
Reply:

The prefix minus decrements the variable and then the variable's (new) value is used in the rest of the statement.

subTotal = 10;
newsub = --subTotal;

subTotal is decremented to 9 and then the new value of subTotal (9) is assigned to newsub. So both newsub and subTotal will be 9.


The postfix minus will use the value of the variable in the statement and then decrement the variable.

subTotal = 10;
newsub = subTotal--;

The value of subTotal (10) is assigned to newsub and then subTotal is decremented. So, newsub will be 10 and subTotal 9.


Hope this helps.



0

Response Number 2
Name: Dado
Date: October 6, 2003 at 13:57:25 Pacific
Reply:

Thank you for the reply. What if I don't want to decrement subTotal, but only decrement newsub by 1 using subTotal, can this method still be used? In other words, in both of your examples, subTotal is decremented, can subnew be the only one decremented, and can you leave subTotal the same using this method?


0

Response Number 3
Name: Dado
Date: October 6, 2003 at 18:46:43 Pacific
Reply:

I've found a way how to do it, no need to worry about it.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Perl Integer FFFFFFFF Java Execution Problem



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: - - prefix in C??

Pointers in C/C++ www.computing.net/answers/programming/pointers-in-cc/876.html

what does this func do in C www.computing.net/answers/programming/what-does-this-func-do-in-c/9528.html

Null Character in C? (NOT '\0') www.computing.net/answers/programming/null-character-in-c-not-0/3201.html