473,387 Members | 1,362 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,387 software developers and data experts.

types, constants and casting

When assigning or testing equality between a type and a constant,
should the constant be casted to the type?

I.e.
should fork() == -1 be written as fork() == (pid_t)-1?
should uid = 45 be written as uid = (uid_t)45?

Is the casting necessary? Does it increase portability? Browsing
some projects, I noticed that -1 is casted but not other values, i.e.
chown("somefile", 45, (gid_t)-1).

In short, what is the best practice.

TIA

-Tom
Nov 13 '05 #1
5 1793
j

"Tom Carroll" <tc*************@chimesnet.com> wrote in message
news:dc**************************@posting.google.c om...
When assigning or testing equality between a type and a constant,
should the constant be casted to the type?

I.e.
should fork() == -1 be written as fork() == (pid_t)-1?
Seems superfluous. As long as fork() returns a value that is considered to
be of arithmetic type, then the cast is unnecessary.
should uid = 45 be written as uid = (uid_t)45?
As long as ``uid'' is an arithmetic type, then casting is again,
unnecessary.

Is the casting necessary? Does it increase portability?
No(unless you are forced to use a torturing device such as lint) and no.
Browsing
some projects, I noticed that -1 is casted but not other values, i.e.
chown("somefile", 45, (gid_t)-1).
As long as a prototype is available for ``chown'', then the cast is
unnecessary.

In short, what is the best practice.

To understand the constraints of the equality operators and to _never_
violate those constraints.

``Constraints
2 One of the following shall hold:
- both operands have arithmetic type;
- both operands are pointers to qualified or unqualified versions of
compatible types;
- one operand is a pointer to an object or incomplete type and the other is
a pointer to a
qualified or unqualified version of void;or
- one operand is a pointer and the other is a null pointer constant. ''
TIA

-Tom

Nov 13 '05 #2
On 1 Sep 2003 20:41:52 -0700, tc*************@chimesnet.com (Tom
Carroll) wrote in comp.lang.c:
When assigning or testing equality between a type and a constant,
should the constant be casted to the type?

I.e.
should fork() == -1 be written as fork() == (pid_t)-1?
should uid = 45 be written as uid = (uid_t)45?

Is the casting necessary? Does it increase portability? Browsing
some projects, I noticed that -1 is casted but not other values, i.e.
chown("somefile", 45, (gid_t)-1).

In short, what is the best practice.

TIA

-Tom


It does not increase portability at all, at least not for sensibly
written functions.

If there is a proper prototype in scope for the function, or at least
a declaration with the return type specified, and that function
returns an arithmetic type other than int, the compiler will
automatically convert -1 to the proper type.

I seem to recall that there are some (UNIX/POSIX/Linux) functions that
return a pointer to something if successful, and return (T *)-1,
instead of a null pointer, if they fail. I hope I am remembering
incorrectly, because this is not really portable even with a cast.

In any case, if there is no prototype in scope and the function
returns something other than int, the result is undefined behavior
regardless of whether you use the cast.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #3
Jack Klein <ja*******@spamcop.net> wrote:
On 1 Sep 2003 20:41:52 -0700, tc*************@chimesnet.com (Tom
Carroll) wrote in comp.lang.c:
When assigning or testing equality between a type and a constant,
should the constant be casted to the type?

I.e.
should fork() == -1 be written as fork() == (pid_t)-1?
should uid = 45 be written as uid = (uid_t)45?

Is the casting necessary? Does it increase portability? Browsing
some projects, I noticed that -1 is casted but not other values, i.e.
chown("somefile", 45, (gid_t)-1).

In short, what is the best practice.

TIA

-Tom


It does not increase portability at all, at least not for sensibly
written functions.

If there is a proper prototype in scope for the function, or at least
a declaration with the return type specified, and that function
returns an arithmetic type other than int, the compiler will
automatically convert -1 to the proper type.


That's not true. Consider:

