Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   stuck (http://www.velocityreviews.com/forums/t742189-stuck.html)

Bill Cunningham 01-17-2011 07:12 PM

stuck
 
I have these two functions one calls the other one. I have tried
everything to debug them except a damn debugger. I have placed puts messages
at all the return and I still get two numbers on my screen. What's wrong
with this code? I will post the header, object file source and main() code.

#include <stdio.h>

double oper(char, double, double);
double vop(double);

Ok that's my declarations

#include "core.h"

double oper(char c, double x, double y)
{
if (c == 'a')
return vop(x + y);
else if (c == 's') {
return vop(x - y);
} else if (c == 'm') {
return vop(x * y);
} else if (c == 'd') {
return vop(x / y);
} else {
fputs("usage error\n", stderr);
return -1;
}
return 0;
}

double vop(double oper)
{
return printf("%.2f\n",oper);
}

Above here is the main code I compile in .o file. And this is the main
function:

#include "core.h"

int main()
{
double a;
a = oper('s', 5, 2);
return vop(a);
}

Everything works like I want it to except I am getting two numbers. The
second number is always the same whatever I pass to oper().

Bill



Ian Collins 01-17-2011 07:37 PM

Re: stuck
 
On 01/18/11 08:12 AM, Bill Cunningham wrote:
> I have these two functions one calls the other one. I have tried
> everything to debug them except a damn debugger. I have placed puts messages
> at all the return and I still get two numbers on my screen. What's wrong
> with this code?


What do you expect if you call printf twice?

3/10.

--
Ian Collins

steve 01-17-2011 07:47 PM

Re: stuck
 
On Jan 17, 11:12*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> * * I have these two functions one calls the other one. I have tried
> everything to debug them except a damn debugger. I have placed puts messages
> at all the return and I still get two numbers on my screen. What's wrong
> with this code? I will post the header, object file source and main() code.
>
> #include <stdio.h>
>
> double oper(char, double, double);
> double vop(double);
>
> Ok that's my declarations
>
> #include "core.h"
>
> double oper(char c, double x, double y)
> {
> * * if (c == 'a')
> * * * * return vop(x + y);
> * * else if (c == 's') {
> * * * * return vop(x - y);
> * * } else if (c == 'm') {
> * * * * return vop(x * y);
> * * } else if (c == 'd') {
> * * * * return vop(x / y);
> * * } else {
> * * * * fputs("usage error\n", stderr);
> * * * * return -1;
> * * }
> * * return 0;
>
> }
>
> double vop(double oper)
> {
> * * return printf("%.2f\n",oper);
>
> }
>
> Above here is the main code I compile in .o file. And this is the main
> function:
>
> #include "core.h"
>
> int main()
> {
> * * double a;
> * * a = oper('s', 5, 2);
> * * return vop(a);
>
> }
>
> Everything works like I want it to except I am getting two numbers. The
> second number is always the same whatever I pass to oper().


Of course, you're going to get 2 numbers. You call vop() twice.
Have you read the documentation for printf? Try changing your
main to

int main(void)
{
double a, b;
a = oper('s', 5., 2.);
b = oper('s', 50., 2.);
printf("%f %f\n", a, b);
return (0);
}


--
steve

Pavel Borzenkov 01-17-2011 07:48 PM

Re: stuck
 
Hi,

On 2011-01-17, Bill Cunningham <nospam@nspam.invalid> wrote:
> I have these two functions one calls the other one. I have tried
> everything to debug them except a damn debugger. I have placed puts messages
> at all the return and I still get two numbers on my screen. What's wrong
> with this code? I will post the header, object file source and main() code.
>
> #include <stdio.h>
>
> double oper(char, double, double);
> double vop(double);
>
> Ok that's my declarations
>
> #include "core.h"
>
> double oper(char c, double x, double y)
> {
> if (c == 'a')
> return vop(x + y);
> else if (c == 's') {
> return vop(x - y);
> } else if (c == 'm') {
> return vop(x * y);
> } else if (c == 'd') {
> return vop(x / y);
> } else {
> fputs("usage error\n", stderr);
> return -1;
> }
> return 0;
> }


This function will return the vop's return value (in case the supported
operation is passed in the 'c' variable).

