LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-15-2013, 07:56 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
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;
}
 
Old 05-15-2013, 08:02 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by atlantis43 View Post
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?
 
Old 05-15-2013, 08:16 AM   #3
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
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?
 
Old 05-15-2013, 08:22 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
1 members found this post helpful.
Old 05-15-2013, 08:25 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by atlantis43 View Post
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.
 
1 members found this post helpful.
Old 05-15-2013, 08:46 AM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
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!
 
Old 05-15-2013, 08:59 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by atlantis43 View Post
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.
 
Old 05-15-2013, 09:03 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by atlantis43 View Post
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).
 
Old 05-15-2013, 09:11 AM   #9
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-15-2013, 09:05 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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'.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Strange behaviour in bash comparison when variable contains arithmetic operator michael.wegemer Programming 3 10-19-2010 05:22 AM
Need to define arithmetic functions and evaluate with different values david.karr Linux - Software 2 02-17-2009 11:19 AM
Values like VARIABLE[i] ? BlueSpirit Programming 5 04-27-2007 01:35 PM
arithmetic operators in Kylix3 (C++) herbie_52 Programming 2 05-23-2003 07:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration