Does char == char implicitly cast both operands to int?












2















I am freshening up my C++ skills, using a copy of the german book "C++ - Das Übungsbuch"; 5. Auflage from mitp.



Exercise 8.1 poses the following question (translated and reduced to the relevant content):




8.1 Given the following definition:



char c = 'q';



Determine the type to which the operands will be converted during evaluation of the following statements:



a) c == 'Q'




My answer would have been char, as c is declared as a char and as far as I understood, 'Q' is a character literal. The solution however states:




a) The type of both operands will be converted to int




This confuses me. Is it really the case, that a char == char comparison is implicitly converted to int == int and why would this be done? If the question were




a) c == 82




I could have understood that 82 would be interpreted as an integer constant and therefor c would be converted to int as well, but the answer explicitly mentions both operands to be converted.



Can someone explain this to me please?










share|improve this question




















  • 5





    Yes, technically both operands get casted to int. However, most compilers will not generate the actual code to do that, because a compiler is permitted to perform any optimization that has no observable effect, and it's pretty much impossible to observe the effect of this optimization. But, technically, both are promoted to int.

    – Sam Varshavchik
    Jan 1 at 16:35






  • 2





    Just a bit of terminology: this question is about implicit conversions, not casts. A cast is something you write in your source code to tell the compiler to do a conversion. There is no such thing as an implicit cast.

    – Pete Becker
    Jan 1 at 16:48











  • @PeteBecker Thank you, I'll fix that. I already thought that cast wouldn't be the correct term here, but I was lost for the correct word, which you now provided. :)

    – T3 H40
    Jan 1 at 16:50
















2















I am freshening up my C++ skills, using a copy of the german book "C++ - Das Übungsbuch"; 5. Auflage from mitp.



Exercise 8.1 poses the following question (translated and reduced to the relevant content):




8.1 Given the following definition:



char c = 'q';



Determine the type to which the operands will be converted during evaluation of the following statements:



a) c == 'Q'




My answer would have been char, as c is declared as a char and as far as I understood, 'Q' is a character literal. The solution however states:




a) The type of both operands will be converted to int




This confuses me. Is it really the case, that a char == char comparison is implicitly converted to int == int and why would this be done? If the question were




a) c == 82




I could have understood that 82 would be interpreted as an integer constant and therefor c would be converted to int as well, but the answer explicitly mentions both operands to be converted.



Can someone explain this to me please?










share|improve this question




















  • 5





    Yes, technically both operands get casted to int. However, most compilers will not generate the actual code to do that, because a compiler is permitted to perform any optimization that has no observable effect, and it's pretty much impossible to observe the effect of this optimization. But, technically, both are promoted to int.

    – Sam Varshavchik
    Jan 1 at 16:35






  • 2





    Just a bit of terminology: this question is about implicit conversions, not casts. A cast is something you write in your source code to tell the compiler to do a conversion. There is no such thing as an implicit cast.

    – Pete Becker
    Jan 1 at 16:48











  • @PeteBecker Thank you, I'll fix that. I already thought that cast wouldn't be the correct term here, but I was lost for the correct word, which you now provided. :)

    – T3 H40
    Jan 1 at 16:50














2












2








2








I am freshening up my C++ skills, using a copy of the german book "C++ - Das Übungsbuch"; 5. Auflage from mitp.



Exercise 8.1 poses the following question (translated and reduced to the relevant content):




8.1 Given the following definition:



char c = 'q';



Determine the type to which the operands will be converted during evaluation of the following statements:



a) c == 'Q'




My answer would have been char, as c is declared as a char and as far as I understood, 'Q' is a character literal. The solution however states:




a) The type of both operands will be converted to int




This confuses me. Is it really the case, that a char == char comparison is implicitly converted to int == int and why would this be done? If the question were




a) c == 82




I could have understood that 82 would be interpreted as an integer constant and therefor c would be converted to int as well, but the answer explicitly mentions both operands to be converted.



Can someone explain this to me please?










share|improve this question
















I am freshening up my C++ skills, using a copy of the german book "C++ - Das Übungsbuch"; 5. Auflage from mitp.



Exercise 8.1 poses the following question (translated and reduced to the relevant content):




8.1 Given the following definition:



char c = 'q';



Determine the type to which the operands will be converted during evaluation of the following statements:



a) c == 'Q'




My answer would have been char, as c is declared as a char and as far as I understood, 'Q' is a character literal. The solution however states:




a) The type of both operands will be converted to int