>
> double vop(double oper)
> {
> return printf("%.2f\n",oper);
> }


This function will return the printf's return value (number of successfully
written characters), not the oper's value.

>
> Above here is the main code I compile in .o file. And this is the main
> function:
>
> #include "core.h"
>
> int main()
> {
> double a;
> a = oper('s', 5, 2);
> return vop(a);
> }


The 'a' variable will contain the oper's return value (number of characters
written by printf). Try call like oper('s', 9, 2); and you will see that the
number has changed.

-P

>
> Everything works like I want it to except I am getting two numbers. The
> second number is always the same whatever I pass to oper().
>
> Bill
>
>


Bill Cunningham 01-17-2011 07:53 PM

Re: stuck
 
steve wrote:

[snip]

> Of course, you're going to get 2 numbers. You call vop() twice.


How am I doing that? Do you mean in the oper() and in main() I am
calling vop?

> Have you read the documentation for printf? Try changing your
> main to
>
> int main(void)
> {
> double a, b;
> a = oper('s', 5., 2.);
> b = oper('s', 50., 2.);
> printf("%f %f\n", a, b);
> return (0);
> }


Yes I see what you're doing above but it is defeating the purpose I
started out with. I want vop() or oper() to call the other to printf. I
don't just want to stick printf into main(). I hope I'm clear.

Bill



Ben Bacarisse 01-17-2011 07:58 PM

Re: stuck
 
"Bill Cunningham" <nospam@nspam.invalid> writes:

> I have these two functions one calls the other one. I have tried
> everything to debug them except a damn debugger.


Might be worth a try then.

> I have placed puts messages
> at all the return and I still get two numbers on my screen. What's wrong
> with this code? I will post the header, object file source and main() code.
>
> #include <stdio.h>
>
> double oper(char, double, double);
> double vop(double);
>
> Ok that's my declarations
>
> #include "core.h"
>
> double oper(char c, double x, double y)
> {
> if (c == 'a')
> return vop(x + y);
> else if (c == 's') {
> return vop(x - y);
> } else if (c == 'm') {
> return vop(x * y);
> } else if (c == 'd') {
> return vop(x / y);
> } else {
> fputs("usage error\n", stderr);
> return -1;
> }
> return 0;


This last line serves no purpose.

> }
>
> double vop(double oper)
> {
> return printf("%.2f\n",oper);


What value does printf return? Is that really the value you want vop
(and hence) oper to return?

> }
>
> Above here is the main code I compile in .o file. And this is the main
> function:
>
> #include "core.h"
>
> int main()
> {
> double a;
> a = oper('s', 5, 2);
> return vop(a);


Do you really want the result of vop to the return value from main?
Looks unlikely to me.

> }
>
> Everything works like I want it to except I am getting two numbers.


You get two numbers printed because there are two calls to vop: one in
main and one from the call to oper in main.

> The
> second number is always the same whatever I pass to oper().


Not if you change the call to oper('s', 50, 2) or to oper('s', 2, 5) or,
indeed, to lots of other things that give a different second number.

--
Ben.

Bill Cunningham 01-17-2011 08:02 PM

Re: stuck
 
Pavel Borzenkov wrote:
> Hi,
>
> On 2011-01-17, Bill Cunningham <nospam@nspam.invalid> wrote:
>> I have these two functions one calls the other one. I have tried
>> everything to debug them except a damn debugger. I have placed puts
>> messages at all the return and I still get two numbers on my screen.
>> What's wrong with this code? I will post the header, object file
>> source and main() code.
>>
>> #include <stdio.h>
>>
>> double oper(char, double, double);
>> double vop(double);
>>
>> Ok that's my declarations
>>
>> #include "core.h"
>>
>> double oper(char c, double x, double y)
>> {
>> if (c == 'a')
>> return vop(x + y);
>> else if (c == 's') {
>> return vop(x - y);
>> } else if (c == 'm') {
>> return vop(x * y);
>> } else if (c == 'd') {
>> return vop(x / y);
>> } else {
>> fputs("usage error\n", stderr);
>> return -1;
>> }
>> return 0;
>> }

