Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Portable int?

Reply
Thread Tools

Portable int?

 
 
Phui Hock
Guest
Posts: n/a
 
      11-11-2003
I'm just curious, why don't we just use uint8_t or uint16_t for example
to declare an integer type storage when we know int is always
platform-dependent? I do not have programming experience on other
platform except M$ Windows and Linux.

Thank you.
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      11-11-2003

"Phui Hock" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I'm just curious, why don't we just use uint8_t or uint16_t for example
> to declare an integer type storage when we know int is always
> platform-dependent? I do not have programming experience on other
> platform except M$ Windows and Linux.


Cuz that's stupid. The reason "int" isn't always a fixed size [it does have
minimums btw] is that it's meant to be an ideal platform size. E.g. for the
8086 it's 16-bits while for the 68000 it's 32-bits.

The idea is if I want something simple like

int x;
for (x = 0; x < strlen(buf); x++) {
buf[x] = tolower(buf[x]);
}

[or whatever].... the variable "x" should be easily manageable [e.g. not
32-bits on an 8086 and not 16-bits on a 68k].

A "long" was always meant to be "slightly less convenient but larger range".
That's not always the case though. on 68k/386 compilers "long" is typically
synonymous for "int" [except when array expressions are present cuz we all
know they must be int].

Tom


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-11-2003
Tom St Denis wrote:
>
> "Phui Hock" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I'm just curious, why don't we just use uint8_t or uint16_t for example
> > to declare an integer type storage when we know int is always
> > platform-dependent? I do not have programming experience on other
> > platform except M$ Windows and Linux.

>
> Cuz that's stupid. The reason "int" isn't always a fixed size [it does have
> minimums btw] is that it's meant to be an ideal platform size. E.g. for the
> 8086 it's 16-bits while for the 68000 it's 32-bits.
>
> The idea is if I want something simple like
>
> int x;
> for (x = 0; x < strlen(buf); x++) {
> buf[x] = tolower(buf[x]);
> }
>
> [or whatever].... the variable "x" should be easily manageable [e.g. not
> 32-bits on an 8086 and not 16-bits on a 68k].
>
> A "long" was always meant to be "slightly less convenient but larger range".
> That's not always the case though. on 68k/386 compilers "long" is typically
> synonymous for "int" [except when array expressions are present cuz we all
> know they must be int].


I was nodding my head and agreeing with you right up
until that final [remark]. Would you mind explaining just
what it is "we all know," because I can't figure it out.
What's an "array expression," and what has it to do with
the int-ness of anything?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      11-11-2003

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I was nodding my head and agreeing with you right up
> until that final [remark]. Would you mind explaining just
> what it is "we all know," because I can't figure it out.
> What's an "array expression," and what has it to do with
> the int-ness of anything?


When an expression is used to index an array...e.g.

int *p;

p[(some expression)] = (some other expression);

The expression on the left has to [technically] evaluate to an integer "int"
data type. I'm fairly certain it's a rule but I couldn't quote you the
section/page num of it.

Tom


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      11-11-2003
Tom St Denis <(E-Mail Removed)> scribbled the following:
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I was nodding my head and agreeing with you right up
>> until that final [remark]. Would you mind explaining just
>> what it is "we all know," because I can't figure it out.
>> What's an "array expression," and what has it to do with
>> the int-ness of anything?


> When an expression is used to index an array...e.g.


> int *p;


> p[(some expression)] = (some other expression);


> The expression on the left has to [technically] evaluate to an integer "int"
> data type. I'm fairly certain it's a rule but I couldn't quote you the
> section/page num of it.


AFAIK you're wrong. It doesn't have to evaluate to int. It merely has to
evaluate to some type that is assignable to int. For example, p[0l]
should be valid, as should be p[*"Hello world!"]. What about p[0.0]?
But, AYCS, that's only AFAIK, and it might be that WIKIW. YHBW. HTH,
HAND.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      11-11-2003

