Justify the output in the program

K

kaushalgoa

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
 
R

Richard Heathfield

(e-mail address removed) 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.)
 
A

Ancient_Hacker

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.
 
K

kaushalgoa

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
!!!
 
K

Kenneth Brody

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".

[...][...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
T

Thomas J. Gritzan

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.

Also please don't top-post.
 
R

Richard Heathfield

(e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top