Thread: doesn't print the real value

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    2

    doesn't print the real value

    Hi I have a function that get a struct, and sum all his values, and return a float, and i want to print that returned float but, for some reason i get weird number, and i don't know whats happening

    Code:
    typedefstructcompetitor_status{    
        float cucumber;
        float carrot;
        float bean;
    
        struct competitor_status * left; /* links to teams with more or same points */
        struct competitor_status * right; /* links to teams with less points */
        
    } competitor_node;
    
    typedef competitor_node * comp_node_ptr;
    
    void printNode(comp_node_ptr node){
        
        if(node == NULL){
            printf("Can't print information from one NULL node. \n");
            return;
        }
        
        float total;
        total = totalLength(node);
        
        printf("%-20s %-5d %20.2f %20.2f %20.2f %20.2f \n",
                node -> name,
                node -> id,
                node -> cucumber,
                node -> carrot,
                node -> bean,
                total
                );
        
    }
    
    
    float totalLength(comp_node_ptr node){
        
        float result;
        result = 0;
        
        result += node -> carrot;
        result += node -> bean;
        result += node -> cucumber;
        
        printf("%.2f \n", result);
        
        return result;
        
        
         }
    inside the function totalLength when it prints out they have the value correct 108.40, but when is returned and printed they have 1121504512.00 . I'm confued now, because is not the first time i had this problem

    This is the output I have
    108.40
    Margaret Mouse 1 12.50 39.70 56.20 1121504512.00


    First green number is the sum of the other green values, this is produced inside the totalLength method, and the red number is when we print that value
    Last edited by jotask; 11-24-2015 at 03:13 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well if you don't prototype your function before calling it, C will assume that totalLength() returns int.

    So it will do another int to float conversion on what is already a float.

    If your compiler doesn't warn about redeclaration errors, you need a better compiler.

    If you're just ignoring compiler warnings, you're an idiot.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    2
    Completly agree with you, I am a completly idiot. The thing is I had the prototype to that function in the wrong file. But the thing is that the compiler doesn't show any compiler warning or anything. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doesn't print what it's supposed to
    By fane in forum C Programming
    Replies: 5
    Last Post: 02-01-2014, 07:31 PM
  2. printf doesn't print on screen
    By Roffemuffe in forum C Programming
    Replies: 4
    Last Post: 05-30-2013, 04:21 AM
  3. Why doesn't this print?
    By Matt Holden in forum C Programming
    Replies: 1
    Last Post: 01-21-2013, 03:04 PM
  4. this code doesn't print any thing with gcc ??
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 11:06 AM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 08:30 AM