Why do C and C++ allow the expression (int) + 4*5?
.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
19
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
56229
56229
19
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
19
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
19
19
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
11
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
11
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
11
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
114k16137195
114k16137195
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
11
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
11
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
11
11
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
34.2k33660
34.2k33660
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
19
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