LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 10-09-2008, 05:51 AM   #1
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
Smile 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<stdio.h>
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 .
 
Old 10-09-2008, 07:14 AM   #2
sohail0399
Member
 
Registered: Oct 2008
Location: Pakistan, Islamabad
Distribution: CentOS, Fedora, Solaris
Posts: 154

Rep: Reputation: 23
Smile 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.
 
Old 10-09-2008, 07:22 AM   #3
Nightfish
Member
 
Registered: Sep 2008
Posts: 36

Rep: Reputation: 15
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.
 
Old 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: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by dina3e View Post
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.
 
Old 10-09-2008, 08:27 AM   #5
linuxman8786
Member
 
Registered: Sep 2008
Posts: 43

Rep: Reputation: 15
Quote:
Originally Posted by Nightfish View Post
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.
 
Old 10-14-2008, 12:21 PM   #6
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Nightfish View Post
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.

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)
 
Old 10-14-2008, 01:26 PM   #7
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by dina3e View Post
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.
 
Old 10-14-2008, 01:49 PM   #8
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
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.
 
Old 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: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
I told you, the real answer is undefined, so different compilers will give different results, as will different languages.
 
Old 10-14-2008, 02:08 PM   #10
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by H_TeXMeX_H View Post
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.
 
Old 10-14-2008, 02:24 PM   #11
kavinchat
LQ Newbie
 
Registered: Jul 2008
Posts: 22

Rep: Reputation: 15
Question Hv you checked the same in c++?

Hv you checked the same in c++?. . .
 
Old 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: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by CRC123 View Post
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.
 
Old 10-14-2008, 02:43 PM   #13
kavinchat
LQ Newbie
 
Registered: Jul 2008
Posts: 22

Rep: Reputation: 15
Lightbulb 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....
 
Old 10-14-2008, 02:56 PM   #14
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by kavinchat View Post
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?
 
Old 10-15-2008, 12:03 PM   #15
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
@ CRC123
What you explain is great...i am getting some what more information about ++ operator in stack.
 
  


Reply

Tags
program


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
Printf and ctime question about output when used together. shadow5277 Programming 2 06-22-2007 03:46 AM
No output from printf Indiestory Programming 2 01-06-2007 08:52 AM
printf: postion cursor to output columns schneidz Programming 5 09-20-2005 09:59 AM
Output problems with printf pateldenish Programming 1 02-24-2005 10:02 AM
printf output with commas eastsuse Linux - General 1 03-09-2004 08:26 PM

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

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