C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 12th, 2012, 01:57 AM
varshc1 varshc1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 varshc1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 59 sec
Reputation Power: 0
Question Usage of increment operator in output

#include<stdio.h>
void main()
{
int i;
i=3;
printf("%d%d",i,i++);
}
output of this is displayed as 43 y???

Reply With Quote
  #2  
Old November 12th, 2012, 03:50 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
Originally Posted by varshc1
Code:
#include<stdio.h>
void main()
{
	int i;
	i=3;
	printf("%d%d",i,i++);
}

output of this is displayed as 43 y???


Because using and updating an object without an intervening sequence point is undefined behaviour.

Basically an object is a variable.
Very basically a sequence point is the semicolon at the end of a statement.

In your printf("%d%d", i, i++) statement, you use the value of i and update it in the same expression originating UB.

You never ever want to write programs that contain UB!

So ... don't do that. Write your program as one of the following alternate options:

Code:
#include<stdio.h>
int main(void)
{
	int i, j;
	i = 3;
        j = i++;
	printf("%d%d\n", i, j);
}


Code:
#include<stdio.h>
int main(void)
{
	int i;
	i = 3;
	printf("%d%d\n", i, i);
        i++;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Usage of increment operator in output

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap