#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Newb question with C programming


    So I don't understand why my software doesn't show that
    1000000 x 1000000 = 1000000000000

    Instead it shows a different result

    Code:
    #include<stdio. h>
    main(void)
    {
    signed long long int m, n=1000000;
    m = 1000000;
    
    printf("m*n=%d \n", m*n);
    		}
    The result it shows me isn't 1000000000000, but -727379968

    If I do "double" instead of "int" it also doesn't help, it gives a slightly different result than if I do "signed long long int", but still not correct.

    What's the problem here?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Your Problem is here.

    Code:
    printf("m*n=%d \n", m*n);
    If you are using signed long long int then printing it with %d is wrong.While printing long value you should put "l" before it. Here it is long long so it will be "%lld" in the printf.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Here you initialized as long long double

    asking to print int

    modify your print statement as
    Code:
    	printf("m*n=%lli\n", m*n);
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Ah, I was not aware of this little rule..thank you!

IMN logo majestic logo threadwatch logo seochat tools logo