Why do C and C++ allow the expression (int) + 4*5?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







81















(int) + 4*5;


Why is this (adding a type with a value) possible? (tried with g++ and gcc.)



I know that it doesn't make sense (and has no effect), but I want to know why this is possible.










share|improve this question




















  • 19





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 45





    There is a useful tool called cppinsights that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)

    – Nikita Kniazev
    Apr 21 at 21:02






  • 22





    This statement is equivalent to +(int)+ 4*5; and -(int)- 4*5; and -+-+-(int)-+-+- 4*5; and less poetically ;

    – chqrlie
    Apr 22 at 0:12






  • 16





    What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.

    – Carsten S
    Apr 22 at 6:26






  • 5





    Shouldn't C++ warn you that C-type casts are not recommended in C++?

    – Mr Lister
    Apr 22 at 9:38




















81















(int) + 4*5;


Why is this (adding a type with a value) possible? (tried with g++ and gcc.)



I know that it doesn't make sense (and has no effect), but I want to know why this is possible.










share|improve this question




















  • 19





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 45





    There is a useful tool called cppinsights that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)

    – Nikita Kniazev
    Apr 21 at 21:02






  • 22





    This statement is equivalent to +(int)+ 4*5; and -(int)- 4*5; and -+-+-(int)-+-+- 4*5; and less poetically ;

    – chqrlie
    Apr 22 at 0:12






  • 16





    What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.

    – Carsten S
    Apr 22 at 6:26






  • 5





    Shouldn't C++ warn you that C-type casts are not recommended in C++?

    – Mr Lister
    Apr 22 at 9:38
















81












81








81


8






(int) + 4*5;


Why is this (adding a type with a value) possible? (tried with g++ and gcc.)



I know that it doesn't make sense (and has no effect), but I want to know why this is possible.










share|improve this question
















(int) + 4*5;


Why is this (adding a type with a value) possible? (tried with g++ and gcc.)



I know that it doesn't make sense (and has no effect), but I want to know why this is possible.







c++ c casting language-lawyer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 23 at 18:40







Ernest Bredar

















asked Apr 21 at 14:29









Ernest BredarErnest Bredar

56229




56229








  • 19





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 45





    There is a useful tool called cppinsights that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)

    – Nikita Kniazev
    Apr 21 at 21:02






  • 22





    This statement is equivalent to +(int)+ 4*5; and -(int)- 4*5; and -+-+-(int)-+-+- 4*5; and less poetically ;

    – chqrlie
    Apr 22 at 0:12






  • 16





    What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.

    – Carsten S
    Apr 22 at 6:26






  • 5





    Shouldn't C++ warn you that C-type casts are not recommended in C++?

    – Mr Lister
    Apr 22 at 9:38
















  • 19





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 45





    There is a useful tool called cppinsights that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)

    – Nikita Kniazev
    Apr 21 at 21:02






  • 22





    This statement is equivalent to +(int)+ 4*5; and -(int)- 4*5; and -+-+-(int)-+-+- 4*5; and less poetically ;

    – chqrlie
    Apr 22 at 0:12






  • 16





    What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.

    – Carsten S
    Apr 22 at 6:26






  • 5





    Shouldn't C++ warn you that C-type casts are not recommended in C++?

    – Mr Lister
    Apr 22 at 9:38










19




19





same as (int)-4*5

– P__J__
Apr 21 at 15:29





same as (int)-4*5

– P__J__
Apr 21 at 15:29




45




45





There is a useful tool called cppinsights that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)

– Nikita Kniazev
Apr 21 at 21:02





There is a useful tool called cppinsights that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)

– Nikita Kniazev
Apr 21 at 21:02




22




22





This statement is equivalent to +(int)+ 4*5; and -(int)- 4*5; and -+-+-(int)-+-+- 4*5; and less poetically ;

– chqrlie
Apr 22 at 0:12





This statement is equivalent to +(int)+ 4*5; and -(int)- 4*5; and -+-+-(int)-+-+- 4*5; and less poetically ;

– chqrlie
Apr 22 at 0:12




16




16





What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.

– Carsten S
Apr 22 at 6:26





What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.

– Carsten S
Apr 22 at 6:26




5




5





Shouldn't C++ warn you that C-type casts are not recommended in C++?

– Mr Lister
Apr 22 at 9:38







Shouldn't C++ warn you that C-type casts are not recommended in C++?

– Mr Lister
Apr 22 at 9:38














2 Answers
2






active

oldest

votes


















134














