Thread: type conversions

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    type conversions

    Hi guys, I've been studying type conversions and have some problems when running a simple display program. It prints out what I think might be some garbage and I do not get why. Can you take a look and tell me where I should look for an error? Thanks.
    Code:
    #include <stdio.h>
    
    
    main()
    {
    char c = '\1';
    short int s = 2;
    int i = -3;
    long int m = 5;
    float f = 6.5;
    double d = 7.5;
    
    printf("%c %hd %d %ld %f %f\n", c, s, i, m, f, d);
    
    printf("%d  %ld  %f  %ld %ld %d\n", c * i, s + m, f / c, d / s, f - d, (int) f); 
    /*
    (int, long int, float, double, double, int)
    */
    return 0;
    }
    The results I am getting are:
    2 -3 5 6.500000 7.500000
    -3 7 6.500000 0 1074659328 0
    It looks to me as if something does not get printed out...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I like Lint's helpful information.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char c = '\1';
       short int s = 2;
       int i = -3;
       long int m = 5;
       float f = 6.5;
       double d = 7.5;
    
       printf("%c %hd %d %ld %f %f\n", c, s, i, m, f, d);
    
       printf("%d  %ld  %f  %ld %ld %d\n", c * i, s + m, f / c, d / s, f - d, (int) f);
    /*
    test.c 15: [559 Warning] Size of argument no. 5 inconsistent with format
    test.c 15: [559 Warning] Size of argument no. 6 inconsistent with format
    */
       return 0;
    }
    Use %f for float or double values with printf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Oh, right, there is a difference between scanf and printf with specifications! Thanks! I really appreciate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM