data type problem

This is a discussion on data type problem within the C Programming forums, part of the General Programming Boards category; Hi all, I am hoping someone can tell me what I am doing wrong. I want to divide two numbers ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    11

    data type problem

    Hi all, I am hoping someone can tell me what I am doing wrong. I want to divide two numbers and get a decimal from that, but I cannot figure out how to make this work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
      int a=0, b=0;
      float c=0;
      float d=0;
      float f=0;
    
      a = 40749 - 1;
      b = 65536 - 1;
      c = (a/b);
      d = 40748 / 65535;
      f = d * 255;
      printf("a=%d b=%d c=%d d=%d f=%d\n",a,b,c,d,f);
    }
    And, when I compile and run, this is what I get (below), c, d, and f all giving back a 0, not a decimal number.

    %: gcc test.c
    %: a.out
    a=40748 b=65535 c=0 d=0 f=0
    %:

    How do I get a decimal number back? Or, in the case of f, a char/int (because it will be between 0 and 255).

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You cannot divide two ints together and get a decimal (float) result. You must start with (at least) one float. So 40748.0/65535.0 would give you a decimal result.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Three things. First, use %f for floats:
    Code:
    printf("a=%d b=%d c=%f d=%f f=%f\n",a,b,c,d,f);
    Second, a and b, 40748 and 65535, are integers, and the compiler treats them that way unless you indicate otherwise

    Code:
      c = (float)a/(float)b;
      d = 40748.0f / 65535.0f;
    Thirdly (this is really a minor addendum to 2), always prefix floats with "f" even if you are assigning to a float,
    Code:
    float c=0.0f;
    Because while the compiler will convert "0" into "0.0" since c is a float, the final product of your program will be slightly more streamlined if the compiler does not have to do that accounting. Or so I have been told.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    OK, so I just changed everything to floats (for simplicity). And used the %f notation for printing to the screen. It all seems to work!

    Thanks for you help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  3. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 11:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21