The + here is unary + operator, not the binary addition operator. There's no addition happening here.



Also, the syntax (int) is used for typecasting.



You can re-read that statement as



(int) (+ 4) * 5;    


which is parsed as



((int) (+ 4)) * (5);    


which says,




  1. Apply the unary + operator on the integer constant value 4.

  2. typecast to an int

  3. multiply with operand 5


This is similar to (int) (- 4) * (5);, where the usage of the unary operator is more familiar.



In your case, the unary + and the cast to int - both are redundant.






share|improve this answer





















  • 48





    "Casting", not "typecasting". Typecasting is something that happens to actors.

    – Keith Thompson
    Apr 21 at 23:41






  • 8





    (+ 4) is not make the operand +4, it means apply the unary + to operand 4, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c is probably false and sizeof +"a" is probably not 2.

    – chqrlie
    Apr 22 at 0:07






  • 7





    "both are redundant" - the whole thing is redundant, just like 42; :-)

    – paxdiablo
    Apr 22 at 8:17






  • 11





    I see nothing wrong with using the term Type Casting. It seems I'm not the only one.

    – Ben
    Apr 22 at 15:28






  • 14





    @Ben Typecasting is not type casting.

    – Kenneth K.
    Apr 22 at 17:05



















37














This is interpreted as ((int)(+4)) * 5. That is, an expression +4 (a unary plus operator applied to a literal 4), cast to type int with a C-style cast, and the result multiplied by 5.