>
> This function will return the vop's return value (in case the
> supported operation is passed in the 'c' variable).


Ok maybe that's the mistake. I've never really worked in this manner
before with returns. I thought I was sending to vop(). So what I probably
need then is to use return oper() does that sound right. Boy I feel like a
big fool now but this one really stuck me.
>>
>> double vop(double oper)
>> {
>> return printf("%.2f\n",oper);
>> }

>
> This function will return the printf's return value (number of
> successfully written characters), not the oper's value.


I'm not sure I understand here.

>> Above here is the main code I compile in .o file. And this is the
>> main function:
>>
>> #include "core.h"
>>
>> int main()
>> {
>> double a;
>> a = oper('s', 5, 2);
>> return vop(a);
>> }

>
> The 'a' variable will contain the oper's return value (number of
> characters written by printf). Try call like oper('s', 9, 2); and you
> will see that the number has changed.
>
> -P
>
>>
>> Everything works like I want it to except I am getting two numbers.
>> The second number is always the same whatever I pass to oper().
>>
>> Bill




Niklas Holsti 01-17-2011 08:05 PM

Re: stuck
 
Bill Cunningham wrote:
> I have these two functions one calls the other one. I have tried
> everything to debug them except a damn debugger. I have placed puts messages
> at all the return and I still get two numbers on my screen. What's wrong
> with this code?


Whether the code is wrong or right depends on what you want it to do,
which you haven't said -- yet.

