473,830 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_laye rs, 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.0000 00

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

Nov 15 '05 #1
12 1687
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.0000 00

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.
Nov 15 '05 #2
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.0000 00

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.0000 00

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.


Nov 15 '05 #3
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.0000 00

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
Nov 15 '05 #4
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.0000 00

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
Nov 15 '05 #5
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.0000 00

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.
Nov 15 '05 #6
> 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

Nov 15 '05 #7
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.0000 00

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.
Nov 15 '05 #8
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_laye rs, 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.0000 00

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.

$
Nov 15 '05 #9
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.0000 00

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.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2158
by: Claudio | last post by:
hi all i put in this email source code so u can copy and paste to verify strange first : in this example bit size is a BYTE ?! second : in the last printf output is wrong ? ? best regards all
24
4756
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone out there has run into this issue before and can shed some light on it for me. I have a stored procedure that essentially does a mini ETL from a source OLTP DB to a the target Operational Data Store. This is still in development so both DB's reside on the same machine for convenience. The stored proc runs successfully from within Query analyzer and this holds true on the following platforms: XP...
8
1832
by: grundmann | last post by:
Hello, i got a strange compiler error. When compiling the following: // forward declarations typedef AvlTree<LineSegment,LineSegmentComperator> LSTree; void handleEventPoint (const EventPoint& , LSTree& , double&, std::list<IntersectionPoint>& );
8
2060
by: Victor Lamberty | last post by:
Greetings C coders I am new to the world of C and have been trying to compile this program. I got the result that I wanted but outputed it in a strange way it put it before the prompt is there a reason in this code. Or is that just a trait of the KDE Konsole. /*#I hope that this is a C program*/ /*#define hello-computer the output is hello-computer*/ #include <stdio.h>
6
8542
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
1
1451
by: Martin Feuersteiner | last post by:
Dear Group I'm having a very weird problem. Any hints are greatly appreciated. I'm returning two values from a MS SQL Server 2000 stored procedure to my Webapplication and store them in sessions. Like This: prm4 = cmd1.CreateParameter With prm4
5
4083
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object serialisation framework that cannot be done in binary format currently, so please no comments about this ,-)) It took quite some time to shrink down the problem but it looks like that C++ does not behave well in regards to very tiny numbers.
5
3113
by: Ian | last post by:
Hi everyone, I have found some bizarre (to me...!) behaviour of the Form_Activate function. I have a form which has a button control used to close the form and a subform with a datasheet view showing a list of jobs from the database. When the main form loses focus and the user clicks the 'Close' button, I kept receiving error 2585 (This action cannot be carried out whilst processing a form or report event). This was tracked down to...
4
1398
by: stat_holyday | last post by:
Greetings. I'm confused. I'm attemting to create dynamic buttons based on the count of questions in a database. The button has 3 states, blank, selected, and inactive. The script below works great, but with a strange bug that seems impossible to me. When the $question_count and $question_id are 2 or more values apart the script works, but when they are only 1 value apart, it bugs out. Examples...
2
1593
by: danep2 | last post by:
Hello all This is a really strange problem. I have code that performs a few calculations based on input from a joystick, and writes these values to a file using basically the following code: fprintf(fpResults, "i: %4.2f, j: %4.2f", i, j ); This works correctly (literally) 99.9999% of the time. However, once every million or so writes I get the following output:
0
9793
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10774
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10491
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10526
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10206
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7746
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.