By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

explicit casts

P: n/a
rCs

How sound is the following advice for a C language coding standard?

"Explicitly cast or convert variables. Do not rely on the implicit
conversions."

I have my opinions, but I would like to hear yours.

Thanks,
rCs

Mar 17 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"rCs" <rc*@cert.orgwrites:
How sound is the following advice for a C language coding standard?

"Explicitly cast or convert variables. Do not rely on the implicit
conversions."
No. Bad idea. Casts are to be avoided when possible.

By the way, casts are always explicit; "explicitly cast" is
redundant.
--
"I'm not here to convince idiots not to be stupid.
They won't listen anyway."
--Dann Corbit
Mar 17 '07 #2

P: n/a
On Mar 16, 5:25 pm, "rCs" <r...@cert.orgwrote:
How sound is the following advice for a C language coding standard?

"Explicitly cast or convert variables. Do not rely on the implicit
conversions."

I have my opinions, but I would like to hear yours.
Very unsound. It would clutter up the code making it much harder to
read, and is a recipe for hiding bugs from the compiler.

Which of these assignment makes for better code?

char c;

c = 'a';
c = (char)'a';
Mar 17 '07 #3

P: n/a
rCs wrote:
How sound is the following advice for a C language coding standard?

"Explicitly cast or convert variables. Do not rely on the implicit
conversions."
There's clearly absolutely nothing wrong with

char c = 'c';
bool b = false;

and both rely on implicit conversions.

Mar 17 '07 #4

P: n/a
In article <87************@blp.benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.eduwrote:
>By the way, casts are always explicit; "explicitly cast" is
redundant.
Logically, but not necessarily didactically.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 17 '07 #5

P: n/a
rCs wrote:
How sound is the following advice for a C language coding standard?

"Explicitly cast or convert variables. Do not rely on the implicit
conversions."
I agree with the other posters opposing that as a general rule. Using
casts for assignment to objects with types smaller than the maximum
possible, based on the type of operands and operations within a
statement makes more sense:

char c1, c2;
int i;
long la, lb;

/* assign initial values somehow */

c1 = 'a'; /* can't truncate, no cast */
c2 = c1; /* can't truncate */
c2 = c1 & 0x7f; /* can't truncate */
c2 = (char)(c1 + 5); /* could truncate* */
c2 = (char)i; /* could truncate */
i = (int)(la - lb); /* could truncate */
i = i * 100; /* might overflow, but no cast */

Personally, I probably wouldn't use a cast of c2 = c2 + 5, relying
instead on knowing the range of values to be handled.

--
Thad
Mar 17 '07 #6

P: n/a
rCs wrote:
How sound is the following advice for a C language coding standard?

"Explicitly cast or convert variables. Do not rely on the implicit
conversions."
It's silly advice. Recommending superfluous casts is a dumb idea;
following that recommendation is bad programming.
Mar 17 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.