Thread: c programming

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    c programming

    Hi!
    I want to learn C programming.
    I have problem whit print out.
    if I have:

    Code:
    double    value_1;
    int       value_2;
    // how do I print out double ....?
    printf(?????)
    if I have

    Code:
    int A=5;
    double B=10.5;
    result=(double) A/B
    // the result type is double but how do I print it out.
    printf(????);

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    printf("%f",(double)(A/B));
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    printf("%lf\n",mydouble);

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    %f is sufficient for printf(). %lf only makes a difference with the scanf() family of functions.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    printf("%f",A/B);
    no need to typecast.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by cbastard
    Code:
    printf("%f",A/B);
    no need to typecast.
    You don't need to typecast that because B is already a double. If there is one double in the equation, the whole equation has a double value.

    You would need a typecast if both numbers were ints, though (unless you want truncuation):
    Code:
    #include <stdio.h>
    
    int main(void) {
        int x = 3, y = 2;
        double d = 2;
    
        printf("%f\n", x/d);
        printf("%f\n", x/y);
        printf("%f\n", (double)x/y);
    
        return 0;
    }
    Output:
    Code:
    1.500000
    1
    1.500000
    The second printf() truncuates the result of x/y. In other words, the decimal portion of the number is discarded.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,162
    Try reading the manual page for printf() next time. We're not here to tell you how to use foo() for every new foo() you come across.
    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.

Popular pages Recent additions subscribe to a feed