Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

warning on toupper(c)

14 views
Skip to first unread message

KEN BRADY

unread,
Sep 23, 1995, 3:00:00 AM9/23/95
to
Is there any way to prevent the statement below from generating a compiler warning
"integral value my be truncated during assignment". I know I can typecast the return
value to (char), but it seems kind of stupid for a function which is designed to work on
char to require this.

char c='a';
c=toupper(c)

___ Blue Wave/QWK v2.12

Martin Ambuhl

unread,
Sep 24, 1995, 3:00:00 AM9/24/95
to
ken....@swcbbs.com (KEN BRADY) in
<8B1A4F3.0063...@swcbbs.com> writes:

>Is there any way to prevent the statement below from generating a
>compiler warning "integral value my be truncated during assignment". I
>know I can typecast the return value to (char), but it seems kind of
>stupid for a function which is designed to work on char to require this.

This is wrong. The prototype is
int toupper(int c);
You have confused characters and chars. It is designed to work on ints,
not chars. So the answer is to declare c an int.

>char c='a';
>c=toupper(c)

In this call, the value of char variable c is promoted to an int which
is passed to toupper, which returns an int which must be converted, with
possible trunction, to a char.
--
* Martin Ambuhl net: mam...@ripco.com
* Chicago, IL (USA)

Lawrence Kirby

unread,
Sep 24, 1995, 3:00:00 AM9/24/95
to
In article <DFEMI...@rci.ripco.com> mam...@ripco.com "Martin Ambuhl" writes:

>ken....@swcbbs.com (KEN BRADY) in
><8B1A4F3.0063...@swcbbs.com> writes:
>
>>Is there any way to prevent the statement below from generating a
>>compiler warning "integral value my be truncated during assignment". I
>>know I can typecast the return value to (char), but it seems kind of
>>stupid for a function which is designed to work on char to require this.

You could try checking your compiler documentation to see if there is a switch
to control this error. Personally I don't consider warning about implicit
conversions from int to char to be very useful since there are too many
situations where you would legitimately do it. However a compiler is allowed
to generate any extra diagnostics it feels like so there is nothing illegal
about the warning.

>This is wrong. The prototype is
> int toupper(int c);
>You have confused characters and chars. It is designed to work on ints,
>not chars. So the answer is to declare c an int.

The reason the argument and return types are int is similar to that for
getchar() - the function is defined for values representable as unsigned
char and also EOF (which has a negative value). Any other value passed to
toupper results in undefined behaviour (which is why it is sometimes necessary
to case the argument to ctype.h funtions to unsigned char before passing it).

In this case declaring c as int is a good solution but it is not a general
solution e.g. where the character being assigned to is in an array/string.
Casting every time you make such an assignment is messy and I don't have
much sympathy for compiler diagnostics that force messy coding. Maybe
the compiler would be happier with an assignment to unsigned char but I
would prefer disabling the diagnostic in the compiler. If that is not
possible I would consider complaining to the vendor and ultimately dumping
the compiler.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------

Makarand Gadre

unread,
Sep 27, 1995, 3:00:00 AM9/27/95
to
/Is there any way to prevent the statement below from generating a
/compiler warning
/"integral value my be truncated during assignment". I know I can
/typecast the return
/value to (char), but it seems kind of stupid for a function which is
/designed to work on
/char to require this.
/
/char c='a';
/c=toupper(c)
/
/___ Blue Wave/QWK v2.12

Look up ANSI C section 7.3 on why all these ctype.h functions require
an int c as an argument.

Makarand


0 new messages