468,456 Members | 1,639 Online

# Justify the output in the program

int sum(int a, int b)
{
int c = a + b;
}

int main()
{
int p = 5, q = 10;
printf("%d, %d, %d\n", p,q,sum(p,q));

}

// ans 5, 10, 15

---------------------------------------------------------------------------------
int sum(int a, int b)
{
int c =0;
}

int main()
{
int p = 5, q = 10;
printf("%d, %d, %d\n", p,q,sum(p,q));

}

// ans 5, 10, 5

Jul 11 '06 #1
9 1396
ka********@gmail.com said:
int sum(int a, int b)
{
int c = a + b;
}
You have failed to return a value from this function...
>
int main()
{
int p = 5, q = 10;
printf("%d, %d, %d\n", p,q,sum(p,q));
....and yet you try to use the return value. Therefore, the behaviour of the
program is undefined. Any output, including "Here be dragons", is
justifiable from the implementation's point of view. You broke the rules,
so all bets are off.

(You also called a variadic function - printf - without a valid function
prototype in scope. That's another cause of undefined behaviour.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 11 '06 #2

kaushal...@gmail.com wrote:
int sum(int a, int b)
{
int c = a + b;
}

int main()
{
int p = 5, q = 10;
printf("%d, %d, %d\n", p,q,sum(p,q));

}
You're not returning any value from sum(), so you're going to get
undefined results.
You might get the sum, if the compiler just happens to use the same
register for addition as is expected to return int results.

Jul 11 '06 #3
When I compiled the 1st program in VC++ it gave the o/p as 5, 10, 15 .

But the o/p of 2nd Program in VC++ is 5, 10 , Garbage value .

I feel in both the cases the value should be garbage.
If I do

int sum(int a, int b)
{
int c = 2+7;
return;
}

int main()
{
int p=5, q=10;
printf("%d, %d, %d\n", p,q,sum(p,q));

}

// ans 5, 10 , 5 in gcc compiler.
I am confused . It should be unpredictable behaviour but o/p is same
!!!
Richard Heathfield wrote:
ka********@gmail.com said:
int sum(int a, int b)
{
int c = a + b;
}

You have failed to return a value from this function...

int main()
{
int p = 5, q = 10;
printf("%d, %d, %d\n", p,q,sum(p,q));

...and yet you try to use the return value. Therefore, the behaviour of the
program is undefined. Any output, including "Here be dragons", is
justifiable from the implementation's point of view. You broke the rules,
so all bets are off.

(You also called a variadic function - printf - without a valid function
prototype in scope. That's another cause of undefined behaviour.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 11 '06 #4
ka********@gmail.com schrieb:
When I compiled the 1st program in VC++ it gave the o/p as 5, 10, 15 .

But the o/p of 2nd Program in VC++ is 5, 10 , Garbage value .

I feel in both the cases the value should be garbage.
If I do

int sum(int a, int b)
{
int c = 2+7;
return;
}

int main()
{
int p=5, q=10;
printf("%d, %d, %d\n", p,q,sum(p,q));

}

// ans 5, 10 , 5 in gcc compiler.
I am confused . It should be unpredictable behaviour but o/p is same
!!!
It is unpredictable behaviour. That's why your are confused.

Just return a value in the function, then everything is ok.

--
Thomas
Jul 11 '06 #5
ka********@gmail.com wrote:
>
When I compiled the 1st program in VC++ it gave the o/p as 5, 10, 15 .

But the o/p of 2nd Program in VC++ is 5, 10 , Garbage value .

I feel in both the cases the value should be garbage.
It _is_ "garbage" in both cases, since in neither case does sum()
return a value. By pure coincidence, the "garbage" from the first
example is the sum of the two values passed.

Remember, one possible outcome of "undefined behavior" is "it does
what I wanted it to do".

[...]
int sum(int a, int b)
{
int c = a + b;
}
[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jul 11 '06 #6
ka********@gmail.com said:
When I compiled the 1st program in VC++ it gave the o/p as 5, 10, 15 .

But the o/p of 2nd Program in VC++ is 5, 10 , Garbage value .

I feel in both the cases the value should be garbage.
It's not up to you, though, and your feelings don't come into it. What
happens, happens. Undefined behaviour can manifest itself in an unlimited
number of ways, and if it chooses to give what you see as being a
disappointingly "correct" answer, well, that's what you get and you just
have to live with it. If it's any consolation, though, it might give a
different result next time you run it.

Or it might not. It might delay doing so until 18th March, 2019 (so that's
less than 13 years to wait). Or it might just wait a week or two. When
you're not looking.

You never can tell, with undefined behaviour. That's why we try to avoid it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 11 '06 #7
I got the point :) . Its unpredictable .

Ancient_Hacker wrote:
kaushal...@gmail.com wrote:
int sum(int a, int b)
{
int c = a + b;
}

int main()
{
int p = 5, q = 10;
printf("%d, %d, %d\n", p,q,sum(p,q));

}

You're not returning any value from sum(), so you're going to get
undefined results.
You might get the sum, if the compiler just happens to use the same
register for addition as is expected to return int results.
Jul 11 '06 #8
ka********@gmail.com writes:
Subject: Justify the output in the program
You could use nroff.
--
--Richard Heathfield
Jul 11 '06 #9
ka********@gmail.com writes:
I got the point :) . Its unpredictable .
(and most of the articles in this newsgroup) for details.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 11 '06 #10

### This discussion thread is closed

Replies have been disabled for this discussion.