Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org please explain the output of the printf
 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

 10-09-2008, 05:51 AM #1 dina3e Member   Registered: Mar 2008 Location: Bangalore Distribution: Enterprise Red Hat linux Posts: 98 Rep: please explain the output of the printf i have compiled this program but i am expecting the answer is 32 because first all the pre-increment of 'a' after that the value replacement . so the excepted result .. but the outcome is 27. #include int main(){ int a,b; a=5; a= a + ++a + ++a + ++a; printf("value of a:%d \n", a ); return 0 ; } can any body explain the peculiar behavior of the printf () in pre-increment and the post - increment of the operator. and important thing is that how all these things are maintained in the stack and flow of the increment of the value .
 10-09-2008, 07:14 AM #2 sohail0399 Member   Registered: Oct 2008 Location: Pakistan, Islamabad Distribution: CentOS, Fedora, Solaris Posts: 154 Rep: solution first a and ++a is solved from left, it is preincriment so first a becomes 6. so the answer is 12. then 12 + ++a will be solved, due to preincriment a becomes 7 and answer becomes 12+7=19 then 19 + ++a will be solved and now a becomes due to another incciment 8, so 19+8=27. in this way computer calculates the value.
 10-09-2008, 07:22 AM #3 Nightfish Member   Registered: Sep 2008 Posts: 36 Rep: what I don't understand is your logic of getting the 32 answer. a= 6(preincrement ahead) + 6(++a) = 12 12 + 7(++a preincrement so we get another 1)=19 19 + 8(++a) = 27. Fedora Development Last edited by Nightfish; 10-22-2008 at 01:52 PM.
10-09-2008, 07:52 AM   #4
H_TeXMeX_H
LQ Guru

Registered: Oct 2005
Location: \$RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep:
Quote:
 Originally Posted by dina3e a= a + ++a + ++a + ++a;
Please, don't tell me you would ever ever EVER write anything like this in a real program. I'm sure you've been told that you should never use increments in combination with arithmetic, some results are actually undefined.

10-09-2008, 08:27 AM   #5
linuxman8786
Member

Registered: Sep 2008
Posts: 43

Rep:
Quote:
 Originally Posted by Nightfish what I don't understand is your logic of getting the 32 answer. a= 6(preincrement ahead) + 6(++a) = 12 12 + 7(++a preincrement so we get another 1)=19 19 + 8(++a) = 27.
I think it is true result, you should note with a++ and ++a, they are different

Last edited by Tinkster; 10-30-2010 at 04:49 PM.

10-14-2008, 12:21 PM   #6
dina3e
Member

Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep:
Quote:
 Originally Posted by Nightfish what I don't understand is your logic of getting the 32 answer. a= 6(preincrement ahead) + 6(++a) = 12 12 + 7(++a preincrement so we get another 1)=19 19 + 8(++a) = 27.

but when i am trying to solve this

a = ++a + ++a;

answer is 14 how this result is possible.

Plz suggest how all these thing are done in stack. Mean how push and pop operation happen during the ++ (pre and post increment operation)

10-14-2008, 01:26 PM   #7
CRC123
Member

Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep:
Quote:
 Originally Posted by dina3e ok , fine i agreed with your answer , but when i am trying to solve this a = ++a + ++a; answer is 14 how this result is possible. Plz suggest how all these thing are done in stack. Mean how push and pop operation happen during the ++ (pre and post increment operation)
This sounds like a wacky question professors give you to try and make you really understand how something works (the compiler in this case and the way it uses a stack to execute mathematical expressions). I think a useful and interesting observation in this example is that the compiler doesn't store the value of a variable in the stack, instead it stores a pointer to a variable in the stack.

There are a few different ways to use a stack to correctly evaluate a mathematical expression while following the orders of operation.

