Why do C and C++ allow the expression (int) + 4*5?What does the unary plus operator do?Cast int to enum in C#What are the differences between a pointer variable and a reference variable in C++?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is it faster to process a sorted array than an unsorted array?Compiling an application for use in highly radioactive environments
Co-worker works way more than he should
Work requires me to come in early to start computer but wont let me clock in to get paid for it
Restricting the options of a lookup field, based on the value of another lookup field?
How do I produce this Greek letter koppa: Ϟ in pdfLaTeX?
Could moose/elk survive in the Amazon forest?
How to have a sharp product image?
Can I criticise the more senior developers around me for not writing clean code?
I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?
Would the change in enthalpy (ΔH) for the dissolution of urea in water be positive or negative?
Is it acceptable to use working hours to read general interest books?
What is the best way to deal with NPC-NPC combat?
Find a stone which is not the lightest one
Cayley's Matrix Notation
Mistake in years of experience in resume?
Philosophical question on logistic regression: why isn't the optimal threshold value trained?
Why did C use the -> operator instead of reusing the . operator?
What is purpose of DB Browser(dbbrowser.aspx) under admin tool?
SFDX - Create Objects with Custom Properties
Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?
Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?
How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?
How important is it that $TERM is correct?
Why do distances seem to matter in the Foundation world?
Where was the County of Thurn und Taxis located?
Why do C and C++ allow the expression (int) + 4*5?
What does the unary plus operator do?Cast int to enum in C#What are the differences between a pointer variable and a reference variable in C++?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is it faster to process a sorted array than an unsorted array?Compiling an application for use in highly radioactive environments
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
(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
New contributor
|
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
New contributor
16
same as(int)-4*5
– P__J__
Apr 21 at 15:29
44
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
21
This statement is equivalent to+(int)+ 4*5;
and-(int)- 4*5;
and-+-+-(int)-+-+- 4*5;
and less poetically;
– chqrlie
Apr 22 at 0:12
15
What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.
– Carsten S
Apr 22 at 6:26
5
Shouldn't C++ warn you that C-type casts are not recommended in C++?
– Mr Lister
Apr 22 at 9:38
|
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
New contributor
(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
New contributor
New contributor
edited 2 days ago
Ernest Bredar
New contributor
asked Apr 21 at 14:29
Ernest BredarErnest Bredar
48428
48428
New contributor
New contributor
16
same as(int)-4*5
– P__J__
Apr 21 at 15:29
44
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
21
This statement is equivalent to+(int)+ 4*5;
and-(int)- 4*5;
and-+-+-(int)-+-+- 4*5;
and less poetically;
– chqrlie
Apr 22 at 0:12
15
What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.
– Carsten S
Apr 22 at 6:26
5
Shouldn't C++ warn you that C-type casts are not recommended in C++?
– Mr Lister
Apr 22 at 9:38
|
show 7 more comments
16
same as(int)-4*5
– P__J__
Apr 21 at 15:29
44
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
21
This statement is equivalent to+(int)+ 4*5;
and-(int)- 4*5;
and-+-+-(int)-+-+- 4*5;
and less poetically;
– chqrlie
Apr 22 at 0:12
15
What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.
– Carsten S
Apr 22 at 6:26
5
Shouldn't C++ warn you that C-type casts are not recommended in C++?
– Mr Lister
Apr 22 at 9:38
16
16
same as
(int)-4*5
– P__J__
Apr 21 at 15:29
same as
(int)-4*5
– P__J__
Apr 21 at 15:29
44
44
There is a useful tool called
cppinsights
that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)– Nikita Kniazev
Apr 21 at 21:02
There is a useful tool called
cppinsights
that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)– Nikita Kniazev
Apr 21 at 21:02
21
21
This statement is equivalent to
+(int)+ 4*5;
and -(int)- 4*5;
and -+-+-(int)-+-+- 4*5;
and less poetically ;
– chqrlie
Apr 22 at 0:12
This statement is equivalent to
+(int)+ 4*5;
and -(int)- 4*5;
and -+-+-(int)-+-+- 4*5;
and less poetically ;
– chqrlie
Apr 22 at 0:12
15
15
What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.
– Carsten S
Apr 22 at 6:26
What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.
– Carsten S
Apr 22 at 6:26
5
5
Shouldn't C++ warn you that C-type casts are not recommended in C++?
– Mr Lister
Apr 22 at 9:38
Shouldn't C++ warn you that C-type casts are not recommended in C++?
– Mr Lister
Apr 22 at 9:38
|
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.
44
"Casting", not "typecasting". Typecasting is something that happens to actors.
– Keith Thompson
Apr 21 at 23:41
7
(+ 4)
is not make the operand+4
, it means apply the unary+
to 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
9
I see nothing wrong with using the term Type Casting. It seems I'm not the only one.
– Ben
Apr 22 at 15:28
13
@Ben Typecasting is not type casting.
– Kenneth K.
Apr 22 at 17:05
|
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
);
);
Ernest Bredar is a new contributor. Be nice, and check out our Code of Conduct.
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.
44
"Casting", not "typecasting". Typecasting is something that happens to actors.
– Keith Thompson
Apr 21 at 23:41
7
(+ 4)
is not make the operand+4
, it means apply the unary+
to 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
9
I see nothing wrong with using the term Type Casting. It seems I'm not the only one.
– Ben
Apr 22 at 15:28
13
@Ben Typecasting is not type casting.
– Kenneth K.
Apr 22 at 17:05
|
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.
44
"Casting", not "typecasting". Typecasting is something that happens to actors.
– Keith Thompson
Apr 21 at 23:41
7
(+ 4)
is not make the operand+4
, it means apply the unary+
to 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
9
I see nothing wrong with using the term Type Casting. It seems I'm not the only one.
– Ben
Apr 22 at 15:28
13
@Ben Typecasting is not type casting.
– Kenneth K.
Apr 22 at 17:05
|
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 2 days ago
answered Apr 21 at 14:31
Sourav GhoshSourav Ghosh
112k16137195
112k16137195
44
"Casting", not "typecasting". Typecasting is something that happens to actors.
– Keith Thompson
Apr 21 at 23:41
7
(+ 4)
is not make the operand+4
, it means apply the unary+
to 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
9
I see nothing wrong with using the term Type Casting. It seems I'm not the only one.
– Ben
Apr 22 at 15:28
13
@Ben Typecasting is not type casting.
– Kenneth K.
Apr 22 at 17:05
|
show 4 more comments
44
"Casting", not "typecasting". Typecasting is something that happens to actors.
– Keith Thompson
Apr 21 at 23:41
7
(+ 4)
is not make the operand+4
, it means apply the unary+
to 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
9
I see nothing wrong with using the term Type Casting. It seems I'm not the only one.
– Ben
Apr 22 at 15:28
13
@Ben Typecasting is not type casting.
– Kenneth K.
Apr 22 at 17:05
44
44
"Casting", not "typecasting". Typecasting is something that happens to actors.
– Keith Thompson
Apr 21 at 23:41
"Casting", not "typecasting". Typecasting is something that happens to actors.
– Keith Thompson
Apr 21 at 23:41
7
7
(+ 4)
is not make the operand +4
, it means apply the unary +
to operand 4
, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c
is probably false and sizeof +"a"
is probably not 2.– chqrlie
Apr 22 at 0:07
(+ 4)
is not make the operand +4
, it means apply the unary +
to operand 4
, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example char c = 0; sizeof +c == sizeof c
is probably false and sizeof +"a"
is probably not 2.– chqrlie
Apr 22 at 0:07
7
7
"both are redundant" - the whole thing is redundant, just like
42;
:-)– paxdiablo
Apr 22 at 8:17
"both are redundant" - the whole thing is redundant, just like
42;
:-)– paxdiablo
Apr 22 at 8:17
9
9
I see nothing wrong with using the term Type Casting. It seems I'm not the only one.
– Ben
Apr 22 at 15:28
I see nothing wrong with using the term Type Casting. It seems I'm not the only one.
– Ben
Apr 22 at 15:28
13
13
@Ben Typecasting is not type casting.
– Kenneth K.
Apr 22 at 17:05
@Ben Typecasting is not type casting.
– Kenneth K.
Apr 22 at 17:05
|
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.6k33659
33.6k33659
add a comment |
add a comment |
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.
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.
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
16
same as
(int)-4*5
– P__J__
Apr 21 at 15:29
44
There is a useful tool called
cppinsights
that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given)– Nikita Kniazev
Apr 21 at 21:02
21
This statement is equivalent to
+(int)+ 4*5;
and-(int)- 4*5;
and-+-+-(int)-+-+- 4*5;
and less poetically;
– chqrlie
Apr 22 at 0:12
15
What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign.
– Carsten S
Apr 22 at 6:26
5
Shouldn't C++ warn you that C-type casts are not recommended in C++?
– Mr Lister
Apr 22 at 9:38