LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   changing variable values with unary vs arithmetic operators (https://www.linuxquestions.org/questions/linux-newbie-8/changing-variable-values-with-unary-vs-arithmetic-operators-4175462092/)

atlantis43 05-15-2013 07:56 AM

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;
}


pan64 05-15-2013 08:02 AM

Quote:

Originally Posted by atlantis43 (Post 4951679)
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;
}


What is unclear?

atlantis43 05-15-2013 08:16 AM

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?

pan64 05-15-2013 08:22 AM

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.

johnsfine 05-15-2013 08:25 AM

Quote:

Originally Posted by atlantis43 (Post 4951703)
if passed as a++, the value is incremented in main() to val a = 1?

That is the defined behavior of the ++ operator. It changes the value of its source.

atlantis43 05-15-2013 08:46 AM

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!

johnsfine 05-15-2013 08:59 AM

Quote:

Originally Posted by atlantis43 (Post 4951726)
It seems strange that instructional manuals always seem to state that a++ is a simple equivalent of a+1

I think you misunderstood what you read.

That is an unlikely error for any C manual.

Are any of the manuals that you think said that online? If you provide a link, someone may be able to explain what the text really means.

suicidaleggroll 05-15-2013 09:03 AM

Quote:

Originally Posted by atlantis43 (Post 4951726)
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).

atlantis43 05-15-2013 09:11 AM

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.

chrism01 05-15-2013 09:05 PM

Actually, re '++' it depends when you use it
Code:

# 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'.


All times are GMT -5. The time now is 10:47 PM.