I will do an example of a version the scans the expression from LEFT to RIGHT (this would be easier with a white board to visually represent the stack, but I will try with words. You should go along with this and create your own 'visualized' stack of what's going on on a piece of paper).
Code:
```a=5;
a= a + ++a + ++a + ++a;```
1. LEFT to RIGHT way
-A pointer to the variable 'a' is pushed on the stack
-An '=' is pushed on the stack
-A pointer to 'a' is pushed on the stack
-A '+' is pushed on the stack
-the operation '++a' is carried out on 'a' and 'a' now equals 6
-the '+' is popped off the stack
-the 'a' is popped off the stack
-the expression 'a' + 'a' is evaluated to 12 (since a=6)
-the VALUE 12 is pushed onto the stack
-A '+' is pushed onto the stack
-the operation '++a' is carried out on 'a' and 'a' now equals 7
-the '+' is popped off the stack
-the VALUE 12 is popped off the stack
-the expression 12 + 'a' is evaluated to 19(since a=7)
-the VALUE 19 is pushed on the stack
-A '+' is pushed on the stack
-the operation '++a' is carried out on 'a' and 'a' now equals 8
-the '+' is popped of the stack
-the '19 is popped off the stack
-the expression 19 + 'a' is evaluated to 27(since a=8)
-the '=' is popped off the stack
-the 'a' is popped off the stack
-the operation a = 27 is evaluated
-a=27

This is one way of doing it. I have no clue how your compiler (or mine for that matter) actually does it. But this is one way it could do it.

 10-14-2008, 01:49 PM #8 CRC123 Member   Registered: Aug 2008 Distribution: opensuse, RHEL Posts: 374 Blog Entries: 1 Rep: The funny thing is, java will give you 26 (probably because it always stores values instead of pointers)! Who's right, C++ or java!! lol.
 10-14-2008, 01:59 PM #9 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: I told you, the real answer is undefined, so different compilers will give different results, as will different languages.
10-14-2008, 02:08 PM   #10
CRC123
Member

Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep:
Quote:
 Originally Posted by H_TeXMeX_H I told you, the real answer is undefined, so different compilers will give different results, as will different languages.
I think I have to agree with you now. Here's a link to read.

Look at the bottom section.

 10-14-2008, 02:24 PM #11 kavinchat LQ Newbie   Registered: Jul 2008 Posts: 22 Rep: Hv you checked the same in c++? Hv you checked the same in c++?. . .
10-14-2008, 02:40 PM   #12
H_TeXMeX_H
LQ Guru

Registered: Oct 2005
Location: \$RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep:
Quote:
 Originally Posted by CRC123 I think I have to agree with you now. Here's a link to read. Look at the bottom section.
ahh, thank you, if someone refused to believe me I was going to post a paragraph out of my C programming book that said the same thing.

 10-14-2008, 02:43 PM #13 kavinchat LQ Newbie   Registered: Jul 2008 Posts: 22 Rep: Hv this look out i think u had still not understood the basic concept of a c compiler... "+" operator evaluates values from "left to right".. you had avaluated from "right to left" to get the answer... only unary operators are executed from right to left....
10-14-2008, 02:56 PM   #14
CRC123
Member

Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep:
Quote:
 Originally Posted by kavinchat i think u had still not understood the basic concept of a c compiler... "+" operator evaluates values from "left to right".. you had avaluated from "right to left" to get the answer... only unary operators are executed from right to left....
who are you referring to in this case?

 10-15-2008, 12:03 PM #15 dina3e Member   Registered: Mar 2008 Location: Bangalore Distribution: Enterprise Red Hat linux Posts: 98 Original Poster Rep: @ CRC123 What you explain is great...i am getting some what more information about ++ operator in stack.

 Tags program

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post shadow5277 Programming 2 06-22-2007 03:46 AM Indiestory Programming 2 01-06-2007 08:52 AM schneidz Programming 5 09-20-2005 09:59 AM pateldenish Programming 1 02-24-2005 10:02 AM eastsuse Linux - General 1 03-09-2004 08:26 PM

All times are GMT -5. The time now is 04:06 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -