stuck

B

Bill Cunningham

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
 
I

Ian Collins

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

steve

    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);
}
 
P

Pavel Borzenkov

Hi,

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
 
B

Bill Cunningham

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
 
B

Ben Bacarisse

Bill Cunningham said:
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.
 
B

Bill Cunningham

Pavel said:
Hi,



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

Niklas Holsti

Bill said:
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).
 
B

Bill Cunningham

Ben said:
Might be worth a try then.


This last line serves no purpose.


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

Bill Cunningham

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


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


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.


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.


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

osmium

:

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

This is a persistent problem, Bill. Cn you complete the comments I have
added? I can *almost* figure out what oper() is supposed to do, then it
calls vop() for some reason unknown to me and I am baffled. I think you
wanted to print something but wanted to do it indirectly. Might a macro be
be usefull here? Is the object to save typing? Or what?

// This function ....
// This fucntion ...Why overuse identifiers? There are plenty of unique identifiers to go
around, and uniqueness adds clarity.
In other words:

double vop(double abc)
{
retrun printf("%.2f\n", abc):
}
would be less confusing.

// this program tests ... by.... and then ....
 
B

Bill Cunningham

osmium said:

Ok I thought it was clear I will start adding comments from now on. I
want to call a function to do basic arithmetic operations called oper().
vop() mean ViewOPer. What oper() returns is passed to vop() which acts as a
sort of printf without calling printf directly in main. What I wanted was a
simple experiment in a function passing value to another function which
takes that data and does something with it. I this case prints data.
 
I

Ian Collins

Ok I thought it was clear I will start adding comments from now on. I
want to call a function to do basic arithmetic operations called oper().
vop() mean ViewOPer.

Then why don't you call it ViewOper?
 
O

osmium

Bill Cunningham said:
Ok I thought it was clear I will start adding comments from now on. I
want to call a function to do basic arithmetic operations called oper().
vop() mean ViewOPer. What oper() returns is passed to vop() which acts as
a sort of printf without calling printf directly in main. What I wanted
was a simple experiment in a function passing value to another function
which takes that data and does something with it. I this case prints data.

In that case, the call to vop directly from main makes no sense does it?
Why do you think the operating system wants to know the results returned by
a function that *you* wrote? The OS only cares about goodness or badness.

In the future you might want to think "what will the caller do with this
value I am planning to return?" If the answer is "nothing", then don't use a
return, the function will return without the explicit statement being
included. In the case at hand you wanted the printf for the side effects,
not the value returned by printf. It may seem ridiculous, but printing (and
other very useful things, are classified as side effects.

I will look forward to an explanatory comment in front of each of your
function definitions.
 
B

Bill Cunningham

osmium said:
In that case, the call to vop directly from main makes no sense does
it? Why do you think the operating system wants to know the results
returned by a function that *you* wrote? The OS only cares about
goodness or badness.
In the future you might want to think "what will the caller do with
this value I am planning to return?" If the answer is "nothing", then
don't use a return, the function will return without the explicit
statement being included. In the case at hand you wanted the printf
for the side effects, not the value returned by printf. It may seem
ridiculous, but printing (and other very useful things, are
classified as side effects.
I will look forward to an explanatory comment in front of each of your
function definitions.

What if oper() said this...

if (c=='a')
return x+y; ...

Now if I am correct the added variables in the function would be returned
but it wouldn't be printed. I want to pass this "return x+y;" to a function
that would print the results. An experiment using two functions. I could use
printf in main but I want a wrapper function that would basically printf in
this function. Make sense?

Bill
 
O

osmium

Bill Cunningham said:
What if oper() said this...

if (c=='a')
return x+y; ...

Now if I am correct the added variables in the function would be returned
but it wouldn't be printed. I want to pass this "return x+y;" to a
function that would print the results. An experiment using two functions.
I could use printf in main but I want a wrapper function that would
basically printf in this function. Make sense?

You could compute the desired result for all four good cases and pass that
value to a function that prints it.

Something like:

in oper():
double result;
....
if(c == 'a')
result = x+y;
// other three cases plus error detection case
....
print(result);
return result;

void print(double arg)
{
printf( fromat_stuff, arg); // note lack of return statement
}
 
A

August Karlstrom

And I would recommend you write code that others will understand : and
you will soon see why.

OK, my bad. I meant to say "Don't write code that doesn't make sense to
you".


August
 
K

Keith Thompson

Bill Cunningham said:
Ben Bacarisse wrote: [...]
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.

That makes no sense at all.

I don't expect you to stop making wild guesses and stringing words
together randomly, but *if* your actual goal is to learn to write C,
then you should.
 
B

BruceS

Bill Cunningham said:
Ben Bacarisse wrote: [...]
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.

That makes no sense at all.

I don't expect you to stop making wild guesses and stringing words
together randomly, but *if* your actual goal is to learn to write C,
then you should.

It seems clear to me that that is not his goal. Bill seems to be
achieving his goal quite nicely.
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top