LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2010, 09:55 AM   #1
shariefbe
Member
 
Registered: Aug 2008
Location: Tiruchirappalli, India
Distribution: UBUNTU 11.04
Posts: 368

Rep: Reputation: 31
C programming increment and decrement doubt


Hello friends,
I have an big doubt in increment and decrement in c programming
i am trying to compile the below code in gcc
Code:
#include<stdio.h>
main()
{
int a=10,b=12;

printf("\n%d\n%d\n%d\n%d\n%d\n",a++,++a,a--,--a,a);
}
and i am getting the below output
Code:
9
10
9
10
10
i am very much confused. how it is parsing and how i will get this output?please explain me
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-26-2010, 09:59 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
It is not correct to change the same variable more than once without a "sequence point".

Nothing in the C standard tells the compiler the sequence in which your a++ and ++a are executed. So the results are undefined.
 
2 members found this post helpful.
Old 05-26-2010, 10:38 AM   #3
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
oh what fun, do before, do after...

Code:
#include<stdio.h>
main()
{
	int a=10;
	printf("%d\n",a++);
	printf("%d\n",++a);
	printf("%d\n",a--);
	printf("%d\n",--a);
	printf("%d\n",a);
	
	printf("\n");
	
	a = 10;
	printf("%d\n",++a);
	printf("%d\n",a++);
	printf("%d\n",--a);
	printf("%d\n",a--);
	printf("%d\n",a);
}
resulting in...

Code:
10
12
12
10
10

11
11
11
11
10
I love this second part of the example...
 
Old 05-26-2010, 10:52 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Code:
#include<stdio.h>

int
main(int argc, char *argv[])
{
  int a=10,b=12;
  /* WRONG: the evaluation order for this expression is UNDEFINED! */
  printf("\n%d\n%d\n%d\n%d\n%d\n",a++,++a,a--,--a,a);
  return 0;
}
Johnsfine is absolutely correct. Different compilers (or even different versions of the SAME compiler) might give you different results; any of which might be different from what you expect.

Here are two more links that might help:

http://www.daniweb.com/forums/thread100183.html
http://c-faq.com/expr/index.html

'Hope that helps .. PSM

Last edited by paulsm4; 05-26-2010 at 10:54 AM.
 
  


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
How to achieve atomic increment/decrement in Linux silentray Programming 11 01-14-2010 06:33 AM
programming doubt vivasaayi Programming 1 01-02-2009 04:56 AM
One more C Programming Doubt indian Programming 4 09-07-2004 01:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 PM.

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