problem bei Umrechnung von Interger



  • #include <stdio.h>

    main(){

    short c=10000;
    short b = 10*c;
    unsigned long d = b;

    printf("%d\n",d);
    }

    aus meinem computer ,bekomme ich -31072
    aber ich habe da ein unsigned long typ gehabt?wie kann es eine Minus-Wert ausgeben?

    ein andres Programm
    #include <stdio.h>

    main(){

    short c=10000;
    short b = 10*c;

    printf("%x\n",b);

    }
    dafür bekomme ich ffff86a0.
    Aber der typ "short" kann nur 16 bit haben??

    Könnte Jemand mir das phänomen entschlüssln?
    vielen dank.



  • kun.T schrieb:

    #include  <stdio.h>
    
    main(){
    
       short c=10000;
       short b = 10*c; // das Ergebnis ist wohl bereits zu groß für short
       unsigned long d = b;	 
    
       printf("%d\n",d); // du gibst d auch als 'signed' aus. Du willst eher %u
    }
    
    #include  <stdio.h>
    
    main(){
    
      short c=10000;
      short b = 10*c; // s.o.
     
      printf("%x\n",b); // du solltest den "length modifier" für 'short' benutzen (%h...) [1]
    }
    

    [1] http://www.die.net/doc/linux/man/man3/printf.3.html


Anmelden zum Antworten