> double oper(char c, double x, double y)
> {
> if (c == 'a')
> return vop(x + y);
> else if (c == 's') {
> return vop(x - y);
> } else if (c == 'm') {
> return vop(x * y);
> } else if (c == 'd') {
> return vop(x / y);
> } else {
> fputs("usage error\n", stderr);
> return -1;
> }
> return 0;


That "return 0" is unreachable, so I would say it is "wrong".

> double vop(double oper)
> {
> return printf("%.2f\n",oper);


Since I have never seen a function that was expected to return the
return value of a printf() call, I *suspect* that that is "wrong".
Perhaps you meant:

printf("%.2f\n",oper);
return oper;

> }
>
> Above here is the main code I compile in .o file. And this is the main
> function:
>
> #include "core.h"
>
> int main()
> {
> double a;
> a = oper('s', 5, 2);


The first number is printed in vop() that is called from oper(). It
prints the number 3 (5-2) in the form 3.00.

With your implementation of vop(), the variable "a" will always have the
return value of printf() which normally is the number of characters
printed. This number depends on the magnitude of the parameter oper in
vop(); here oper equals 3.0 so the printed string is "3.00\n" which is 5
characters long (under Linux, where \n is a single character).
Therefore, "a" is 5.0.

> return vop(a);


The second number is printed in this call of vop() and is 5.00.

By the way, I would call this "return" statement "wrong" because it
tries to return a double value from main(), instead of an int.

> }
>
> Everything works like I want it to except I am getting two numbers. The
> second number is always the same whatever I pass to oper().


Try oper('s', 5000.0, 2.0). The second printed number should then be
larger than 5 because the first printf() prints the longer string
4998.00\n (which is 8 characters under Linux).

--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .

Bill Cunningham 01-17-2011 08:11 PM

Re: stuck
 
Ben Bacarisse wrote:
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>
>> I have these two functions one calls the other one. I have tried
>> everything to debug them except a damn debugger.

>
> Might be worth a try then.
>
>> I have placed puts messages
>> at all the return and I still get two numbers on my screen. What's
>> wrong with this code? I will post the header, object file source and
>> main() code.
>>
>> #include <stdio.h>
>>
>> double oper(char, double, double);
>> double vop(double);
>>
>> Ok that's my declarations
>>
>> #include "core.h"
>>
>> double oper(char c, double x, double y)
>> {
>> if (c == 'a')
>> return vop(x + y);
>> else if (c == 's') {
>> return vop(x - y);
>> } else if (c == 'm') {
>> return vop(x * y);
>> } else if (c == 'd') {
>> return vop(x / y);
>> } else {
>> fputs("usage error\n", stderr);
>> return -1;
>> }
>> return 0;

>
> This last line serves no purpose.
>
>> }
>>
>> double vop(double oper)
>> {
>> return printf("%.2f\n",oper);

>
> What value does printf return? Is that really the value you want vop
> (and hence) oper to return?


Ok no no. I want printf to return vop() then. Ok thanks much for clearing me
up. I will try this again maybe from scratch. I want the answer of oper() to
be passed to vop() which is called from main and it prints what oper()
returns.

>> }
>>
>> Above here is the main code I compile in .o file. And this is the
>> main function:
>>
>> #include "core.h"
>>
>> int main()
>> {
>> double a;
>> a = oper('s', 5, 2);
>> return vop(a);

>
> Do you really want the result of vop to the return value from main?
> Looks unlikely to me.
>
>> }
>>
>> Everything works like I want it to except I am getting two numbers.

>
> You get two numbers printed because there are two calls to vop: one in
> main and one from the call to oper in main.
>
>> The
>> second number is always the same whatever I pass to oper().

>
> Not if you change the call to oper('s', 50, 2) or to oper('s', 2, 5)
> or, indeed, to lots of other things that give a different second
> number.






Bill Cunningham 01-17-2011 08:16 PM

Re: stuck
 
Niklas Holsti wrote:
> Bill Cunningham wrote:
>> I have these two functions one calls the other one. I have tried
>> everything to debug them except a damn debugger. I have placed puts
>> messages at all the return and I still get two numbers on my screen.
>> What's wrong with this code?

>
> Whether the code is wrong or right depends on what you want it to do,
> which you haven't said -- yet.
>
>> double oper(char c, double x, double y)
>> {
>> if (c == 'a')
>> return vop(x + y);
>> else if (c == 's') {
>> return vop(x - y);
>> } else if (c == 'm') {
>> return vop(x * y);
>> } else if (c == 'd') {
>> return vop(x / y);
>> } else {
>> fputs("usage error\n", stderr);
>> return -1;
>> }
>> return 0;

>
> That "return 0" is unreachable, so I would say it is "wrong".


I wondered if that return was needed or not. I seemed like it should be
there.

>> double vop(double oper)
>> {
>> return printf("%.2f\n",oper);

>
> Since I have never seen a function that was expected to return the
> return value of a printf() call, I *suspect* that that is "wrong".


I have seen it. I don't know if it's good code or not though.

> Perhaps you meant:
>
> printf("%.2f\n",oper);
> return oper;
>
>> }
>>
>> Above here is the main code I compile in .o file. And this is the
>> main function:
>>
>> #include "core.h"
>>
>> int main()
>> {
>> double a;
>> a = oper('s', 5, 2);

>
> The first number is printed in vop() that is called from oper(). It
> prints the number 3 (5-2) in the form 3.00.
>
> With your implementation of vop(), the variable "a" will always have
> the return value of printf() which normally is the number of
> characters printed. This number depends on the magnitude of the
> parameter oper in vop(); here oper equals 3.0 so the printed string
> is "3.00\n" which is 5 characters long (under Linux, where \n is a
> single character). Therefore, "a" is 5.0.
>
>> return vop(a);

>
> The second number is printed in this call of vop() and is 5.00.
>
> By the way, I would call this "return" statement "wrong" because it
> tries to return a double value from main(), instead of an int.
>
>> }
>>
>> Everything works like I want it to except I am getting two numbers.
>> The second number is always the same whatever I pass to oper().

>
> Try oper('s', 5000.0, 2.0). The second printed number should then be
> larger than 5 because the first printf() prints the longer string
> 4998.00\n (which is 8 characters under Linux).


Interesting.




All times are GMT. The time now is 03:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.