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

Avoiding Implicit Casting

P: n/a
Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

Thanks,

Simon ;o)
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In article <bl**********@titan.btinternet.com>,
Simon <so*********@no.no> wrote:
Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?
Terminology nitpick: There's no such thing as an implicit cast; a
cast is a source-level construct (which forces a coversion) and is by
definition explicit.
The term you're looking for is "implicit conversion".
i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast


No, there's no way to avoid implicit conversions other than not writing
code that uses them in the first place.
The canonical question to ask at this point is "What are you really
trying to do?".
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
The n869 draft does not define the language, but this non-definition is more
authoritative (though less readable) than K&R's non-definition of the language.
--Richard Heathfield in comp.lang.c
Nov 13 '05 #2

P: n/a
Simon wrote:
Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast


Not really. (Why would you want to?) I can think of a couple of ways
to achieve a similar effect:

* use user-defined types (struct or union) instead of builtins.

typedef struct { int value; } Int;
typedef struct { short value; } Short;
void foo(Int);
Short b;
foo(b); /* error */

* pass pointers instead of values around.

void foo(const int *);
short b;
foo(&b); /* error */

These are likely to be fairly cumbersome to use, though.

(As a side note, I wouldn't describe C's various implicit conversions
as "casting". "Casting" always refers, I think, to conversions which
the programmer explicitly requests by using a cast operator.)

Jeremy.
Nov 13 '05 #3

P: n/a
Simon wrote:

Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

#include <stdio.h>

void foo(int a) {
printf("%d\n", a);
}

int main(void) {
short b = 42;
foo(b);
return 0;
}
Works fine. What am I missing?
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #4

P: n/a
Joe Wright wrote:
Simon wrote:

Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

#include <stdio.h>

void foo(int a) {
printf("%d\n", a);
}

int main(void) {
short b = 42;
foo(b);
return 0;
}
Works fine. What am I missing?


Only the fact that he wants your code to generate a diagnostic (or, perhaps,
a run-time error).
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #5

P: n/a
Simon wrote:
Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?
Yes. I hereby disallow it. (There is no such thing as implicit casting in C.
A cast is an explicit conversion, so "implicit cast" is an oxymoron.)
i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast


Disabling normal conversion between short int and int would be short-sighted
(but not int-sighted), since it would break a lot of existing code. *Why*
do you want to do this? Cui bono?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6

P: n/a
Richard Heathfield wrote:

Joe Wright wrote:
Simon wrote:

Hi All,

Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast

#include <stdio.h>

void foo(int a) {
printf("%d\n", a);
}

int main(void) {
short b = 42;
foo(b);
return 0;
}
Works fine. What am I missing?


Only the fact that he wants your code to generate a diagnostic (or, perhaps,
a run-time error).

Do you still have "Reading for Comprehension" available? :-)
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #7

P: n/a
In <bl**********@titan.btinternet.com> "Simon" <so*********@no.no> writes:
Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast


These implicit function argument conversions are a standard feature of the
language, no conforming compiler is allowed not to perform them.

At best, your compiler may have an option to warn when such conversions
are performed. However, compilers usually can be made to warn about
implicit conversions to narrower types, not to wider types (the latter
being always value-preserving and, therefore, not a source of problems).

You can also usually get warnings about implicit conversions to types
of a different signedness (e.g. from short int to unsinged int).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8

P: n/a
You can force these to be errors by creating your own data type (struct or
union) and "wrapping" your value in the data type. I don't know how viable
that is for you, but it's a solution. Depending on your compiler, you may
also have the option to treat warnings like errors. You may want to look
into that as well.
"Dan Pop" <Da*****@cern.ch> wrote in message
news:bl**********@sunnews.cern.ch...
In <bl**********@titan.btinternet.com> "Simon" <so*********@no.no> writes:
Is it possible to disallow implicit casting for an operand of a function
written in C?

i.e.
void foo(int a) {..}
short b;
foo(b) // error without explicit cast


These implicit function argument conversions are a standard feature of the
language, no conforming compiler is allowed not to perform them.

At best, your compiler may have an option to warn when such conversions
are performed. However, compilers usually can be made to warn about
implicit conversions to narrower types, not to wider types (the latter
being always value-preserving and, therefore, not a source of problems).

You can also usually get warnings about implicit conversions to types
of a different signedness (e.g. from short int to unsinged int).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de

Nov 13 '05 #9

P: n/a
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> writes:
You can force these to be errors by creating your own data type (struct or
union) and "wrapping" your value in the data type. I don't know how viable
that is for you, but it's a solution. Depending on your compiler, you may
also have the option to treat warnings like errors. You may want to look
into that as well.


(Please don't top-post. Luckily, I didn't need any of what Dan
said for my response to you).

In addition to being ugly, this solution also only achieves an
extra level of indirection; you're still able to do:

struct foo { short bar; } quux;

int quuux = 10;
quux.bar = quuux;

-Micah
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.