#include <stdio.h>

typedef unsigned short foo_t;

foo_t do_thing()
{
return -1;
}

int main()
{
if (do_thing() == -1)
puts("y");
else
puts("n");

return 0;
}

Which on many typical implementations will require a (foo_t) cast on the
-1 for the comparison to ever succeed.

On the other hand, the cast in the function call:
chown("somefile", 45, (gid_t)-1).


is never required, so long as the function has a prototyped declaration
in scope.

- Kevin.

Nov 13 '05 #4
On Tue, 02 Sep 2003 06:19:51 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote in comp.lang.c:
Jack Klein <ja*******@spamcop.net> wrote:
On 1 Sep 2003 20:41:52 -0700, tc*************@chimesnet.com (Tom
Carroll) wrote in comp.lang.c:
When assigning or testing equality between a type and a constant,
should the constant be casted to the type?

I.e.
should fork() == -1 be written as fork() == (pid_t)-1?
should uid = 45 be written as uid = (uid_t)45?

Is the casting necessary? Does it increase portability? Browsing
some projects, I noticed that -1 is casted but not other values, i.e.
chown("somefile", 45, (gid_t)-1).

In short, what is the best practice.

TIA

-Tom


It does not increase portability at all, at least not for sensibly
written functions.

If there is a proper prototype in scope for the function, or at least
a declaration with the return type specified, and that function
returns an arithmetic type other than int, the compiler will
automatically convert -1 to the proper type.


That's not true. Consider:

#include <stdio.h>

typedef unsigned short foo_t;

foo_t do_thing()
{
return -1;
}


Sad to say, you are absolutely right and I was wrong.

I would like to be able to say nobody in their right mind would write
code like this using an unsigned type of lesser rank than unsigned
int, but I know all too well that the assertion is indefensible.

But nobody who works for me would ever write code like that a second
time, one way or the other...

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #5
In article <7a********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
On Tue, 02 Sep 2003 06:19:51 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote in comp.lang.c:
That's not true. Consider:

#include <stdio.h>

typedef unsigned short foo_t;

foo_t do_thing()
{
return -1;
}
Sad to say, you are absolutely right and I was wrong.

I would like to be able to say nobody in their right mind would write
code like this using an unsigned type of lesser rank than unsigned
int, but I know all too well that the assertion is indefensible.


The code could be

typedef <sometype> uint32;
typedef <sometype> uint16;
typedef <sometype> uint8;

typedef uint16 foo_t;
etc.

And when the code was written, uint16 was unsigned int and everything
was fine, but then someone switched to a different compiler, and now
uint16 is unsigned short...

I'd hope that a decent compiler gives a warning for the return
statement, because it definitely doesn't return what you expect (on all
five compilers that I can use easily it will return 65535). And another
warning possibly on the comparison do_thing () == -1 because that could
never be true (on the same five compilers).
But nobody who works for me would ever write code like that a second
time, one way or the other...

Nov 13 '05 #6

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

Similar topics

20
by: sandSpiderX | last post by:
Hi, I have to develop a program to compute range of all data types at run time... meaning say char c; now I have to see how many characters char can represent,, one logic i think is to...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
3
by: farseer | last post by:
if an enum requires boxing often, i'd assume constants would win on performance, is that true? Further, it appears that if you need to pass enum values to functions that accept only uint, int or...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
27
by: Matt | last post by:
Does the language have header files containing definitions of numbers such as pi and e? I believe some implementations used to have those, but I can't find them in Stroustrup. Thanks.
58
by: jacob navia | last post by:
Hi people I have been working again in my tutorial, and I have finished the "types" chapter. If you feel like "jacob bashing" this is the occasion! I am asking for criticisms, or for things I may...
3
by: www.gerardvignes.com | last post by:
I do most JavaScript programming with objects and methods. I use tiny boot and shutdown scripts as hooks for the browser load and unload events. My JavaScripts interact with scripts running on...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.