this is driving me crazy

This is a discussion on this is driving me crazy within the C Programming forums, part of the General Programming Boards category; Can anyone tell why I am getting c=1 b=0 as the output. I am using the MinGW that comes the ...

  1. #1
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    this is driving me crazy

    Can anyone tell why I am getting c=1 b=0 as the output.
    I am using the MinGW that comes the latest version of windows if it helps
    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
        unsigned long long c=1,b=23;
        printf("c=%llu b=%llu\n",c,b);
        return 0;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    i am getting
    c=1 b=23
    using gcc 3.2.3 on linux

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    funny

    Strangely enough, I am getting the output c=1 b=0
    c=1
    b=23
    with this code
    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
        unsigned long long c=1,b=23;
        printf("c=%llu b=%llu\n",c,b);
        printf("c=%llu\n",c);
        printf("b=%llu\n",b);
        return 0;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by viaxd
    i am getting
    c=1 b=23
    using gcc 3.2.3 on linux
    Thanks, that means MinGW is broken. Its damn crazy!!!!
    The one who says it cannot be done should never interrupt the one who is doing it.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by pinko_liberal
    Can anyone tell why I am getting c=1 b=0 as the output.
    I am using the MinGW that comes the latest version of windows if [/code]
    I meant MinGW that comes with the lastest version of bloodshed.
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > I meant MinGW that comes with the lastest version of bloodshed.
    If you do
    gcc --version
    what do you get?

    unsigned long long c=1,b=23;
    printf( "%d %d\n", sizeof(c), sizeof(b) );
    What do you get?

    > unsigned long long c=1,b=23;
    > printf("c=%llu b=%llu\n",c,b);
    If you compile with
    gcc -S prog.c
    what do you get in prog.s ?

    Compare with calling a function which expects two unsigned long long parameters
    void foo ( unsigned long long p1, unsigned long long p2 );
    foo( c, b );

    Does this fix anything (since printf is a variadic function, and arguments have default promotions)
    unsigned long long c=1,b=23;
    printf("c=%llu b=%llu\n",(unsigned long long)c, (unsigned long long)b );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    gcc.exe (GCC) 3.2 (mingw special 20020817-1)
    Copyright (C) 2002 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    The one who says it cannot be done should never interrupt the one who is doing it.

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Code:
    .file	"junk.c"
    	.def	___main;	.scl	2;	.type	32;	.endef
    	.text
    LC0:
    	.ascii "c=%llu b=%llu\12\0"
    	.align 2
    .globl _main
    	.def	_main;	.scl	2;	.type	32;	.endef
    _main:
    	pushl	%ebp
    	movl	%esp, %ebp
    	subl	$24, %esp
    	andl	$-16, %esp
    	movl	$0, %eax
    	movl	%eax, -20(%ebp)
    	movl	-20(%ebp), %eax
    	call	__alloca
    	call	___main
    	movl	$1, -8(%ebp)
    	movl	$0, -4(%ebp)
    	movl	$23, -16(%ebp)
    	movl	$0, -12(%ebp)
    	subl	$12, %esp
    	pushl	-12(%ebp)
    	pushl	-16(%ebp)
    	pushl	-4(%ebp)
    	pushl	-8(%ebp)
    	pushl	$LC0
    	call	_printf
    	addl	$32, %esp
    	movl	$0, %eax
    	leave
    	ret
    	.def	_printf;	.scl	2;	.type	32;	.endef
    The one who says it cannot be done should never interrupt the one who is doing it.

  9. #9
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by Salem
    Does this fix anything (since printf is a variadic function, and arguments have default promotions)
    unsigned long long c=1,b=23;
    printf("c=%llu b=%llu\n",(unsigned long long)c, (unsigned long long)b );
    I did cast the results to unsigned long long in an attempt to make it work, it didn't help.
    The one who says it cannot be done should never interrupt the one who is doing it.

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    sizeof's are coming to be 8.
    The one who says it cannot be done should never interrupt the one who is doing it.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well on the face of it, the compiler is generating the right code for storing and passing unsigned long long values.
    So perhaps your library implementation of printf isn't up to scratch.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    http://groups.google.com/groups?selm...ay.alphanet.ch
    "%llu", c'est valable pour les compilateurs et bibliothèques (souvent Unix)
    du temps intermédiaire, entre ~1994 et la compatibilité C99 (~2001).
    Attention: il ne suffit pas d'un compilateur qui comprend long long: par
    exemple, GNU C avec Mingw va rater lamentablement avec la version "normale"
    de MSVCRT.DLL, parce que le code de Microsoft ne "comprend" pas "%llu" (je
    crois qu'il considère le deuxième l redondant).

    Avec une bibliothèque de Microsoft (ou équivalent), et en particulier
    MSVCRT.DLL, la solution est "%I64d"
    In short, MSVCRT, which MinGW uses, does not understand "%llu" and interprets it as "%lu". Therefore, you are printing the two halves of the first long long you pass to printf(). You can use "%I64d" and "%I64u" to print signed and unsigned long longs respectively.

    Edit: Found the MinGW page on the issue:
    http://www.mingw.org/MinGWiki/index.php/long%20long
    Last edited by anonytmouse; 06-05-2004 at 07:45 AM.

  13. #13
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by anonytmouse
    In short, MSVCRT, which MinGW uses, does not understand "%llu" and interprets it as "%lu". Therefore, you are printing the two halves of the first long long you pass to printf(). You can use "%I64d" and "%I64u" to print signed and unsigned long longs respectively.

    Edit: Found the MinGW page on the issue:
    http://www.mingw.org/MinGWiki/index.php/long%20long
    thanks! That explains it.
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This program is driving my crazy!
    By zyphirr in forum C Programming
    Replies: 8
    Last Post: 12-05-2008, 04:36 PM
  2. This is driving me crazy! 256 colors palette
    By manugarciac in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2007, 05:36 PM
  3. Driving me crazy
    By GUI_XP in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2002, 09:19 PM
  4. IE 6 and Banner Ads is driving me crazy
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-09-2002, 01:18 AM
  5. Replies: 0
    Last Post: 10-29-2001, 10:40 PM

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