This confuses me. Is it really the case, that a char == char comparison is implicitly converted to int == int and why would this be done? If the question were




a) c == 82




I could have understood that 82 would be interpreted as an integer constant and therefor c would be converted to int as well, but the answer explicitly mentions both operands to be converted.



Can someone explain this to me please?







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 16:51







T3 H40

















asked Jan 1 at 16:31









T3 H40T3 H40

1,48072330




1,48072330








  • 5





    Yes, technically both operands get casted to int. However, most compilers will not generate the actual code to do that, because a compiler is permitted to perform any optimization that has no observable effect, and it's pretty much impossible to observe the effect of this optimization. But, technically, both are promoted to int.

    – Sam Varshavchik
    Jan 1 at 16:35






  • 2





    Just a bit of terminology: this question is about implicit conversions, not casts. A cast is something you write in your source code to tell the compiler to do a conversion. There is no such thing as an implicit cast.

    – Pete Becker
    Jan 1 at 16:48











  • @PeteBecker Thank you, I'll fix that. I already thought that cast wouldn't be the correct term here, but I was lost for the correct word, which you now provided. :)

    – T3 H40
    Jan 1 at 16:50














  • 5





    Yes, technically both operands get casted to int. However, most compilers will not generate the actual code to do that, because a compiler is permitted to perform any optimization that has no observable effect, and it's pretty much impossible to observe the effect of this optimization. But, technically, both are promoted to int.

    – Sam Varshavchik
    Jan 1 at 16:35






  • 2





    Just a bit of terminology: this question is about implicit conversions, not casts. A cast is something you write in your source code to tell the compiler to do a conversion. There is no such thing as an implicit cast.

    – Pete Becker
    Jan 1 at 16:48











  • @PeteBecker Thank you, I'll fix that. I already thought that cast wouldn't be the correct term here, but I was lost for the correct word, which you now provided. :)

    – T3 H40
    Jan 1 at 16:50








5




5





Yes, technically both operands get casted to int. However, most compilers will not generate the actual code to do that, because a compiler is permitted to perform any optimization that has no observable effect, and it's pretty much impossible to observe the effect of this optimization. But, technically, both are promoted to int.

– Sam Varshavchik
Jan 1 at 16:35





Yes, technically both operands get casted to int. However, most compilers will not generate the actual code to do that, because a compiler is permitted to perform any optimization that has no observable effect, and it's pretty much impossible to observe the effect of this optimization. But, technically, both are promoted to int.

– Sam Varshavchik
Jan 1 at 16:35




2




2





Just a bit of terminology: this question is about implicit conversions, not casts. A cast is something you write in your source code to tell the compiler to do a conversion. There is no such thing as an implicit cast.

– Pete Becker
Jan 1 at 16:48





Just a bit of terminology: this question is about implicit conversions, not casts. A cast is something you write in your source code to tell the compiler to do a conversion. There is no such thing as an implicit cast.

– Pete Becker
Jan 1 at 16:48













@PeteBecker Thank you, I'll fix that. I already thought that cast wouldn't be the correct term here, but I was lost for the correct word, which you now provided. :)

– T3 H40
Jan 1 at 16:50





@PeteBecker Thank you, I'll fix that. I already thought that cast wouldn't be the correct term here, but I was lost for the correct word, which you now provided. :)

– T3 H40
Jan 1 at 16:50












2 Answers
2






active

oldest

votes


















5














The book is talking about usual arithmetic conversion which happens if both operands have arithmetic types (which is true for all integer types, including char). That leads to integral promotion.






