Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Initializing sum to zero wouldn't compile |
| Date | 2015-08-26 11:38 -0700 |
| Organization | None to speak of |
| Message-ID | <lnmvxdx5d6.fsf@kst-u.example.com> (permalink) |
| References | (4 earlier) <4m3itahdhql900hjkegsqtflvsajjsd0hp@4ax.com> <b5f85a9f-fbbb-4a6e-9df8-7048a301428e@googlegroups.com> <r21rtat1fbpjsu4l4eru2ae1l0j5336eiu@4ax.com> <863bc31e-b8f9-4f8d-8d43-a99cf48cf849@googlegroups.com> <oZhDx.314562$fK1.181897@fx36.am4> |
Richard Heathfield <rjh@cpax.org.uk> writes:
> On 26/08/2015 12:21, Alla _ wrote:
>>
>>> > for ( i = 0; i< 3; i++)
>>> > {
>>> > scanf ("%f",&element);
>>> >
>>> > is_float = 1;
>>> > n = (int) element; /*casting allows to check if
>>> > the value entered by the user is a float or
>>> > an integer; when I turn element into integer,
>>> > then (int) element - element tests the entered value*/
>>>
>>> The cast does no such thing. What happens differently if you delete
>>> the cast?
>> Well, as I initialized element as a float, I assumed
>> that even if the user enters an integer, the compiler
>> will take it as a float X.XX..
>>
>> Therefore, when I cast int to the entered variable element
>
> Absolutely the biggest favour you can do yourself when learning C is to
> STOP CASTING. Casting is ALMOST ALWAYS WRONG, and the very few times
> when it's right are not the times you'd expect.
>
> Whenever you are tempted to cast, resist the temptation. Find a way to
> do what you're doing that doesn't require a cast.
>
> Yes, there are some circumstances where a cast is appropriate, but they
> are few and far between.
It's also important to understand what a "cast" is, and the
difference between a cast and a conversion. (I don't think Alla
has this problem, but plenty of other people do.)
A *cast* is an operator, consising of a type name in parentheses,
that performs an explicit type conversion. You'll sometimes
see the phrase "implicit cast", but there is no such thing in C.
There are implicit conversions, which is what someone using the
phrase "implicit cast" is usually trying to say.
A *conversion* takes a value of one type and yields a value of a
different type, usually with the same or a similar logical value.
For example converting the double value 2.0 to int yields the int
value 2.
Conversions may be either implicit or explicit. Explicit conversions
are performed using a cast. In well written code, most conversions
are likely to be implicit.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 04:39 -0700
Re: Initializing sum to zero wouldn't compile James Kuyper <jameskuyper@verizon.net> - 2015-08-21 07:54 -0400
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 05:27 -0700
Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-21 10:28 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:15 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 05:30 -0700
Re: Initializing sum to zero wouldn't compile Ike Naar <ike@iceland.freeshell.org> - 2015-08-21 12:40 +0000
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:16 -0700
Re: Initializing sum to zero wouldn't compile Siri Cruz <chine.bleu@yahoo.com> - 2015-08-21 07:41 -0700
Re: Initializing sum to zero wouldn't compile Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-08-21 10:56 -0400
Re: Initializing sum to zero wouldn't compile Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-21 09:28 -0600
Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-21 10:02 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:21 -0700
Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-21 22:27 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 02:25 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 03:50 -0700
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-22 13:18 +0100
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 11:21 -0700
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-22 21:55 +0100
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 01:36 -0700
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 11:24 +0100
Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-22 17:17 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 01:26 -0700
Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-26 02:27 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 04:21 -0700
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 12:37 +0100
Re: Initializing sum to zero wouldn't compile Richard Heathfield <rjh@cpax.org.uk> - 2015-08-26 13:06 +0100
Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-26 13:43 +0100
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 14:03 +0100
Re: Initializing sum to zero wouldn't compile Richard Heathfield <rjh@cpax.org.uk> - 2015-08-26 14:06 +0100
Re: Initializing sum to zero wouldn't compile Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 15:14 -0700
Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-26 11:38 -0700
Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-26 11:44 -0700
Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-22 10:49 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 11:16 -0700
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-29 07:19 -0700
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-29 15:35 +0100
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-30 02:11 -0700
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 12:28 +0100
Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 15:02 -0700
Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 22:36 +0100
Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 16:10 -0700
Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-31 00:14 +0100
Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 19:56 -0700
Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-29 16:57 +0100
Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-29 17:37 +0100
Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-30 02:14 -0700
csiph-web