"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:borm65$lnc$(E-Mail Removed)...
> AFAIK you're wrong. It doesn't have to evaluate to int. It merely has to
> evaluate to some type that is assignable to int. For example, p[0l]
> should be valid, as should be p[*"Hello world!"]. What about p[0.0]?
> But, AYCS, that's only AFAIK, and it might be that WIKIW. YHBW. HTH,
> HAND.


Seems you're right. Given

int main(void)
{
char buf[10];

buf[0l] = 'a';
buf[0ul] = 'a';
buf[*"hello"] = 'a';
buf[0.0] = 'a';
return 0;
}

tombox root # gcc -O2 -Wall -W --std=c99 -pedantic -c test.c
test.c: In function `main':
test.c:7: warning: array subscript has type `char'
test.c:8: array subscript is not an integer

Tom


 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      11-11-2003

On Tue, 11 Nov 2003, Tom St Denis wrote:
>
> p[(some expression)] = (some other expression);
>
> [Tom thought...] The expression on the left has to [technically]
> evaluate to an integer "int" data type. I'm fairly certain it's a rule
> but I couldn't quote you the section/page num of it.


Close, but no cigar. The array-indexing [] operator takes
one pointer to a (complete) object type and one integer type.
From N869 s6.5.2.1 (Array subscripting):

[#1] One of the expressions [i.e., 'p' or '(some expression)', above]
shall have type ``pointer to object type'', the other expression
shall have integer type, and the result has type ``type''.

The integer types are described in section 6.2.5, particularly #17.

HTH,
-Arthur
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      11-11-2003

"Arthur J. O'Dwyer" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>
> On Tue, 11 Nov 2003, Tom St Denis wrote:
> >
> > p[(some expression)] = (some other expression);
> >
> > [Tom thought...] The expression on the left has to [technically]
> > evaluate to an integer "int" data type. I'm fairly certain it's a rule
> > but I couldn't quote you the section/page num of it.

>
> Close, but no cigar. The array-indexing [] operator takes
> one pointer to a (complete) object type and one integer type.
> From N869 s6.5.2.1 (Array subscripting):
>
> [#1] One of the expressions [i.e., 'p' or '(some expression)', above]
> shall have type ``pointer to object type'', the other expression
> shall have integer type, and the result has type ``type''.
>
> The integer types are described in section 6.2.5, particularly #17.


Swing and a miss. What probably happend is that someone else asked this, I
read the reply and munged "integer type" with "int".

Tom


 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      11-11-2003
On 11 Nov 2003 12:42:13 -0800, (E-Mail Removed) (Phui Hock) wrote:

>I'm just curious, why don't we just use uint8_t or uint16_t for example
>to declare an integer type storage when we know int is always
>platform-dependent? I do not have programming experience on other
>platform except M$ Windows and Linux.
>
>Thank you.


When the size of an integer variable is critical, we do. Otherwise,
the implementation probably has good reasons for the default size.

You do need to remember that support for the uint-type integers is not
universal.

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      11-12-2003
Phui Hock wrote:

> Why don't we just use uint8_t or uint16_t for example
> to declare an integer type storage
> when we know int is always platform-dependent?
> I do not have programming experience
> on other platform except M$ Windows and Linux.


Because there is no guarantee that uint8_t or uint16_t
will actually be supported by every C [99] compiler.
Use them only if you are certain that they are supported
on every potential target platform for your software.

To my knowledge, they are supported
by every C99 compiler for Windows on Intel computers
and Linux on Intel computers and IBM/Motorola PowerPC.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Portable Python - free portable development environment ! perica.zivkovic@gmail.com Python 7 01-13-2007 11:19 AM
portable (VHDL) vs. non-portable (altera LPM) approaches to signed computations Eli Bendersky VHDL 1 03-01-2006 02:43 PM
Vantec Portable USB2 Docking Station Video Review #599: Silverstrand Front Page News 0 09-15-2005 02:59 AM
Logitech MM22 Portable iPod Speakers @ BonaFideReviews Silverstrand Front Page News 0 08-05-2005 03:07 AM
Ultra Mini Portable Disk Enclosure Review @ PC Modding Malay Silverstrand Front Page News 0 07-01-2005 01:33 AM



Advertisments