LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 08:56 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 238

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, 09:02 AM   #2
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,571

Rep: Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468
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, 09:16 AM   #3
atlantis43
Member
 
Registered: Feb 2013
Posts: 238

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, 09:22 AM   #4
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,571

Rep: Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468
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, 09:25 AM   #5
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,186

Rep: Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136
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, 09:46 AM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 238

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, 09:59 AM   #7
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,186

Rep: Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136
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, 10:03 AM   #8
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,455

Rep: Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244
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, 10:11 AM   #9
atlantis43
Member
 
Registered: Feb 2013
Posts: 238

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 10:17 AM.
 
Old 05-15-2013, 10:05 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,401

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:22 AM
Need to define arithmetic functions and evaluate with different values david.karr Linux - Software 2 02-17-2009 12:19 PM
Values like VARIABLE[i] ? BlueSpirit Programming 5 04-27-2007 02:35 PM
arithmetic operators in Kylix3 (C++) herbie_52 Programming 2 05-23-2003 08:36 AM


All times are GMT -5. The time now is 12:10 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration