Why do C and C++ allow the expression (int) + 4*5; Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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 ListDo I cast the result of malloc?Why is “using namespace std” considered bad practice?What is the “-->” operator in C++?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?

Why are current probes so expensive?

Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?

Vertical ranges of Column Plots in 12

Does a random sequence of vectors span a Hilbert space?

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

3D Masyu - A Die

systemd and copy (/bin/cp): no such file or directory

How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?

How many time has Arya actually used Needle?

How does TikZ render an arc?

How to resize main filesystem

How do I say "this must not happen"?

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

Problem with display of presentation

How to achieve cat-like agility?

Is the time—manner—place ordering of adverbials an oversimplification?

What does 丫 mean? 丫是什么意思?

Why not use the yoke to control yaw, as well as pitch and roll?

Can two people see the same photon?

Why is there so little support for joining EFTA in the British parliament?

What was the last profitable war?

One-one communication

A question about the degree of an extension field

How do you write "wild blueberries flavored"?



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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 ListDo I cast the result of malloc?Why is “using namespace std” considered bad practice?What is the “-->” operator in C++?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?



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








19















(int) + 4*5;


Why is this possible? (tried with g++ and gcc.)










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.




















  • Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^

    – bruno
    9 hours ago







  • 3





    same as (int)-4*5

    – P__J__
    8 hours ago











  • Though "(int) (+4.0 * 5.0)" might make more sense, or "(double) (+4 * 5)". But this has me totally puzzled. Why would you write a line of code like this, without assigning the result, and why the heck does the compiler not at least issue a warning?/

    – jamesqf
    7 hours ago






  • 1





    @jamesqf I know that it dosn't make sense, but I want to know why this is possible and for this is sense not needed.

    – Ernest Bredar
    7 hours ago






  • 4





    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
    3 hours ago

















19















(int) + 4*5;


Why is this possible? (tried with g++ and gcc.)










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.




















  • Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^

    – bruno
    9 hours ago







  • 3





    same as (int)-4*5

    – P__J__
    8 hours ago











  • Though "(int) (+4.0 * 5.0)" might make more sense, or "(double) (+4 * 5)". But this has me totally puzzled. Why would you write a line of code like this, without assigning the result, and why the heck does the compiler not at least issue a warning?/

    – jamesqf
    7 hours ago






  • 1





    @jamesqf I know that it dosn't make sense, but I want to know why this is possible and for this is sense not needed.

    – Ernest Bredar
    7 hours ago






  • 4





    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
    3 hours ago













19












19








19








(int) + 4*5;


Why is this possible? (tried with g++ and gcc.)










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 possible? (tried with g++ and gcc.)







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 35 mins ago









Dai

74.6k15120209




74.6k15120209






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 9 hours ago









Ernest BredarErnest Bredar

1135




1135




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.












  • Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^

    – bruno
    9 hours ago







  • 3





    same as (int)-4*5

    – P__J__
    8 hours ago











  • Though "(int) (+4.0 * 5.0)" might make more sense, or "(double) (+4 * 5)". But this has me totally puzzled. Why would you write a line of code like this, without assigning the result, and why the heck does the compiler not at least issue a warning?/

    – jamesqf
    7 hours ago






  • 1





    @jamesqf I know that it dosn't make sense, but I want to know why this is possible and for this is sense not needed.

    – Ernest Bredar
    7 hours ago






  • 4





    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
    3 hours ago

















  • Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^

    – bruno
    9 hours ago







  • 3





    same as (int)-4*5

    – P__J__
    8 hours ago











  • Though "(int) (+4.0 * 5.0)" might make more sense, or "(double) (+4 * 5)". But this has me totally puzzled. Why would you write a line of code like this, without assigning the result, and why the heck does the compiler not at least issue a warning?/

    – jamesqf
    7 hours ago






  • 1





    @jamesqf I know that it dosn't make sense, but I want to know why this is possible and for this is sense not needed.

    – Ernest Bredar
    7 hours ago






  • 4





    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
    3 hours ago
















Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^

– bruno
9 hours ago






Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^

– bruno
9 hours ago





3




3





same as (int)-4*5

– P__J__
8 hours ago





same as (int)-4*5

– P__J__
8 hours ago













Though "(int) (+4.0 * 5.0)" might make more sense, or "(double) (+4 * 5)". But this has me totally puzzled. Why would you write a line of code like this, without assigning the result, and why the heck does the compiler not at least issue a warning?/

– jamesqf
7 hours ago





Though "(int) (+4.0 * 5.0)" might make more sense, or "(double) (+4 * 5)". But this has me totally puzzled. Why would you write a line of code like this, without assigning the result, and why the heck does the compiler not at least issue a warning?/

– jamesqf
7 hours ago




1




1





@jamesqf I know that it dosn't make sense, but I want to know why this is possible and for this is sense not needed.

– Ernest Bredar
7 hours ago





@jamesqf I know that it dosn't make sense, but I want to know why this is possible and for this is sense not needed.

– Ernest Bredar
7 hours ago




4




4





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
3 hours ago





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
3 hours ago












2 Answers
2






active

oldest

votes


















34














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,



  • Make the operand +4

  • typecasted to an int

  • 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

























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

    – Keith Thompson
    27 mins ago











  • (+ 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
    1 min ago


















23














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









    34














    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,



    • Make the operand +4

    • typecasted to an int

    • 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

























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

      – Keith Thompson
      27 mins ago











    • (+ 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
      1 min ago















    34














    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,



    • Make the operand +4

    • typecasted to an int

    • 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

























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

      – Keith Thompson
      27 mins ago











    • (+ 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
      1 min ago













    34












    34








    34







    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,



    • Make the operand +4

    • typecasted to an int

    • 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,



    • Make the operand +4

    • typecasted to an int

    • 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 6 hours ago









    Dancrumb

    18.2k448103




    18.2k448103










    answered 9 hours ago









    Sourav GhoshSourav Ghosh

    112k15136194




    112k15136194












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

      – Keith Thompson
      27 mins ago











    • (+ 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
      1 min ago

















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

      – Keith Thompson
      27 mins ago











    • (+ 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
      1 min ago
















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

    – Keith Thompson
    27 mins ago





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

    – Keith Thompson
    27 mins ago













    (+ 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
    1 min ago





    (+ 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
    1 min ago













    23














    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



























      23














      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

























        23












        23








        23







        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 9 hours ago









        Igor TandetnikIgor Tandetnik

        33.4k33559




        33.4k33559




















            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

            Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

            2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

            Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?