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;
(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
|
show 7 more comments
(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
18
same as(int)-4*5
– P__J__
Apr 21 at 15:29
45
There is a useful tool calledcppinsights
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
|
show 7 more comments
(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
(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
c++ c casting language-lawyer
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 calledcppinsights
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
|
show 7 more comments
18
same as(int)-4*5
– P__J__
Apr 21 at 15:29
45
There is a useful tool calledcppinsights
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
|
show 7 more comments
2 Answers
2
active
oldest
votes
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,
- Apply the unary
+
operator on the integer constant value4
. - typecast 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.
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 operand4
, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For examplechar c = 0; sizeof +c == sizeof c
is probably false andsizeof +"a"
is probably not 2.
– chqrlie
Apr 22 at 0:07
7
"both are redundant" - the whole thing is redundant, just like42;
:-)
– 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
|
show 4 more comments
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
.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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,
- Apply the unary
+
operator on the integer constant value4
. - typecast 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.
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 operand4
, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For examplechar c = 0; sizeof +c == sizeof c
is probably false andsizeof +"a"
is probably not 2.
– chqrlie
Apr 22 at 0:07
7
"both are redundant" - the whole thing is redundant, just like42;
:-)
– 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
|
show 4 more comments
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,
- Apply the unary
+
operator on the integer constant value4
. - typecast 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.
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 operand4
, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For examplechar c = 0; sizeof +c == sizeof c
is probably false andsizeof +"a"
is probably not 2.
– chqrlie
Apr 22 at 0:07
7
"both are redundant" - the whole thing is redundant, just like42;
:-)
– 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
|
show 4 more comments
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,
- Apply the unary
+
operator on the integer constant value4
. - typecast 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.
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,
- Apply the unary
+
operator on the integer constant value4
. - typecast 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.
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 operand4
, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For examplechar c = 0; sizeof +c == sizeof c
is probably false andsizeof +"a"
is probably not 2.
– chqrlie
Apr 22 at 0:07
7
"both are redundant" - the whole thing is redundant, just like42;
:-)
– 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
|
show 4 more comments
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 operand4
, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For examplechar c = 0; sizeof +c == sizeof c
is probably false andsizeof +"a"
is probably not 2.
– chqrlie
Apr 22 at 0:07
7
"both are redundant" - the whole thing is redundant, just like42;
:-)
– 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
|
show 4 more comments
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Apr 21 at 14:32
Igor TandetnikIgor Tandetnik
33.7k33659
33.7k33659
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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