[SOLVED] changing variable values with unary vs arithmetic operators
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
changing variable values with unary vs arithmetic operators
wondering if anyone can explain why unary ops vs, arithmetic ops affect variables in main() in different ways in the following function call:
Code:
#include <stdio.h>
void displayParam(int a)
{
printf("\nInt variable a: %d\n", a);
}
int main(void)
{
int a = 0;
displayParam(a+1);
printf("val of a in main (unchanged from initialization) is %d\n",a); //a still = 0:
displayParam(a++);
printf("val of a in main (now changed from initialization val) is %d\n",a); //a now = 1
return 0;
}
wondering if anyone can explain why unary ops vs, arithmetic ops affect variables in main() in different ways in the following function call:
Code:
#include <stdio.h>
void displayParam(int a)
{
printf("\nInt variable a: %d\n", a);
}
int main(void)
{
int a = 0;
# this will be equal to displayParam(0+1), a will remain 0
displayParam(a+1);
printf("val of a in main (unchanged from initialization) is %d\n",a); //a still = 0:
# this will be equal to displayParam(0), a will be incremented - after the call
displayParam(a++);
printf("val of a in main (now changed from initialization val) is %d\n",a); //a now = 1
# you can also try displayParam(++a)
return 0;
}
changing variable values with unary vs aritmetic operators
Pam;
What is unclear is, I thought, already stated in the code comments. Why is it that val a, originally initialized =0, is NOT changed to val a = 1 in main() (a is still = 0), but if passed as a++, the value is incremented in main() to val a = 1?
a+1 will return with the value a+1 and will not modify a.
a++ will return the original value of a and will increment a afterwards.
This is how c works, this is the syntax.
so in
displayParam(a+1);
1 will be used and a will remain 0, in
displayParam(a++);
0 will be used and after the call to displayParam a will be incremented.
unary operator usually modifies the variable, binary operators usually take only the values and leave the variables intact.
Thanks to both Pan64 & Johnsfine for the quick replies.
It seems strange that instructional manuals always seem to state that a++ is a simple equivalent of a+1, yet there is such an obvious difference in the way they actually operate that they neglect to point out!
Thanks to both Pan64 & Johnsfine for the quick replies.
It seems strange that instructional manuals always seem to state that a++ is a simple equivalent of a+1, yet there is such an obvious difference in the way they actually operate that they neglect to point out!
I've never seen a manual state that. In fact, they always seem to state that a++ is equivalent to a=a+1, which is more or less correct (at least when it's used on its own line and not embedded in some other call).
RE: reply by johnsfine
yes, suicidaleggroll's comment is more explicit than what I wrote (but is exactly what I meant). As he mentioned, I now realize that it is "more or less correct (at least when it's used on its own line and not embedded in some other call)."
It's the part about being embedded in some other call that confused me.
Last edited by atlantis43; 05-15-2013 at 09:17 AM.
# this is pre-increment and affects a before its used in a calc
++a;
# this is post-increment, as explained above
a++;
As above, used on their own(!), they are equiv to 'a=a+1', BUT inside a fn call or calc (eg your code), the difference between pre & post increment becomes important (to put it mildly ).
You should play around with these to get it clear in your head; see also 'a+=1'.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.