Wie ist das möglich??



  • Hi Leute!
    Sagt mal, seit wann kann man booleans auf 10 setzen?

    #pragma hdrstop
    #include <condefs.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
            bool test = false;
            for(int i=0;i<10;i++)
            {
                    test += true;
                    printf("%d count my boolean is: %d\n", i, test);
            }
            getch();
            return 0;
    }
    

    😮 😕 🕶 😋

    Die Ausgabe:

    0 count my boolean is: 1
    1 count my boolean is: 2
    2 count my boolean is: 3
    3 count my boolean is: 4
    4 count my boolean is: 5
    5 count my boolean is: 6
    6 count my boolean is: 7
    7 count my boolean is: 8
    8 count my boolean is: 9
    9 count my boolean is: 10

    LOL 😉 nee mal ernst is das 'nen Bug oder wie?

    Gruss,
    ~cp code_pilot 😃



  • Also hier auf meinem System gibt folgende Datei

    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(int argc, char* argv[]) 
    { 
            bool test = false; 
    
            for(int i=0;i<10;i++) 
            { 
                    test += true; 
                    printf("%d count my boolean is: %d\n", i, test); 
            } 
            return 0; 
    }
    

    folgendes aus:

    0 count my boolean is: 1
    1 count my boolean is: 1
    2 count my boolean is: 1
    3 count my boolean is: 1
    4 count my boolean is: 1
    5 count my boolean is: 1
    6 count my boolean is: 1
    7 count my boolean is: 1
    8 count my boolean is: 1
    9 count my boolean is: 1
    


  • Visual C++ .NET 2003 macht auch alles 1er. 🙂



  • Boolean ist in irgendeiner Datei per Typedef einfach nur ein int...



  • nein, in C++ ist bool ein Builtin Type! (und der Code soll C++ sein, auch wenn er extrem wie C aussieht :))

    In C99 ist bool ein typedef (auf _Bool) 🙂



  • gcc macht auch nur 1er draus. Was fürnen komischen Compiler benutzt du 🙂



  • Dev-C++ 4.9.8.0:

    #include <iostream>
    #include <conio.h> 
    
    int main() 
    { 
        bool test = false; 
        for( int i=0; i<10; ++i ) 
        { 
             test += true; 
             std::cout << i << " count my boolean is: " << test << std::endl;
        } 
        getch(); 
    }
    

    output:
    0 count my boolean is: 1
    1 count my boolean is: 1
    2 count my boolean is: 1
    3 count my boolean is: 1
    4 count my boolean is: 1
    5 count my boolean is: 1
    6 count my boolean is: 1
    7 count my boolean is: 1
    8 count my boolean is: 1
    9 count my boolean is: 1



  • E.H: Das brachte jetzt nicht viel, DevC++ verwendet doch auch die gcc, hm?



  • Ne standardmäßig den MinGW, aber das ändert ja nicht viel 😉



  • TriPhoenix schrieb:

    gcc macht auch nur 1er draus. Was fürnen komischen Compiler benutzt du 🙂

    Er benutzt den Borland C++Builder.



  • bool ist ein signed byte oder ?
    AFAIK ist alles ungleich 0 == true und == 0 eben false



  • Jo, meiner gibt 3 Warnungen aus:
    warning C4804: '+=' : unsafe use of type 'bool' in operation
    warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    warning C4508: 'main' : function should return a value; 'void' return type assumed

    ...und zeigt alles Einsen an!



  • michaelwitzik schrieb:

    Jo, meiner gibt 3 Warnungen aus:
    warning C4804: '+=' : unsafe use of type 'bool' in operation
    warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    warning C4508: 'main' : function should return a value; 'void' return type assumed

    Wobei die letzte Warnung nicht gerechtfertigt ist.



  • vermutlich speichert der BCB intern bool als long bzw. int, weil damit x86er besonders schnell arbeiten können. Nur wurde der += Operator für bool nicht richtig unschändlich gemacht. Bei printf tritt der Fehler eben auf, weil der Typ ja explizit angegeben wird (würde mich wundern, wenn das beim operator<<(ostream,bool) passieren würde)


Anmelden zum Antworten