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

Feels like I am getting dragged in office politics

Is it possible to Ready a spell to be cast just before the start of your next turn by having the trigger be an ally's attack?

Find functions f & g such that f(x+y)=g (x.y) for all x & y?

How to back up a running remote server?

Do I have to worry about players making “bad” choices on level up?

Why was Germany not as successful as other Europeans in establishing overseas colonies?

Volunteering in England

Stark VS Thanos

Can fracking help reduce CO2?

Sci-fi novel series with instant travel between planets through gates. A river runs through the gates

What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?

"ne paelici suspectaretur" (Tacitus)

How do I tell my manager that he's wrong?

Pawn Sacrifice Justification

Why do TACANs not have a symbol for compulsory reporting on IFR Enroute Low Altitude charts?

How to determine the actual or "true" resolution of a digital photograph?

A question regarding using the definite article

Can a creature tell when it has been affected by a Divination wizard's Portent?

Why does Bran Stark feel that Jon Snow "needs to know" about his lineage?

Single Colour Mastermind Problem

Given what happens in Endgame, why doesn't Dormammu come back to attack the universe?

Unexpected email from Yorkshire Bank

Is it possible to measure lightning discharges as Nikola Tesla?

What does 「再々起」mean?



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;








70















(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



















  • 18





    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


















70















(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



















  • 18





    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














70












70








70


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

50428




50428







  • 18





    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













  • 18





    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








18




18





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


















128














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






  • 10





    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


















36














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









    128














    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






    • 10





      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















    128














    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






    • 10





      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













    128












    128








    128







    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

    112k16137195




    112k16137195







    • 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






    • 10





      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






    • 10





      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




    10




    10





    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













    36














    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



























      36














      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

























        36












        36








        36







        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.7k33659




        33.7k33659



























            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

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company