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 - Тарых жана география Навигация менюсу

            Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

            Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070