Why would `(int)` precede an assignment in C++?
Here is a MWE of something I came across in some C++ code.
int a = (int)(b/c);
Why is (int)
after the assignment operator?
Is it not recommended to use it like this?
c++ syntax variable-assignment
add a comment |
Here is a MWE of something I came across in some C++ code.
int a = (int)(b/c);
Why is (int)
after the assignment operator?
Is it not recommended to use it like this?
c++ syntax variable-assignment
1
Depends on specific language (so, update the question with appropriate/relevant tags): presumably the result ofb/c
is a "float" and needs an explicit cast to an "int" before assignment. Not quite sure why "int" is considered a class either..
– user2864740
Nov 23 at 0:35
There's no assignment operator in this code. In a definition the=
symbol is not an operator; it's syntax which indicates that an initializer for the variable being defined follows.
– M.M
Nov 23 at 4:05
add a comment |
Here is a MWE of something I came across in some C++ code.
int a = (int)(b/c);
Why is (int)
after the assignment operator?
Is it not recommended to use it like this?
c++ syntax variable-assignment
Here is a MWE of something I came across in some C++ code.
int a = (int)(b/c);
Why is (int)
after the assignment operator?
Is it not recommended to use it like this?
c++ syntax variable-assignment
c++ syntax variable-assignment
edited Nov 23 at 2:54
John3136
23.8k33259
23.8k33259
asked Nov 23 at 0:34
kapple
12
12
1
Depends on specific language (so, update the question with appropriate/relevant tags): presumably the result ofb/c
is a "float" and needs an explicit cast to an "int" before assignment. Not quite sure why "int" is considered a class either..
– user2864740
Nov 23 at 0:35
There's no assignment operator in this code. In a definition the=
symbol is not an operator; it's syntax which indicates that an initializer for the variable being defined follows.
– M.M
Nov 23 at 4:05
add a comment |
1
Depends on specific language (so, update the question with appropriate/relevant tags): presumably the result ofb/c
is a "float" and needs an explicit cast to an "int" before assignment. Not quite sure why "int" is considered a class either..
– user2864740
Nov 23 at 0:35
There's no assignment operator in this code. In a definition the=
symbol is not an operator; it's syntax which indicates that an initializer for the variable being defined follows.
– M.M
Nov 23 at 4:05
1
1
Depends on specific language (so, update the question with appropriate/relevant tags): presumably the result of
b/c
is a "float" and needs an explicit cast to an "int" before assignment. Not quite sure why "int" is considered a class either..– user2864740
Nov 23 at 0:35
Depends on specific language (so, update the question with appropriate/relevant tags): presumably the result of
b/c
is a "float" and needs an explicit cast to an "int" before assignment. Not quite sure why "int" is considered a class either..– user2864740
Nov 23 at 0:35
There's no assignment operator in this code. In a definition the
=
symbol is not an operator; it's syntax which indicates that an initializer for the variable being defined follows.– M.M
Nov 23 at 4:05
There's no assignment operator in this code. In a definition the
=
symbol is not an operator; it's syntax which indicates that an initializer for the variable being defined follows.– M.M
Nov 23 at 4:05
add a comment |
4 Answers
4
active
oldest
votes
This is simply a C-style typecast. It is used to make the author's intentions explicit, especially when the result of b/c
is of another type (such as unsigned
or float
).
Without the cast, you will often get a compiler warning about an implicit conversion which can sometimes have consequences. By using the explicit cast, you are stating that you accept this conversion is fine within whatever other limits your program enforces, and the compiler will perform the conversion without emitting a warning.
In C++, we use static_cast<int>(b/c)
to make the cast even more explicit and intentional.
add a comment |
It is not "(int) after the assignment operator".
It is "(int) before a float - the result of b/c".
It casts the float to an int.
add a comment |
This is a cast used to convert a variable or expression to a given type. In this case if b
and c
were floating point numbers, adding the cast (int)
forces the result to an integer.
Specifically this is a "C style cast", modern C++ has some additional casts to given even more control (static_cast, dynamic_cast, const_cast etc)
Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast.
– kapple
Nov 27 at 0:21
Search is your friend: "does C cast round" will lead you here: stackoverflow.com/a/11128755/857132
– John3136
Nov 27 at 0:25
add a comment |
This is a mistake. In the code:
int a = b/c;
then it may cause undefined behaviour if the result of the division is a floating point value that is out of range of int
(e.g. it exceeds INT_MAX
after truncation). Compilers may warn about this if you use warning flags.
Changing the code to int a = (int)(b/c);
has no effect on the behaviour of the code, but it may cause the compiler to suppress the warning (compilers sometimes treat a cast as the programmer expressing the intent that they do not want to see the warning).
So now you just have silent undefined behaviour, unless the previous code is designed in such a way that the division result can never be out of range.
A better solution to the problem would be:
long a = std::lrint(b/c);
If the quotient is out of range then this will store an unspecified value in a
and you can detect the error using floating point error handling. Reference for std::lrint
1
I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting).
– Ray
Nov 23 at 4:25
1
Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too!
– Ray
Nov 23 at 4:27
@Ray the Q/A on this site are for anyone to read, not just OP
– M.M
Nov 23 at 6:14
1
I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question.
– Ray
Nov 23 at 15:14
@Ray that's why multiple answers are allowed ...
– M.M
Nov 24 at 1:19
|
show 2 more comments
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%2f53439424%2fwhy-would-int-precede-an-assignment-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is simply a C-style typecast. It is used to make the author's intentions explicit, especially when the result of b/c
is of another type (such as unsigned
or float
).
Without the cast, you will often get a compiler warning about an implicit conversion which can sometimes have consequences. By using the explicit cast, you are stating that you accept this conversion is fine within whatever other limits your program enforces, and the compiler will perform the conversion without emitting a warning.
In C++, we use static_cast<int>(b/c)
to make the cast even more explicit and intentional.
add a comment |
This is simply a C-style typecast. It is used to make the author's intentions explicit, especially when the result of b/c
is of another type (such as unsigned
or float
).
Without the cast, you will often get a compiler warning about an implicit conversion which can sometimes have consequences. By using the explicit cast, you are stating that you accept this conversion is fine within whatever other limits your program enforces, and the compiler will perform the conversion without emitting a warning.
In C++, we use static_cast<int>(b/c)
to make the cast even more explicit and intentional.
add a comment |
This is simply a C-style typecast. It is used to make the author's intentions explicit, especially when the result of b/c
is of another type (such as unsigned
or float
).
Without the cast, you will often get a compiler warning about an implicit conversion which can sometimes have consequences. By using the explicit cast, you are stating that you accept this conversion is fine within whatever other limits your program enforces, and the compiler will perform the conversion without emitting a warning.
In C++, we use static_cast<int>(b/c)
to make the cast even more explicit and intentional.
This is simply a C-style typecast. It is used to make the author's intentions explicit, especially when the result of b/c
is of another type (such as unsigned
or float
).
Without the cast, you will often get a compiler warning about an implicit conversion which can sometimes have consequences. By using the explicit cast, you are stating that you accept this conversion is fine within whatever other limits your program enforces, and the compiler will perform the conversion without emitting a warning.
In C++, we use static_cast<int>(b/c)
to make the cast even more explicit and intentional.
answered Nov 23 at 2:58
paddy
42.5k53076
42.5k53076
add a comment |
add a comment |
It is not "(int) after the assignment operator".
It is "(int) before a float - the result of b/c".
It casts the float to an int.
add a comment |
It is not "(int) after the assignment operator".
It is "(int) before a float - the result of b/c".
It casts the float to an int.
add a comment |
It is not "(int) after the assignment operator".
It is "(int) before a float - the result of b/c".
It casts the float to an int.
It is not "(int) after the assignment operator".
It is "(int) before a float - the result of b/c".
It casts the float to an int.
answered Nov 23 at 3:05
Zhang
8621213
8621213
add a comment |
add a comment |
This is a cast used to convert a variable or expression to a given type. In this case if b
and c
were floating point numbers, adding the cast (int)
forces the result to an integer.
Specifically this is a "C style cast", modern C++ has some additional casts to given even more control (static_cast, dynamic_cast, const_cast etc)
Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast.
– kapple
Nov 27 at 0:21
Search is your friend: "does C cast round" will lead you here: stackoverflow.com/a/11128755/857132
– John3136
Nov 27 at 0:25
add a comment |
This is a cast used to convert a variable or expression to a given type. In this case if b
and c
were floating point numbers, adding the cast (int)
forces the result to an integer.
Specifically this is a "C style cast", modern C++ has some additional casts to given even more control (static_cast, dynamic_cast, const_cast etc)
Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast.
– kapple
Nov 27 at 0:21
Search is your friend: "does C cast round" will lead you here: stackoverflow.com/a/11128755/857132
– John3136
Nov 27 at 0:25
add a comment |
This is a cast used to convert a variable or expression to a given type. In this case if b
and c
were floating point numbers, adding the cast (int)
forces the result to an integer.
Specifically this is a "C style cast", modern C++ has some additional casts to given even more control (static_cast, dynamic_cast, const_cast etc)
This is a cast used to convert a variable or expression to a given type. In this case if b
and c
were floating point numbers, adding the cast (int)
forces the result to an integer.
Specifically this is a "C style cast", modern C++ has some additional casts to given even more control (static_cast, dynamic_cast, const_cast etc)
answered Nov 23 at 2:56
John3136
23.8k33259
23.8k33259
Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast.
– kapple
Nov 27 at 0:21
Search is your friend: "does C cast round" will lead you here: stackoverflow.com/a/11128755/857132
– John3136
Nov 27 at 0:25
add a comment |
Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast.
– kapple
Nov 27 at 0:21
Search is your friend: "does C cast round" will lead you here: stackoverflow.com/a/11128755/857132
– John3136
Nov 27 at 0:25
Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast.
– kapple
Nov 27 at 0:21
Does it return the integer part, round down, or round up? Also, thanks for responding, to all that identified this as a style cast.
– kapple
Nov 27 at 0:21
Search is your friend: "does C cast round" will lead you here: stackoverflow.com/a/11128755/857132
– John3136
Nov 27 at 0:25
Search is your friend: "does C cast round" will lead you here: stackoverflow.com/a/11128755/857132
– John3136
Nov 27 at 0:25
add a comment |
This is a mistake. In the code:
int a = b/c;
then it may cause undefined behaviour if the result of the division is a floating point value that is out of range of int
(e.g. it exceeds INT_MAX
after truncation). Compilers may warn about this if you use warning flags.
Changing the code to int a = (int)(b/c);
has no effect on the behaviour of the code, but it may cause the compiler to suppress the warning (compilers sometimes treat a cast as the programmer expressing the intent that they do not want to see the warning).
So now you just have silent undefined behaviour, unless the previous code is designed in such a way that the division result can never be out of range.
A better solution to the problem would be:
long a = std::lrint(b/c);
If the quotient is out of range then this will store an unspecified value in a
and you can detect the error using floating point error handling. Reference for std::lrint
1
I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting).
– Ray
Nov 23 at 4:25
1
Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too!
– Ray
Nov 23 at 4:27
@Ray the Q/A on this site are for anyone to read, not just OP
– M.M
Nov 23 at 6:14
1
I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question.
– Ray
Nov 23 at 15:14
@Ray that's why multiple answers are allowed ...
– M.M
Nov 24 at 1:19
|
show 2 more comments
This is a mistake. In the code:
int a = b/c;
then it may cause undefined behaviour if the result of the division is a floating point value that is out of range of int
(e.g. it exceeds INT_MAX
after truncation). Compilers may warn about this if you use warning flags.
Changing the code to int a = (int)(b/c);
has no effect on the behaviour of the code, but it may cause the compiler to suppress the warning (compilers sometimes treat a cast as the programmer expressing the intent that they do not want to see the warning).
So now you just have silent undefined behaviour, unless the previous code is designed in such a way that the division result can never be out of range.
A better solution to the problem would be:
long a = std::lrint(b/c);
If the quotient is out of range then this will store an unspecified value in a
and you can detect the error using floating point error handling. Reference for std::lrint
1
I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting).
– Ray
Nov 23 at 4:25
1
Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too!
– Ray
Nov 23 at 4:27
@Ray the Q/A on this site are for anyone to read, not just OP
– M.M
Nov 23 at 6:14
1
I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question.
– Ray
Nov 23 at 15:14
@Ray that's why multiple answers are allowed ...
– M.M
Nov 24 at 1:19
|
show 2 more comments
This is a mistake. In the code:
int a = b/c;
then it may cause undefined behaviour if the result of the division is a floating point value that is out of range of int
(e.g. it exceeds INT_MAX
after truncation). Compilers may warn about this if you use warning flags.
Changing the code to int a = (int)(b/c);
has no effect on the behaviour of the code, but it may cause the compiler to suppress the warning (compilers sometimes treat a cast as the programmer expressing the intent that they do not want to see the warning).
So now you just have silent undefined behaviour, unless the previous code is designed in such a way that the division result can never be out of range.
A better solution to the problem would be:
long a = std::lrint(b/c);
If the quotient is out of range then this will store an unspecified value in a
and you can detect the error using floating point error handling. Reference for std::lrint
This is a mistake. In the code:
int a = b/c;
then it may cause undefined behaviour if the result of the division is a floating point value that is out of range of int
(e.g. it exceeds INT_MAX
after truncation). Compilers may warn about this if you use warning flags.
Changing the code to int a = (int)(b/c);
has no effect on the behaviour of the code, but it may cause the compiler to suppress the warning (compilers sometimes treat a cast as the programmer expressing the intent that they do not want to see the warning).
So now you just have silent undefined behaviour, unless the previous code is designed in such a way that the division result can never be out of range.
A better solution to the problem would be:
long a = std::lrint(b/c);
If the quotient is out of range then this will store an unspecified value in a
and you can detect the error using floating point error handling. Reference for std::lrint
answered Nov 23 at 4:17
M.M
104k11113234
104k11113234
1
I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting).
– Ray
Nov 23 at 4:25
1
Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too!
– Ray
Nov 23 at 4:27
@Ray the Q/A on this site are for anyone to read, not just OP
– M.M
Nov 23 at 6:14
1
I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question.
– Ray
Nov 23 at 15:14
@Ray that's why multiple answers are allowed ...
– M.M
Nov 24 at 1:19
|
show 2 more comments
1
I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting).
– Ray
Nov 23 at 4:25
1
Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too!
– Ray
Nov 23 at 4:27
@Ray the Q/A on this site are for anyone to read, not just OP
– M.M
Nov 23 at 6:14
1
I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question.
– Ray
Nov 23 at 15:14
@Ray that's why multiple answers are allowed ...
– M.M
Nov 24 at 1:19
1
1
I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting).
– Ray
Nov 23 at 4:25
I understand what you're getting at, but I don't think starting your message with a "This is a mistake" is a good idea. It gets people's attention but both the OP's line and the line that you gave does compile. It also might be fine for someone in their first hour or two of C (and then the lecturer/teacher might show why it's wrong in the third hour and use it to explain casting).
– Ray
Nov 23 at 4:25
1
1
Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too!
– Ray
Nov 23 at 4:27
Anyway, to the OP, I think you should take such advice carefully. If you are taking a class and start using std::lrint, your teacher might wonder who else are you asking for help. It's ok to be a "little" wrong when you're starting out; learning is a step-by-step process. But yes, if you want to hear the ending to the story, that's ok too!
– Ray
Nov 23 at 4:27
@Ray the Q/A on this site are for anyone to read, not just OP
– M.M
Nov 23 at 6:14
@Ray the Q/A on this site are for anyone to read, not just OP
– M.M
Nov 23 at 6:14
1
1
I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question.
– Ray
Nov 23 at 15:14
I know. But your answer is given within the context of the OP's original question. Surely there's some compromise between providing information for anyone to read and sticking to the original OP's question.
– Ray
Nov 23 at 15:14
@Ray that's why multiple answers are allowed ...
– M.M
Nov 24 at 1:19
@Ray that's why multiple answers are allowed ...
– M.M
Nov 24 at 1:19
|
show 2 more comments
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53439424%2fwhy-would-int-precede-an-assignment-in-c%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
1
Depends on specific language (so, update the question with appropriate/relevant tags): presumably the result of
b/c
is a "float" and needs an explicit cast to an "int" before assignment. Not quite sure why "int" is considered a class either..– user2864740
Nov 23 at 0:35
There's no assignment operator in this code. In a definition the
=
symbol is not an operator; it's syntax which indicates that an initializer for the variable being defined follows.– M.M
Nov 23 at 4:05