function problem

B

Bill Cunningham

I know this must have some simple answer but I'm having trouble with this
function I wrote.
stdio.h
math.h

double relstr (double x,double y)
{double n;
n=x/y;
printf("%f",(return n;));}
I dunno.

Bill
 
M

Morris Dovey

Bill said:
I know this must have some simple answer but I'm having trouble with this
function I wrote.
stdio.h
math.h

double relstr (double x,double y)
{double n;
n=x/y;
printf("%f",(return n;));}
I dunno.

Bill...

I hope your C compiler is having trouble with this one, too.
You're trying to code a statement where the only allowable
construct is an expression.
 
J

Jack Klein

I know this must have some simple answer but I'm having trouble with this
function I wrote.
stdio.h
math.h

double relstr (double x,double y)
{double n;
n=x/y;
printf("%f",(return n;));}
I dunno.

Bill

One of your problems is that you are using a broken compiler, if it
compiles this, since the code is illegal. You can't use a return
statement as an argument to a function, as it does not yield a value.
 
B

Bill Cunningham

One of your problems is that you are using a broken compiler, if it
compiles this, since the code is illegal. You can't use a return
statement as an argument to a function, as it does not yield a value.
Oh no the compiler won't compile this. It says error before return. Don't I
have to get the code to return the value of n and send it to stdout via
printf?

Bill
 
P

pete

Bill said:
Oh no the compiler won't compile this. It says error before return.
Don't I have to get the code to return the value of n and
send it to stdout via printf?

#include <stdio.h>

double relstr (double x, double y)
{
double n;

n = x / y;
printf("%f", n);
return n;
}
 
M

Mark McIntyre

I know this must have some simple answer but I'm having trouble with this
function I wrote.
stdio.h
math.h

first off, get a better newsreader - one that doesn't strip out the
#include and said:
double relstr (double x,double y)
{double n;
n=x/y;
printf("%f",(return n;));}

Two problems here:
1) you can't use a return statement as an argument of a function.
2) your brace style is EXCEPTIONALLY horrible. Do NOT follow or
precede braces with code, it makes it unbelievable hard to read.
 
C

Chris Torek

printf("%f", n);
[where n is a "double"]

ITYM:
printf("%lf", n);

This is well-defined in C99, and does the same as using "%f", but
in C89 the combination of the "l" modifier with any of the floating
point formats in printf (e, E, f, g, and G) is technically undefined.

You always need the l in %lf when using scanf() on "double"s, but
in C89, you should not use the l modifier. The printf() and scanf()
functions are not as symmetric as they might first appear. This
false symmetry begins with the fact that scanf() takes the addresses
of the objects it fills in, and continues with %f vs %lf and gets
even more asymmetric with the behavior of %* directives. In printf,
for instance, %*d requires two int values and prints the second
using the first as a field width, but in scanf, %*d requires zero
pointers and skips the assignment of an "int" conversion.
 

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

Forum statistics

Threads
474,135
Messages
2,570,784
Members
47,342
Latest member
KelseyK737

Latest Threads

Top