473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

int(a) vs (int)a

Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 23 '05 #1
19 1480
Gernot Frisch wrote:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?


Essentially no difference.

int( x ) - is the construction syntax.

(int)( x ) - is the (C like) type-cast syntax which should probably give
way to the "static_cast<int>( x )" syntax.

Jul 23 '05 #2
> Hmm...

sometime I see code like:
int a = int(13.5+17.2);

In this case, you take the integer part of your addition so in this case :
13,5 + 17,2 = 30,7, the result can be 31 or 30.
I use to write:
int a = (int)(13.5+17.2);

But in this case, you modify what you have in the parenthèse. When you have
text in the => ( ), and (int) in front of, it transform your text in numeric
with the ASCII table. So in my opinion there are no reasons to do
(int)(13.5+17.2) when you already have number in the => ( ).

I hope my explications are understandable. See you later ;)
is there any difference?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com

Jul 23 '05 #3
On Wed, 1 Jun 2005 08:23:31 +0200, "Robert ^^" <mo*@moi.com> wrote in
comp.lang.c++:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

In this case, you take the integer part of your addition so in this case :
13,5 + 17,2 = 30,7, the result can be 31 or 30.


Do you know anything about conversions in C++? The result cannot be
anything other than 30 when 30.7 is converted to any integer type.
I use to write:
int a = (int)(13.5+17.2);


But in this case, you modify what you have in the parenthèse. When you have
text in the => ( ), and (int) in front of, it transform your text in numeric
with the ASCII table. So in my opinion there are no reasons to do
(int)(13.5+17.2) when you already have number in the => ( ).


Do you know anything about operators and casts in C++? This
expression adds the two double values 12.5 and 17.2 to produce the
double value 30.7. The cast outside the parentheses has absolutely no
effect on the evaluation of the expression inside them. The cast
causes this double value to be converted to int, dropping the fraction
and again the result must be 30.
I hope my explications are understandable. See you later ;)
I suggest you learn a little more about C++ before answering questions
here.
is there any difference?


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #4
On Wed, 1 Jun 2005 08:05:36 +0200, "Gernot Frisch" <Me@Privacy.net>
wrote in comp.lang.c++:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);
This is C++ initializer syntax, meant to resemble the invocation of a
parameterized constructor for a user defined type. It is illegal in
C.
I use to write:
int a = (int)(13.5+17.2);
This is a plain old ordinary C cast, and it is legal in C. If
portability to C makes any difference to you.
is there any difference?


Other than the C portability for built-in types, none.

And this is identical to both of them:

int a = 13.5+17.2;

Casting the conversion of a floating point value to an integer type in
C++ (or C), is a waste of typing. It buys you exactly nothing. The
conversion is automatic without the case, and if the integral part of
the floating point value is outside the range of the integer type, the
cast does not make the behavior any less undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #5

"Robert ^^" <mo*@moi.com> schrieb im Newsbeitrag
news:d7*********@news.rd.francetelecom.fr...
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

In this case, you take the integer part of your addition so in this
case :
13,5 + 17,2 = 30,7, the result can be 31 or 30.


Mumpitz! Conversion to integer always drops the fraction part:
ASSERT(int(3.999999999) == 4);
I hope my explications are understandable. See you later ;)

Me, too.
Jul 23 '05 #6
> Casting the conversion of a floating point value to an integer type
in
C++ (or C), is a waste of typing. It buys you exactly nothing. The
conversion is automatic without the case, and if the integral part
of
the floating point value is outside the range of the integer type,
the
cast does not make the behavior any less undefined.


It drops some compiler warnings ;)
Jul 23 '05 #7
> > > Hmm...

sometime I see code like:
int a = int(13.5+17.2);


In this case, you take the integer part of your addition so in this case :
13,5 + 17,2 = 30,7, the result can be 31 or 30.


Do you know anything about conversions in C++? The result cannot be
anything other than 30 when 30.7 is converted to any integer type.
I use to write:
int a = (int)(13.5+17.2);


But in this case, you modify what you have in the parenthèse. When you have text in the => ( ), and (int) in front of, it transform your text in numeric with the ASCII table. So in my opinion there are no reasons to do
(int)(13.5+17.2) when you already have number in the => ( ).


Do you know anything about operators and casts in C++? This
expression adds the two double values 12.5 and 17.2 to produce the
double value 30.7. The cast outside the parentheses has absolutely no
effect on the evaluation of the expression inside them. The cast
causes this double value to be converted to int, dropping the fraction
and again the result must be 30.
I hope my explications are understandable. See you later ;)


I suggest you learn a little more about C++ before answering questions
here.


Sorry if i'm just a studient and if i want to help someone, you help nobody
when you say things like these ... But excuse me, i can't speak with you,
you are too stronger for me ...
Jul 23 '05 #8
Robert ^^ wrote:


