Why do C and C++ allow the expression (int) + 4? 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?
How do you write "wild blueberries flavored"?
Does the universe have a fixed centre of mass?
First paper to introduce the "principal-agent problem"
What is a more techy Technical Writer job title that isn't cutesy or confusing?
In musical terms, what properties are varied by the human voice to produce different words / syllables?
How to ask rejected full-time candidates to apply to teach individual courses?
New Order #6: Easter Egg
2018 MacBook Pro won't let me install macOS High Sierra 10.13 from USB installer
Nose gear failure in single prop aircraft: belly landing or nose landing?
Is the time—manner—place ordering of adverbials an oversimplification?
Is this Kuo-toa homebrew race balanced?
Centre cell vertically in tabularx
How does TikZ render an arc?
By what mechanism was the 2017 General Election called?
Is it OK to use the testing sample to compare algorithms?
What does 丫 mean? 丫是什么意思?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
Russian equivalents of おしゃれは足元から (Every good outfit starts with the shoes)
Updates not showing on Software Updater?
Flight departed from the gate 5 min before scheduled departure time. Refund options
Does the Rock Gnome trait Artificer's Lore apply when you aren't proficient in History?
Keyboard layout stuck into CZ_german no english layout after update, restore into original EN_us and EL_Gr ones
What did Turing mean when saying that "machines cannot give rise to surprises" is due to a fallacy?
Vertical ranges of Column Plots in 12
Why do C and C++ allow the expression (int) + 4?
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;
(int) + 4*5;
Why is this possible? (tried with g++ and gcc.)
c++ c casting language-lawyer
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.
add a comment |
(int) + 4*5;
Why is this possible? (tried with g++ and gcc.)
c++ c casting language-lawyer
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
6 hours ago
1
same as(int)-4*5
– P__J__
5 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
4 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
3 hours ago
add a comment |
(int) + 4*5;
Why is this possible? (tried with g++ and gcc.)
c++ c casting language-lawyer
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
c++ c casting language-lawyer
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.
edited 9 mins ago
Davislor
9,10511227
9,10511227
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 6 hours ago
Ernest BredarErnest Bredar
1005
1005
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
6 hours ago
1
same as(int)-4*5
– P__J__
5 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
4 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
3 hours ago
add a comment |
Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^
– bruno
6 hours ago
1
same as(int)-4*5
– P__J__
5 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
4 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
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
6 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
6 hours ago
1
1
same as
(int)-4*5– P__J__
5 hours ago
same as
(int)-4*5– P__J__
5 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
4 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
4 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
3 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
3 hours ago
add a comment |
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,
- 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.
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 |
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-4%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,
- 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.
add a comment |
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.
add a comment |
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.
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.
edited 3 hours ago
Dancrumb
18.2k448103
18.2k448103
answered 6 hours ago
Sourav GhoshSourav Ghosh
112k15136194
112k15136194
add a comment |
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.
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 6 hours ago
Igor TandetnikIgor Tandetnik
33.4k33559
33.4k33559
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-4%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
Even this is valid it is not a good idea to do that in a program because yes this is quite disturbing ^^
– bruno
6 hours ago
1
same as
(int)-4*5– P__J__
5 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
4 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
3 hours ago