[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

doubts in program output!!!

Posted on 2010-01-05
19
Medium Priority
?
295 Views
Last Modified: 2012-05-08
actually the output of the following code is

product=10.50000,address=62205
prod=62205 value at addresss=-NAN

i want to know how this NAN pushed here? i thought the value 10.50000 should be printed!!

can any one clear my doubt???
#include<stdio.h>
#include<conio.h>
void main()
{
 int i=3;
 float f=3.50,*prod;
 float *multiply(int *,float *);
 clrscr();
 prod=multiply(&i,&f);
 printf("\nprod=%u value at address=%f",prod,*prod);
 getch();
}
float *multiply(int *ii,float *ff)
{
float product;
product=*ii**ff;
printf("\nproduct=%f address of product=%u",product,&product);
return(&product);
}

Open in new window

0
Comment
Question by:ganeshkumar_cse
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 8

Expert Comment

by:Jon500
ID: 26187807
Shouldn't

 printf("\nprod=%u value at address=%f",prod,*prod);

be

 printf("\nprod=%u value at address=%f",prod,&prod);

Regards,
Jon500
0
 

Author Comment

by:ganeshkumar_cse
ID: 26187824
Even though i changed this to &prod, it s giving output

as
product=10.50000,address=62205
prod=62205 value at addresss=32.00001
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 26187825
float f=3.50,*prod;
prod is decalred as pointer. In multiply function you return a pointer to a local variable.
Change it to:
float multyply(int i, float f)
you are not changing these i and f parameters in this function.
But the mistake is:
float *multiply(int *ii,float *ff)
{
float product;  product=*ii**ff;
printf("\nproduct=%f address of product=%u",product,&product);
return(&product);
}
 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 85

Expert Comment

by:ozo
ID: 26187834
float *multiply(int *ii,float *ff)
{
float product;
product=*ii**ff;
printf("\nproduct=%f address of product=%u",product,&product);
return(&product);
}

this is invalid, the lifetime of the auto variable  product is limited to the duration of the function.
Any attempt to refer to a varibale outside of its lifetime is undefined behavior.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26187837
I mean that:
float multiply(int ii, float ff)
{
float product;  
product=ii*ff;
printf("\nproduct=%f address of product=%u",product,&product);
return product)
}
so in the main:
float product;
product = muliply(3, 3.5);
 
0
 

Author Comment

by:ganeshkumar_cse
ID: 26187857
>> pgnatyuk:

Even though i changed , it s giving output

as
product=10.50000,address=62205
prod=62205 value at addresss=32.00001
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26187868
What you have instead of this line now?
printf("\nprod=%u value at address=%f",prod,*prod);
 
probably you need:
printf("\nprod=%u value at address=%x",prod, &prod);

 
0
 

Author Comment

by:ganeshkumar_cse
ID: 26187869
>>pgnatyuk:

i knew how to get the ouput!!! my question is why is the output coming so?
0
 
LVL 85

Expert Comment

by:ozo
ID: 26187876
What does the program look like after you changed?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26187882
? I do not understand.
Output from where? You mean why we response so fast? It's a morning time here, so we are full of energy. :)
0
 
LVL 85

Expert Comment

by:ozo
ID: 26187889
> why is the output coming so?
undefined behavior means
behavior, upon use of a nonportable or erroneous program construct or of erroneous data,
for which the International ISO C Standard imposes no requirements
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 26187906
An object whose identifier is declared with no linkage and without the storage-class
specifier static has automatic storage duration.
For such an object that does not have a variable length array type, its lifetime extends
from entry into the block with which it is associated until execution of that block ends in
any way.

If an object is referred to outside of its lifetime, the behavior is undefined.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26187909
I think you need something like that:
printf("prod=%f value at address=%x",prod, &prod);

if prod is float now.
0
 

Author Comment

by:ganeshkumar_cse
ID: 26187951
yeah guys . i got it clear now..
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26187967
Just nice. I answered and didn't get any point here.
 
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26303385
I think that the accepted solution here should be http:#a26187825
http:#a26187906 is the assisted solution.
Regards,
Pavel
0
 

Author Closing Comment

by:ganeshkumar_cse
ID: 31673352
fine guys
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question