share|improve this answer
























    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%2f55783615%2fwhy-do-c-and-c-allow-the-expression-int-45%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









    134














    The + here is unary + operator, not the binary addition operator. There's no addition happening here.



    Also, the syntax (int) is used for typecasting.



    You can re-read that statement as



    (int) (+ 4) * 5;    


    which is parsed as



    ((int) (+ 4)) * (5);    


    which says,




    1. Apply the unary + operator on the integer constant value 4.

    2. typecast to an int

    3. multiply with operand 5


    This is similar to (int) (- 4) * (5);, where the usage of the unary operator is more familiar.



    In your case, the unary + and the cast to int - both are redundant.






    share|improve this answer





















    • 48





      "Casting", not "typecasting". Typecasting is something that happens to actors.

      – Keith Thompson
      Apr 21 at 23:41






    • 8





      (+ 4) is not make the operand +4, it means apply the unary + to operand 4, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c is probably false and sizeof +"a" is probably not 2.

      – chqrlie
      Apr 22 at 0:07






    • 7





      "both are redundant" - the whole thing is redundant, just like 42; :-)

      – paxdiablo
      Apr 22 at 8:17






    • 11





      I see nothing wrong with using the term Type Casting. It seems I'm not the only one.

      – Ben
      Apr 22 at 15:28






    • 14





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05
















    134














    The + here is unary + operator, not the binary addition operator. There's no addition happening here.



    Also, the syntax (int) is used for typecasting.



    You can re-read that statement as



    (int) (+ 4) * 5;    


    which is parsed as



    ((int) (+ 4)) * (5);    


    which says,




    1. Apply the unary + operator on the integer constant value 4.

    2. typecast to an int

    3. multiply with operand 5


    This is similar to (int) (- 4) * (5);, where the usage of the unary operator is more familiar.



    In your case, the unary + and the cast to int - both are redundant.






    share|improve this answer





















    • 48





      "Casting", not "typecasting". Typecasting is something that happens to actors.

      – Keith Thompson
      Apr 21 at 23:41






    • 8





      (+ 4) is not make the operand +4, it means apply the unary + to operand 4, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c is probably false and sizeof +"a" is probably not 2.

      – chqrlie
      Apr 22 at 0:07






    • 7





      "both are redundant" - the whole thing is redundant, just like 42; :-)

      – paxdiablo
      Apr 22 at 8:17






    • 11





      I see nothing wrong with using the term Type Casting. It seems I'm not the only one.

      – Ben
      Apr 22 at 15:28






    • 14





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05














    134












    134








    134







    The + here is unary + operator, not the binary addition operator. There's no addition happening here.



    Also, the syntax (int) is used for typecasting.



    You can re-read that statement as



    (int) (+ 4) * 5;    


    which is parsed as



    ((int) (+ 4)) * (5);    


    which says,




    1. Apply the unary + operator on the integer constant value 4.

    2. typecast to an int

    3. multiply with operand 5


    This is similar to (int) (- 4) * (5);, where the usage of the unary operator is more familiar.



    In your case, the unary + and the cast to int - both are redundant.






    share|improve this answer















    The + here is unary + operator, not the binary addition operator. There's no addition happening here.



    Also, the syntax (int) is used for typecasting.



    You can re-read that statement as



    (int) (+ 4) * 5;    


    which is parsed as



    ((int) (+ 4)) * (5);    


    which says,




    1. Apply the unary + operator on the integer constant value 4.

    2. typecast to an int

    3. multiply with operand 5


    This is similar to (int) (- 4) * (5);, where the usage of the unary operator is more familiar.



    In your case, the unary + and the cast to int - both are redundant.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 23 at 13:48

























    answered Apr 21 at 14:31









    Sourav GhoshSourav Ghosh

    114k16137195




    114k16137195








    • 48





      "Casting", not "typecasting". Typecasting is something that happens to actors.

      – Keith Thompson
      Apr 21 at 23:41






    • 8





      (+ 4) is not make the operand +4, it means apply the unary + to operand 4, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c is probably false and sizeof +"a" is probably not 2.

      – chqrlie
      Apr 22 at 0:07






    • 7





      "both are redundant" - the whole thing is redundant, just like 42; :-)

      – paxdiablo
      Apr 22 at 8:17






    • 11





      I see nothing wrong with using the term Type Casting. It seems I'm not the only one.

      – Ben
      Apr 22 at 15:28






    • 14





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05














    • 48





      "Casting", not "typecasting". Typecasting is something that happens to actors.

      – Keith Thompson
      Apr 21 at 23:41






    • 8





      (+ 4) is not make the operand +4, it means apply the unary + to operand 4, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c is probably false and sizeof +"a" is probably not 2.

      – chqrlie
      Apr 22 at 0:07






    • 7





      "both are redundant" - the whole thing is redundant, just like 42; :-)

      – paxdiablo
      Apr 22 at 8:17






    • 11





      I see nothing wrong with using the term Type Casting. It seems I'm not the only one.

      – Ben
      Apr 22 at 15:28






    • 14





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05








    48




    48





    "Casting", not "typecasting". Typecasting is something that happens to actors.

    – Keith Thompson
    Apr 21 at 23:41





    "Casting", not "typecasting". Typecasting is something that happens to actors.

    – Keith Thompson
    Apr 21 at 23:41




    8




    8





    (+ 4) is not make the operand +4, it means apply the unary + to operand 4, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c is probably false and sizeof +"a" is probably not 2.

    – chqrlie
    Apr 22 at 0:07





    (+ 4) is not make the operand +4, it means apply the unary + to operand 4, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c is probably false and sizeof +"a" is probably not 2.

    – chqrlie
    Apr 22 at 0:07




    7




    7





    "both are redundant" - the whole thing is redundant, just like 42; :-)

    – paxdiablo
    Apr 22 at 8:17





    "both are redundant" - the whole thing is redundant, just like 42; :-)

    – paxdiablo
    Apr 22 at 8:17




    11




    11





    I see nothing wrong with using the term Type Casting. It seems I'm not the only one.

    – Ben
    Apr 22 at 15:28





    I see nothing wrong with using the term Type Casting. It seems I'm not the only one.

    – Ben
    Apr 22 at 15:28




    14




    14





    @Ben Typecasting is not type casting.

    – Kenneth K.
    Apr 22 at 17:05





    @Ben Typecasting is not type casting.

    – Kenneth K.
    Apr 22 at 17:05













    37














    This is interpreted as ((int)(+4)) * 5. That is, an expression +4 (a unary plus operator applied to a literal 4), cast to type int with a C-style cast, and the result multiplied by 5.






    share|improve this answer




























      37














      This is interpreted as ((int)(+4)) * 5. That is, an expression +4 (a unary plus operator applied to a literal 4), cast to type int with a C-style cast, and the result multiplied by 5.






      share|improve this answer


























        37












        37








        37







        This is interpreted as ((int)(+4)) * 5. That is, an expression +4 (a unary plus operator applied to a literal 4), cast to type int with a C-style cast, and the result multiplied by 5.






        share|improve this answer













        This is interpreted as ((int)(+4)) * 5. That is, an expression +4 (a unary plus operator applied to a literal 4), cast to type int with a C-style cast, and the result multiplied by 5.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 21 at 14:32









        Igor TandetnikIgor Tandetnik

        34.2k33660




        34.2k33660






























            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%2f55783615%2fwhy-do-c-and-c-allow-the-expression-int-45%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

            He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

            Bunad

            Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum