Thread: How to printf long long data?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    How to printf long long data?

    When I input 1000(the maxium input value is 1000), it should show 125751375750. But fail. How can I printf it in the large number?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int n;
    int *w;
    
    int main()
    {
    	int i, n, x;
    	scanf("%d", &n);
    	w = malloc(n * sizeof *w);
    	for (i = 0; i < n; i++)
    		scanf("%d", &w[i]);
    	for (i = 0; i < n; i++)
    		printf("%d %d %ld\n", i+1, w[i],
    			   w[i]*(w[i]+1)*(w[i]+2)*(w[i]+3)/8);
    	return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When I input 1000(the maxium input value is 1000), it should show
    >125751375750. But fail. How can I printf it in the large number?
    It sounds like an input problem where you're pulling from memory that you never wrote to.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > How to printf long long data?
    Which compiler are you using?

    > printf("%d %d %ld\n"
    In C99, the format for long long is %lld

    Furthermore, you need to introduce a cast into your expression to make it all evaluate using long long arithmetic. At the moment, everything will be truncated to int precision.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    thanks for reply. I try to use a variable to store my data.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int n;
    int *w;
    
    int main()
    {
    	int i, n;
    	long long t;
    	scanf("%d", &n);
    	w = malloc(n * sizeof *w);
    	for (i = 0; i < n; i++)
    		scanf("%d", &w[i]);
    	for (i = 0; i < n; i++) { 
    		t = (long long)w[i]*(w[i]+1)*(w[i]+2)*(w[i]+3)/8;
    		printf("%d %d %lld\n", i+1, w[i], t);
    	}
    	return 0;
    }
    It runs fine.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Beware that the MSVC runtime library treats %lld as %ld, so if you try to print a number that can't be stored in a long but can fit perfectly well into a long long, it might not work. (MSVC and Dev-C++ use this library.)

    Also you should call free(w) at the end of your program.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  4. Displaying a 'long long' with printf
    By trinitrotoluene in forum C Programming
    Replies: 10
    Last Post: 12-28-2004, 01:32 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM