Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Strange output (http://www.velocityreviews.com/forums/t439722-strange-output.html)

unique 10-09-2005 09:14 AM

Strange output
 
I have written some programs in c lang yet but today I get confused
with output i get.
I have function educate(..) which i call in main program this way:

J = educate(no_layers, no_neurons, input, output, lay, weights, 0.1,0);
printf("J= %f\n",J);

In the educate function i count J and i return it (but before I return
it I output it with printf):

float educate (int no_layers, int no_neurons[], float input[], float
output[], float* lay[], float* weights[], float gama, int debug) {
....
printf("J= %f\n",J);
return(J);
}

This is what i get:

J= 0.304447
J= 1050402944.000000

Is there any syntax prob? Why the values arent the same? :(


Flash Gordon 10-09-2005 10:39 AM

Re: Strange output
 
unique wrote:
> I have written some programs in c lang yet but today I get confused
> with output i get.
> I have function educate(..) which i call in main program this way:


<snip description of program>

> This is what i get:
>
> J= 0.304447
> J= 1050402944.000000
>
> Is there any syntax prob? Why the values arent the same? :(


Post a *complete* small program that exhibits your problem, NOT a
description. How are we to guess which of the many possible errors you
have committed if you don't show us your actual code?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

unique 10-09-2005 11:28 AM

Re: Strange output
 
I was investigating the problem a bit and the problem seems to be that
I call in file educate.c function educate(...) from the other file
neuron.c. I dont use any include, so code is here:
***test1.c***
float educate() {
float a=2.54f;
printf("a= %f", a);
return a;
}


***test2.c***
int main(int argc, char **argv) {
float b=educate();
printf("b= %f", b);
}

After compiling it with: cc test1.c test2.c -lm -o test2 and running it
the output is: a= 2.540000b= 1076006784.000000

So the problem is definitely in including file test2... how can i
include it right? what's going on in my example?

Thank you for your reply
Flash Gordon wrote:
> unique wrote:
> > I have written some programs in c lang yet but today I get confused
> > with output i get.
> > I have function educate(..) which i call in main program this way:

>
> <snip description of program>
>
> > This is what i get:
> >
> > J= 0.304447
> > J= 1050402944.000000
> >
> > Is there any syntax prob? Why the values arent the same? :(

>
> Post a *complete* small program that exhibits your problem, NOT a
> description. How are we to guess which of the many possible errors you
> have committed if you don't show us your actual code?
> --
> Flash Gordon
> Living in interesting times.
> Although my email address says spam, it is real and I read it.



pete 10-09-2005 11:29 AM

Re: Strange output
 
Flash Gordon wrote:
>
> unique wrote:
> > I have written some programs in c lang yet but today I get confused
> > with output i get.
> > I have function educate(..) which i call in main program this way:

>
> <snip description of program>
>
> > This is what i get:
> >
> > J= 0.304447
> > J= 1050402944.000000
> >
> > Is there any syntax prob? Why the values arent the same? :(

>
> Post a *complete* small program that exhibits your problem, NOT a
> description. How are we to guess which of the many possible errors you
> have committed if you don't show us your actual code?


My guess is that changing the declaration of all of
the float type objects, to type double, will fix the problem.

I avoid the small arithmetic types, :

char
unsigned char
/* Especially these next 4 */
signed char
short
unsigned short
float

unless there's a special reason.
The problem is that those types tend to get promoted a lot.

Strings are a special enough reason to use type char.
Reading and/or writing bytes in raw memory
is a good reason to use unsigned char.

--
pete

pete 10-09-2005 11:49 AM

Re: Strange output
 
unique wrote:
>
> I was investigating the problem a bit and the problem seems to be that
> I call in file educate.c function educate(...) from the other file
> neuron.c. I dont use any include, so code is here:
> ***test1.c***
> float educate() {
> float a=2.54f;
> printf("a= %f", a);
> return a;
> }
>
> ***test2.c***
> int main(int argc, char **argv) {
> float b=educate();
> printf("b= %f", b);
> }
>
> After compiling it with:
> cc test1.c test2.c -lm -o test2 and running it


What does "cc test1.c test2.c -lm -o test2" mean?

> the output is: a= 2.540000b= 1076006784.000000
>
> So the problem is definitely in including file test2...
> how can i include it right? what's going on in my example?


I don't see #include <stdio.h> in either file,
so both files are undefined.

> > Post a *complete* small program that exhibits your problem, NOT a
> > description.
> > How are we to guess which of the many possible errors you
> > have committed if you don't show us your actual code?


--
pete

Skarmander 10-09-2005 12:06 PM

Re: Strange output
 
unique wrote:
> I was investigating the problem a bit and the problem seems to be that
> I call in file educate.c function educate(...) from the other file
> neuron.c. I dont use any include, so code is here:
> ***test1.c***
> float educate() {
> float a=2.54f;
> printf("a= %f", a);
> return a;
> }
>
>
> ***test2.c***
> int main(int argc, char **argv) {
> float b=educate();
> printf("b= %f", b);
> }
>
> After compiling it with: cc test1.c test2.c -lm -o test2 and running it
> the output is: a= 2.540000b= 1076006784.000000
>
> So the problem is definitely in including file test2... how can i
> include it right? what's going on in my example?
>

<snip>
What's going on is that the compiler doesn't know what sort of function
educate() is when it's compiling test2.c. In C, units are compiled one
at a time, even if you pass multiple to the compiler.

So the compiler must assume a default of educate() returning an int,
which of course it doesn't. The bits that make up the float are then
interpreted as an int and converted back to a float. To fix this, you
should add a function prototype, like so:

test2.c:
float educate(void);

int main(int argc, char **argv) {
float b=educate();
printf("b= %f", b);
return 0;
}

But good style is to put prototypes of functions accessed by other units
in headers and include them:

test1.h:
float educate(void);

test2.c:
#include "test1.h"

int main(...

Read any good book on C that talks about functions and prototypes for more.

S.

Flash Gordon 10-09-2005 01:19 PM

Re: Strange output
 
unique wrote:

Your reply belongs *after* the text you are replying to, not before,
after deleting (snipping) the text you are not replying to.

> I was investigating the problem a bit and the problem seems to be that
> I call in file educate.c function educate(...) from the other file
> neuron.c. I dont use any include, so code is here:
> ***test1.c***


#include <stdio.h>
#include <test1.h>

I'll explain the reasons for these further down.

> float educate() {


If it doesn't take parameters it is better to explicitly say so.
float educate(void)
{

> float a=2.54f;
> printf("a= %f", a);


printf is a varidac function and *requires* a prototype in scope. The
normal way to do this is to include stdio.h at or about the top of the
source file.

> return a;
> }
>
>
> ***test2.c***


#include <stdio.h>
#include <test1.h>

I'll explain the reasons for these further down.

> int main(int argc, char **argv) {


You are not using the parameters, so you might as well say so.
int main(void)
{

Note that main returns and int (no other return value including void is
standard).

> float b=educate();


There is no prototype for educate in scope, so the compiler is
*required* to assume it returns an int. Since it does not the behaviour
is undefined and the effect in your case is that b is assigned a garbage
value. The standard way to deal with this is using a header file such as
the one I show below and to include it in *both* the file defining the
function (to ensure the prototype matches) and the file from which it is
called. If your C text book (and/or tutor) does not explain this they
need to be replaced.

> printf("b= %f", b);


Again, the prototype is required for printf.

> }
>
> After compiling it with: cc test1.c test2.c -lm -o test2 and running it
> the output is: a= 2.540000b= 1076006784.000000
>
> So the problem is definitely in including file test2... how can i
> include it right? what's going on in my example?


<snip>

Having made the above changes you create a header file which provides a
prototype for educate:

/* test1.h */
#ifdef TEST1_H
#define TEST1_H
float educate(void);
#endif

Then the compiler knows what is going on.

To understand the reason for the #ifdef etc search for include guards.

I also suggest reading the comp.lang.c FAQ (google will find it) and
K&R2 (the FAQ will tell you what that is).
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

unique 10-09-2005 01:39 PM

Re: Strange output
 
> So the compiler must assume a default of educate() returning an int,
which of course it doesn't.

Thank you very much Skarmander, you are completely right, i solved it
with header file.

Have a gr8 day all


Martin Ambuhl 10-09-2005 03:36 PM

Re: Strange output
 
unique wrote:
> I have written some programs in c lang yet but today I get confused
> with output i get.
> I have function educate(..) which i call in main program this way:
>
> J = educate(no_layers, no_neurons, input, output, lay, weights, 0.1,0);
> printf("J= %f\n",J);
>
> In the educate function i count J and i return it (but before I return
> it I output it with printf):
>
> float educate (int no_layers, int no_neurons[], float input[], float
> output[], float* lay[], float* weights[], float gama, int debug) {
> ...
> printf("J= %f\n",J);
> return(J);
> }
>
> This is what i get:
>
> J= 0.304447
> J= 1050402944.000000
>
> Is there any syntax prob? Why the values arent the same? :(
>


$oracle -n
---
---
---
---
-X-
- -

(MWD)
Jian over Gen
3 Yuan 'Wielding'
Wielding: Receipt; little beneficial to determine.

(WB)
Qian over Gen
33 Dun 'Withdrawal'
Withdrawal: prevalence is had. It is fitting
to practice constancy in small matters.

Transforming:
second yin
(MWD) Uphold it using a yellow ox's bridle;
no one will succeed in overturning it,

(WB) If one holds then with yellow ox hide,
none will manage to break away.

Approaches:
---
---
---
---
---
- -
(MWD)
Jian over Suan
8 Gou 'Meeting'
[Meeting]: The maiden matures; do not herewith take
a maiden.

(WB)
Qian over Sun
44 Gou 'Encounter'
Encounter: the woman is strong; it would not do to
marry this woman.

$

Martin Ambuhl 10-09-2005 03:40 PM

Re: Strange output
 
unique wrote:
> I was investigating the problem a bit and the problem seems to be that
> I call in file educate.c function educate(...) from the other file
> neuron.c. I dont use any include, so code is here:
> ***test1.c***
> float educate() {
> float a=2.54f;
> printf("a= %f", a);
> return a;
> }
>
>
> ***test2.c***
> int main(int argc, char **argv) {
> float b=educate();
> printf("b= %f", b);
> }
>
> After compiling it with: cc test1.c test2.c -lm -o test2 and running it
> the output is: a= 2.540000b= 1076006784.000000
>
> So the problem is definitely in including file test2


WRONG! Your problem is the failure to provide a declaration for
educate() in test2.c. The educate() function test2 knows about returns
an int (in C89; your code is just broken in C99), while educate() in
test1.c returns a float.

Your code is broken even if you include a declaration for educate() in
test2.c, since you fail to provide the required declaration for the
variadic function printf(); that's what <stdio.h> is for.


All times are GMT. The time now is 08:38 AM.

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