share|improve this answer































    -5














    I think, that it uses for the comparison the ascii number corresponding to your character.
    For example it converts q to 113 and Q to 81.
    So it should compare:



    'Q'=='q' //81==113


    They are casted into integer because C interprets character as number of the ascii table.
    See https://www.rapidtables.com/code/text/ascii-table.html






    share|improve this answer



















    • 1





      The question is about C++, and C and C++ behaves differently

      – Basile Starynkevitch
      Jan 1 at 16:43











    • Haven't read C++ tag sorry, my bad

      – Massimo Simone
      Jan 1 at 16:44






    • 1





      I don't think this question is as much about internal char representation as it is about how integral comparisons work according to the standard.

      – Moira
      Jan 1 at 16:46













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997093%2fdoes-char-char-implicitly-cast-both-operands-to-int%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    The book is talking about usual arithmetic conversion which happens if both operands have arithmetic types (which is true for all integer types, including char). That leads to integral promotion.






    share|improve this answer




























      5














      The book is talking about usual arithmetic conversion which happens if both operands have arithmetic types (which is true for all integer types, including char). That leads to integral promotion.






      share|improve this answer


























        5












        5








        5







        The book is talking about usual arithmetic conversion which happens if both operands have arithmetic types (which is true for all integer types, including char). That leads to integral promotion.






        share|improve this answer













        The book is talking about usual arithmetic conversion which happens if both operands have arithmetic types (which is true for all integer types, including char). That leads to integral promotion.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 16:40









        Some programmer dudeSome programmer dude

        302k25264424




        302k25264424

























            -5














            I think, that it uses for the comparison the ascii number corresponding to your character.
            For example it converts q to 113 and Q to 81.
            So it should compare:



            'Q'=='q' //81==113


            They are casted into integer because C interprets character as number of the ascii table.
            See https://www.rapidtables.com/code/text/ascii-table.html






            share|improve this answer



















            • 1





              The question is about C++, and C and C++ behaves differently

              – Basile Starynkevitch
              Jan 1 at 16:43











            • Haven't read C++ tag sorry, my bad

              – Massimo Simone
              Jan 1 at 16:44






            • 1





              I don't think this question is as much about internal char representation as it is about how integral comparisons work according to the standard.

              – Moira
              Jan 1 at 16:46


















            -5














            I think, that it uses for the comparison the ascii number corresponding to your character.
            For example it converts q to 113 and Q to 81.
            So it should compare:



            'Q'=='q' //81==113


            They are casted into integer because C interprets character as number of the ascii table.
            See https://www.rapidtables.com/code/text/ascii-table.html






            share|improve this answer



















            • 1





              The question is about C++, and C and C++ behaves differently

              – Basile Starynkevitch
              Jan 1 at 16:43











            • Haven't read C++ tag sorry, my bad

              – Massimo Simone
              Jan 1 at 16:44






            • 1





              I don't think this question is as much about internal char representation as it is about how integral comparisons work according to the standard.

              – Moira
              Jan 1 at 16:46
















            -5












            -5








            -5







            I think, that it uses for the comparison the ascii number corresponding to your character.
            For example it converts q to 113 and Q to 81.
            So it should compare:



            'Q'=='q' //81==113


            They are casted into integer because C interprets character as number of the ascii table.
            See https://www.rapidtables.com/code/text/ascii-table.html






            share|improve this answer













            I think, that it uses for the comparison the ascii number corresponding to your character.
            For example it converts q to 113 and Q to 81.
            So it should compare:



            'Q'=='q' //81==113


            They are casted into integer because C interprets character as number of the ascii table.
            See https://www.rapidtables.com/code/text/ascii-table.html







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 1 at 16:39









            Massimo SimoneMassimo Simone

            36




            36








            • 1





              The question is about C++, and C and C++ behaves differently

              – Basile Starynkevitch
              Jan 1 at 16:43











            • Haven't read C++ tag sorry, my bad

              – Massimo Simone
              Jan 1 at 16:44






            • 1





              I don't think this question is as much about internal char representation as it is about how integral comparisons work according to the standard.

              – Moira
              Jan 1 at 16:46
















            • 1





              The question is about C++, and C and C++ behaves differently

              – Basile Starynkevitch
              Jan 1 at 16:43











            • Haven't read C++ tag sorry, my bad

              – Massimo Simone
              Jan 1 at 16:44






            • 1





              I don't think this question is as much about internal char representation as it is about how integral comparisons work according to the standard.

              – Moira
              Jan 1 at 16:46










            1




            1





            The question is about C++, and C and C++ behaves differently

            – Basile Starynkevitch
            Jan 1 at 16:43





            The question is about C++, and C and C++ behaves differently

            – Basile Starynkevitch
            Jan 1 at 16:43













            Haven't read C++ tag sorry, my bad

            – Massimo Simone
            Jan 1 at 16:44





            Haven't read C++ tag sorry, my bad

            – Massimo Simone
            Jan 1 at 16:44




            1




            1





            I don't think this question is as much about internal char representation as it is about how integral comparisons work according to the standard.

            – Moira
            Jan 1 at 16:46







            I don't think this question is as much about internal char representation as it is about how integral comparisons work according to the standard.

            – Moira
            Jan 1 at 16:46




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997093%2fdoes-char-char-implicitly-cast-both-operands-to-int%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$