Strange output

U

unique

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? :(
 
F

Flash Gordon

unique said:
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:

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?
 
U

unique

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
 
P

pete

Flash said:
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.
 
P

pete

unique said:
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?
 
S

Skarmander

unique said:
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.
 
F

Flash Gordon

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).
 
U

unique

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
 
M

Martin Ambuhl

unique said:
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.

$
 
M

Martin Ambuhl

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

Barry Schwarz

which of course it doesn't.

Thank you very much Skarmander, you are completely right, i solved it
with header file.
In addition to solving it this time, you should up the warning level
of your compiler so that it tells anytime you invoke a function for
which a prototype is not in scope.


<<Remove the del for email>>
 
E

Emmanuel Delahaye

unique a écrit :
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

This works fine to me (single compile unit)

#include <stdio.h>

float educate(void)
{
float a=2.54f;
printf("a= %f\n", a);
return a;
}

int main(void)
{
float b=educate();
printf("b= %f\n", b);
}

Now, if you are gooing to use 2 separated compile units (CUs), you must
define a protoype in a common file called a header. This is the proper
way of doing it:

/* educate.h */
#ifndef H_EDUCATE
#define H_EDUCATE

/* function prototypes */
float educate(void);

#endif /* guard */

Then you must include this header in the definition CU (for consistency)
and in all user CU:

/* educate.c */
#include <stdio.h>
#include "educate.h"
float educate(void)
{
float a=2.54f;
printf("a= %f\n", a);
return a;
}

/* main.c */
#include <stdio.h>
#include "educate.h"

int main(void)
{
float b=educate();
printf("b= %f\n", b);
return 0;
}
 
E

Emmanuel Delahaye

unique a écrit :
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

This works fine to me (single compile unit)

#include <stdio.h>

float educate(void)
{
float a=2.54f;
printf("a= %f\n", a);
return a;
}

int main(void)
{
float b=educate();
printf("b= %f\n", b);
}

Now, if you are going to use two separated compile units (CUs), you must
define a protoype in a common file called a header. This is the proper
way of doing it (the guard protects agains multiple inclusions in a
single CU):

/* educate.h */
#ifndef H_EDUCATE
#define H_EDUCATE

/* function prototypes */
float educate(void);

#endif /* guard */

Then you must include this header in the definition CU (for consistency)
and in all user CU:

/* educate.c */
#include <stdio.h>
#include "educate.h"
float educate(void)
{
float a=2.54f;
printf("a= %f\n", a);
return a;
}

/* main.c */
#include <stdio.h>
#include "educate.h"

int main(void)
{
float b=educate();
printf("b= %f\n", b);
return 0;
}
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top