printf() issue on Windows

This is a discussion on printf() issue on Windows within the C Programming forums, part of the General Programming Boards category; I'm having trouble getting the value of a unsigned long long to print to stdout... here's the program: Code: // ...

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    Question printf() issue on Windows

    I'm having trouble getting the value of a unsigned long long to print to stdout... here's the program:
    Code:
    // Some code omitted
    
    int main()
    {
    	unsigned long long d, t;
    	unsigned int f = 0;
    
    	d = 5;
    	t = 9;
    
    	printf("d = %lld, t = %lld.\n", d, t);
    	printf("Is d prime? %d\nIs t prime? %d\n", isPrime(d), isPrime(t));
    
    	for(t = 2; f < 1000; t++)
    	{
    		if(isPrime(t)) { printf("%lld\n", t); f++; }
    	}
    	
    	return 0;
    }
    It compiles fine, with no errors (GCC, 'gcc -Wall -o primetest.exe primetest.c') but the first few lines of output are:
    Code:
    d = 5, t = 0.
    Is d prime? 1
    Is t prime? 0
    2
    3
    5
    7
    ...
    It should, by my reasoning, say 'd = 5, t = 9', yet it doesn't.
    To complicate things, this only occurs under (you guessed it) Windows. (Windows 98) It compiles fine under Windows, Linux and OS/2, but gives the expected output '...t = 9' only on Linux and OS/2. Windows gives t = 0.

    So I tried to simplify it with:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	unsigned long long a = 1, b = 2, c = 3, d = 4;
    
    	printf("Output:");
    
    	printf("\na = %lld", a);
    	printf("\na = %lld, b = %lld", a, b);
    	printf("\na = %lld, b = %lld, c = %lld", a, b, c);
    	printf("\na = %lld, b = %lld, c = %lld, d = %lld", a, b, c, d);
    
    	printf("\n");
    	return 0;
    }
    Which gives:
    Code:
    Output:
    a = 1
    a = 1, b = 0
    a = 1, b = 0, c = 2
    a = 1, b = 0, c = 2, d = 0
    Again, only on Windows! OS/2 and Linux once again agree with the output of:
    Code:
    ...
    a = 1, b = 2, c = 3, d = 4
    I'm clueless as to what's going on... thanks in advance for any help.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    -std='c99' maybe?

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by n7yap
    -std='c99' maybe?
    As written, that gives:
    cc1.exe: error: unrecognized command line option "-std='99'"
    If I remove the single quotes, it accepts it, but the problem is not solved using:
    gcc -std=c99 -Wall -o primetest.exe primetest.c
    I've no idea what -std=c99 does, and I haven't been able to find any documentation on it.

    I'm using gcc (Mingw) 3.4.2 btw.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Using an unsigned long long requires C99 support. C99 is the latest C Standard. I don't use GCC, but I remembered reading that you have to turn on C99 support with a command-line switch. I found the information here: C Dialect Options

    Yes, it should be -std=c99.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Looks like your library's printf doesn't understand %lld, but your compiler understands long long.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    MinGW uses the Microsoft C Run-time library, and that library does not support C99. It requires %I64 instead of %ll for printf. The GCC compiler supports C99, with the command-line switch, so it supports the long long data type. The equivalent data type in VC is _int64.
    Last edited by n7yap; 09-21-2005 at 10:11 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    So that's why it wouldn't work. What about long doubles?
    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.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In Win32 programming, however, the long double data type maps to the double, 64-bit precision data type.
    That was my problem . . . thanks.
    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.

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    Smile Problem Solved

    Quote Originally Posted by n7yap
    MinGW uses the Microsoft C Run-time library, and that library does not support C99. It requires %I64 instead of %ll for printf. The GCC compiler supports C99, with the command-line switch, so it supports the long long data type. The equivalent data type in VC is _int64.
    That solved it! The odd output makes much more sense now, and the %I64 corrects the problem. (Do they do this just to make porting apps harder?)

    The program compiles & runs fine without the -std=c99 (the documentation said the default dialect uses some features of c99... long long must be one of them.). Still, good to know.

    Thanks to everyone who helped.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Microsoft really needs to support C99 in VC. It's long overdue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  3. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 10:25 PM
  4. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 11:14 AM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 07:59 PM

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