Sorry if i'm just a studient and if i want to help someone, you help nobody
when you say things like these ...


The problem is, that with answers like the one you gave, you also help nobody.
In fact the opposite is true: The group needs to take actions to correct your
errors (which by the way are silly and very basic errors).

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #9
Gianni Mariani wrote:

int( x ) - is the construction syntax.

(int)( x ) - is the (C like) type-cast syntax which should probably give
way to the "static_cast<int>( x )" syntax.


They are both called "explicit conversion" in the C++ standard.
The first is "functional notation" the second is "cast notation."

In the case where there is only one arg in the functional notation
form, the language defines the two as being identical.
Jul 23 '05 #10
> The problem is, that with answers like the one you gave, you also help
nobody.
In fact the opposite is true: The group needs to take actions to correct your errors (which by the way are silly and very basic errors).


Sorry ... : S
Jul 23 '05 #11
Robert ^^ wrote:
The problem is, that with answers like the one you gave, you also help

nobody.
In fact the opposite is true: The group needs to take actions to correct

your
errors (which by the way are silly and very basic errors).


Sorry ... : S


No problem.
We all make errors from time to time (even the regulars).

Don't get us wrong. Nobody wants you to leave this group. You are very
welcome to this group.
But please read this group a while and stick to answering posts for which
you are very, very sure you know what you are talking about. E.g. I consider
myself to have a reasonable understanding of the language C++. Yet you won't
find replies with my name to the topic of 'templates'. They are one of
my weak points. I know it, so I refrain from posting.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #12
In article <3g************@individual.net>,
Gernot Frisch <Me@Privacy.net> wrote:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?


If you use the second form, you can compile your C++ with a C compiler.

This makes it Obviously Superior; just ask the people who keep trying
to tell comp.lang.c that we should be careful to write our C so that we
can compile it with a C++ compiler.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Wow, a C idea *so* stupid that it actually hasn't ever come up on
comp.lang.c in the last 8 or so years to my knowledge.
--Ben Pfaff in the scary devil monastery
Jul 23 '05 #13

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:3g************@individual.net...
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?


You've already received some excellent comments about the explicit
conversions above. What i'ld like to point out is that in C++ the latter
statement might better be stated as so:

int a = static_cast<int>(13.5 + 17.2);

which denotes a compile-time conversion (static) and removes any confusion
about the intentions of the syntax.

Other interesting cast operators you may want to investigate include the
following:
dynamic_cast, const_cast and reinterpret_cast

Jul 23 '05 #14
Peter Julian wrote:

int a = static_cast<int>(13.5 + 17.2);

which denotes a compile-time conversion (static) and removes any confusion
about the intentions of the syntax.

It's not a compile time conversion any more than the other.
static_cast<int>(13.5 + 17.2)
int(13.5+17.2)
(int)(13.5+17.2)
all have exactly the same meaning.

There's no requirement in the standard to evaluate double typed
constrant expressions at compile time.
Jul 23 '05 #15

"Ron Natalie" <ro*@spamcop.net> wrote in message
news:42***********************@news.newshosting.co m...
Peter Julian wrote:

int a = static_cast<int>(13.5 + 17.2);

which denotes a compile-time conversion (static) and removes any confusion about the intentions of the syntax.
It's not a compile time conversion any more than the other.
static_cast<int>(13.5 + 17.2)
int(13.5+17.2)
(int)(13.5+17.2)
all have exactly the same meaning.


Yes, of course. I was refering to coding style only.

There's no requirement in the standard to evaluate double typed
constrant expressions at compile time.


That i didn't know.

Jul 23 '05 #16


Gernot Frisch wrote:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?


As far as I understand, the first form, "int a = int(b)", creates a
temporary, while the second does a compile-time conversion. /david

Jul 23 '05 #17
da********@warpmail.net wrote:

Gernot Frisch wrote:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?

As far as I understand, the first form, "int a = int(b)", creates a
temporary, while the second does a compile-time conversion. /david

You don't understand. The language specifically requires both of them
to have the same behavior.

Both result in a temporary (which may be elided).
Jul 23 '05 #18
What if 'int' is a user-defined type? /david

Jul 23 '05 #19
da********@warpmail.net wrote:
What if 'int' is a user-defined type? /david

Makes no difference. The standard says they are IDENTICAL
regradless of type.
Jul 23 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
14
by: yang__lee | last post by:
Hi, You all know typedef typedef struct g { int a; int b; } google;
16
by: Julia | last post by:
Hi, there, In C programming, for pointer, I saw two programming styles: one is connecting '*' with variable, like, 'int *i'; the other is connecting '*' with data type, like, 'int* i' I...
8
by: beagle197 | last post by:
Folks, Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...} allocated using malloc/calloc, but the arguments I'm passing to qsort are producing the incorrect results (see...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.