Why do C and C++ allow the expression (int) + 4*5?What does the unary plus operator do?Cast int to enum in C#What are the differences between a pointer variable and a reference variable in C++?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is it faster to process a sorted array than an unsorted array?Compiling an application for use in highly radioactive environments

Co-worker works way more than he should

Work requires me to come in early to start computer but wont let me clock in to get paid for it

Restricting the options of a lookup field, based on the value of another lookup field?

How do I produce this Greek letter koppa: Ϟ in pdfLaTeX?

Could moose/elk survive in the Amazon forest?

How to have a sharp product image?

Can I criticise the more senior developers around me for not writing clean code?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

Would the change in enthalpy (ΔH) for the dissolution of urea in water be positive or negative?

Is it acceptable to use working hours to read general interest books?

What is the best way to deal with NPC-NPC combat?

Find a stone which is not the lightest one

Cayley's Matrix Notation

Mistake in years of experience in resume?

Philosophical question on logistic regression: why isn't the optimal threshold value trained?

Why did C use the -> operator instead of reusing the . operator?

What is purpose of DB Browser(dbbrowser.aspx) under admin tool?

SFDX - Create Objects with Custom Properties

Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?

Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

How important is it that $TERM is correct?

Why do distances seem to matter in the Foundation world?

Where was the County of Thurn und Taxis located?



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


What does the unary plus operator do?Cast int to enum in C#What are the differences between a pointer variable and a reference variable in C++?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is it faster to process a sorted array than an unsorted array?Compiling an application for use in highly radioactive environments






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








66















(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









New contributor




Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 16





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 44





    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






  • 21





    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






  • 15





    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


















66















(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









New contributor




Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 16





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 44





    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






  • 21





    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






  • 15





    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














66












66








66


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









New contributor




Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












(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









New contributor




Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago







Ernest Bredar













New contributor




Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 21 at 14:29









Ernest BredarErnest Bredar

48428




48428




New contributor




Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ernest Bredar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 16





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 44





    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






  • 21





    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






  • 15





    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













  • 16





    same as (int)-4*5

    – P__J__
    Apr 21 at 15:29






  • 44





    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






  • 21





    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






  • 15





    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








16




16





same as (int)-4*5

– P__J__
Apr 21 at 15:29





same as (int)-4*5

– P__J__
Apr 21 at 15:29




44




44





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




21




21





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




15




15





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


















123














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




















  • 44





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

    – Keith Thompson
    Apr 21 at 23:41






  • 7





    (+ 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






  • 9





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

    – Ben
    Apr 22 at 15:28






  • 13





    @Ben Typecasting is not type casting.

    – Kenneth K.
    Apr 22 at 17:05


















35














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
    );



    );






    Ernest Bredar is a new contributor. Be nice, and check out our Code of Conduct.









    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









    123














    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




















    • 44





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

      – Keith Thompson
      Apr 21 at 23:41






    • 7





      (+ 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






    • 9





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

      – Ben
      Apr 22 at 15:28






    • 13





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05















    123














    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




















    • 44





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

      – Keith Thompson
      Apr 21 at 23:41






    • 7





      (+ 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






    • 9





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

      – Ben
      Apr 22 at 15:28






    • 13





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05













    123












    123








    123







    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 2 days ago

























    answered Apr 21 at 14:31









    Sourav GhoshSourav Ghosh

    112k16137195




    112k16137195







    • 44





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

      – Keith Thompson
      Apr 21 at 23:41






    • 7





      (+ 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






    • 9





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

      – Ben
      Apr 22 at 15:28






    • 13





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05












    • 44





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

      – Keith Thompson
      Apr 21 at 23:41






    • 7





      (+ 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






    • 9





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

      – Ben
      Apr 22 at 15:28






    • 13





      @Ben Typecasting is not type casting.

      – Kenneth K.
      Apr 22 at 17:05







    44




    44





    "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




    7




    7





    (+ 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




    9




    9





    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




    13




    13





    @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













    35














    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



























      35














      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

























        35












        35








        35







        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

        33.6k33659




        33.6k33659




















            Ernest Bredar is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Ernest Bredar is a new contributor. Be nice, and check out our Code of Conduct.












            Ernest Bredar is a new contributor. Be nice, and check out our Code of Conduct.











            Ernest Bredar is a new contributor. Be nice, and check out our Code of Conduct.














            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

            Sum ergo cogito? 1 nng

            三茅街道4182Guuntc Dn precexpngmageondP