Strange output

Discussion in 'C Programming' started by unique, Oct 9, 2005.

  1. unique

    unique Guest

    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? :(
     
    unique, Oct 9, 2005
    #1
    1. Advertising

  2. unique

    Flash Gordon Guest

    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.
     
    Flash Gordon, Oct 9, 2005
    #2
    1. Advertising

  3. unique

    unique Guest

    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.
     
    unique, Oct 9, 2005
    #3
  4. unique

    pete Guest

    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, Oct 9, 2005
    #4
  5. unique

    pete Guest

    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
     
    pete, Oct 9, 2005
    #5
  6. unique

    Skarmander Guest

    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.
     
    Skarmander, Oct 9, 2005
    #6
  7. unique

    Flash Gordon Guest

    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.
     
    Flash Gordon, Oct 9, 2005
    #7
  8. unique

    unique Guest

    > 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
     
    unique, Oct 9, 2005
    #8
  9. 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, Oct 9, 2005
    #9
  10. 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.
     
    Martin Ambuhl, Oct 9, 2005
    #10
  11. On 9 Oct 2005 06:39:15 -0700, "unique" <> wrote:

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

    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>>
     
    Barry Schwarz, Oct 9, 2005
    #11
  12. 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;
    }
     
    Emmanuel Delahaye, Oct 9, 2005
    #12
  13. 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;
    }
     
    Emmanuel Delahaye, Oct 9, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nikk & Jak Anderson

    GZIP output and input strange behaviour

    Nikk & Jak Anderson, Apr 26, 2004, in forum: Java
    Replies:
    2
    Views:
    1,209
    Roedy Green
    Apr 26, 2004
  2. Miroslaw Osys

    very strange gcc output

    Miroslaw Osys, Sep 7, 2003, in forum: C Programming
    Replies:
    4
    Views:
    475
    Emmanuel Delahaye
    Sep 7, 2003
  3. Harvey Twyman
    Replies:
    8
    Views:
    589
    August Derleth
    Oct 25, 2003
  4. Materialised

    Strange output problem

    Materialised, Apr 1, 2004, in forum: C Programming
    Replies:
    15
    Views:
    519
    Dan Pop
    Apr 6, 2004
  5. Paul
    Replies:
    1
    Views:
    447
Loading...

Share This Page