Exforsys

Online Training

Help in Output

This is a discussion on Help in Output within the C and C++ forums, part of the Programming Talk category; My friend questioned me the output of the below program #include <stdio.h> main() { int a[] = { 1, 4, ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-13-2007, 08:13 AM
Senior Member
 
Join Date: Apr 2006
Posts: 125
ashlee is on a distinguished road
Help in Output

My friend questioned me the output of the below program

#include <stdio.h>
main()
{
int a[] = { 1, 4, 8, 5, 1, 4 };
int *s,b;
s = a + 4;
b = s - a;
printf("%d",b);
}

I told the output as 8.But he told me its wrong and asked me to try. Kindly someone help me to solve this problem.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-16-2007, 05:52 PM
Senior Member
 
Join Date: Apr 2006
Posts: 141
Adrian is on a distinguished road
Yes as your friend said your answer was wrong. The output is 4.Its simple. Consider the base address or starting address of a as 1000. So you would have 1000+4=1004 stored in s and again
b=s-a
gives
b=1004-1000=4 and b is outputted which gives the output as 4.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Different Output cyrus C and C++ 3 06-02-2007 02:04 AM
Puzzled on Output Adrian C and C++ 2 05-24-2007 04:30 PM
To get Output Rahulbatra C and C++ 1 05-10-2007 09:23 AM
Amazing Output cyrus C and C++ 1 05-08-2007 04:37 PM
C Programming - Managing Input and Output Operations JobHelper Career Advice 0 04-15-2006 08:30 AM


All times are GMT -4. The time now is 06:09 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.