sizeof(int) sizeof(long) etc. 
Author Message
 sizeof(int) sizeof(long) etc.

Hello to all:

Someone here defined what the minimum size in bits must be for
char, short, int, and long. Does someone have this information?

Second question:
is it possible to define a 32 bit int type with no ambiguity
and in a portable way? I guess it is impossible when compiling,
one has to run a configure-like software to fix the makefile
before and have a list of common architectures and compilers, but
maybe someone found a practical way to do this at run-time?

Last: in case it is not possible to discover how long are int types
during compilation or run-time, I would be glad to get a list of
most common architectures which run a Unix and the associated
ANSI C compilers, with all type definition so I would include
this in Makefiles once for all.

What I found in the FAQ are vaguely related informations.

Thanks a lot for helping,
--Nicolas



Tue, 05 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.

Quote:

>Hello to all:

>Someone here defined what the minimum size in bits must be for
>char, short, int, and long. Does someone have this information?

type : at least
char : 8 bits
short : 16 bits
int  : 16 bits
long : 32 bits

Quote:

>Second question:
>is it possible to define a 32 bit int type with no ambiguity
>and in a portable way?

  No.  Your best bet is to use long and make sure that your code doesn't
depend on the object being _exactly_ 32 bits wide.  On some machines there
is _no_ type that is exactly 32 bits wide (long would be 36 bits, because
bytes are 9 bits on some machines).

Quote:
>Last: in case it is not possible to discover how long are int types
>during compilation or run-time, I would be glad to get a list of
>most common architectures which run a Unix and the associated
>ANSI C compilers, with all type definition so I would include
>this in Makefiles once for all.

  Information on the type sizes supported by the implementation are given in
<limits.h>.  Your code can check against the appropriate #defined constant
(INT_MAX, ULONG_MAX) to see how big the types are.

- James                    "Good candidates for exceptions are situations

                                                        -- Kevin J. Hopps



Tue, 05 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.


Quote:

>Hello to all:

>Someone here defined what the minimum size in bits must be for
>char, short, int, and long. Does someone have this information?

>Second question:
>is it possible to define a 32 bit int type with no ambiguity
>and in a portable way? I guess it is impossible when compiling,
>one has to run a configure-like software to fix the makefile
>before and have a list of common architectures and compilers, but
>maybe someone found a practical way to do this at run-time?

try this program.

#include <stdio.h>

int main(void)
{

    printf("\nSizeof(int) = %d", sizeof(int));
    printf("\nSizeof(char) = %d", sizeof(char));
    printf("\nSizeof(long) = %l", sizeof(long));
    printf("\nSizeof(short) = %d", sizeof(short));
    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

return 0;

Quote:
}



Tue, 05 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.

Quote:

>Hello to all:

[snip]

Quote:
>Second question:
>is it possible to define a 32 bit int type with no ambiguity
>and in a portable way? I guess it is impossible when compiling,
>one has to run a configure-like software to fix the makefile
>before and have a list of common architectures and compilers, but
>maybe someone found a practical way to do this at run-time?

What about:

#include <limits.h>

#define MAX_32BITS 2147483647L

#if SHORT_MAX == MAX_32BITS
        typedef short INT32;
#elif INT_MAX == MAX_32BITS
        typedef int INT32;
#elif LONG_MAX == MAX_32BITS
        typedef long INT32;
#else
/* Panic? */
#endif

[snip]

Quote:

>Thanks a lot for helping,
>--Nicolas

Ken


Tue, 05 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.


: > try this program.
: >    printf("\nSizeof(int) = %d", sizeof(int));
: >    printf("\nSizeof(char) = %d", sizeof(char));
: >    printf("\nSizeof(long) = %l", sizeof(long));
: >    printf("\nSizeof(short) = %d", sizeof(short));
: >    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
: >    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

: Nice troll.  I presume you were inspired by one of Herb Schildt's
: books?  And that, in the same vein, if we wanted to know the
: sizes of types float and double, that'd be

:       printf("\nSizeof(float) = %f", sizeof(float));
:       printf("\nSizeof(double) = %lf", sizeof(double));
[snip]

In my HP-UX box:

Sizeof(float) = 0.000000
Sizeof(double) = 0.000000

I think we've stumbled onto a Nobel prize here :).  Truly infinite
storage must be the most important invention of the century.  ;)



Wed, 06 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.


Quote:
> Someone here defined what the minimum size in bits must be for
> char, short, int, and long. Does someone have this information?

The ANSI/ISO C Standard defines minimum ranges, not minimum
sizes.  From those ranges, we can infer that char must be at
least 8 bits, short int and int must be at least 16 bits, and
long int must be at least 32 bits.  (See also the FAQ list.)

Quote:
> is it possible to define a 32 bit int type with no ambiguity
> and in a portable way?

No.


Quote:

> try this program.
>    printf("\nSizeof(int) = %d", sizeof(int));
>    printf("\nSizeof(char) = %d", sizeof(char));
>    printf("\nSizeof(long) = %l", sizeof(long));
>    printf("\nSizeof(short) = %d", sizeof(short));
>    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
>    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

Nice troll.  I presume you were inspired by one of Herb Schildt's
books?  And that, in the same vein, if we wanted to know the
sizes of types float and double, that'd be

        printf("\nSizeof(float) = %f", sizeof(float));
        printf("\nSizeof(double) = %lf", sizeof(double));

(Didn't you mean to use %c for sizeof(char) and %h for
sizeof(short), though?)

For anyone out there who isn't laughing at this nonsense, no, of
course you can't print the result of the sizeof operator using
%f, and it's only slightly less silly to be trying to print the
sizes of the integral types using a format appropriate for the
type you're finding the size *of*.  In ANSI/ISO C, the result
type of sizeof is size_t, which is an unsigned type which might
be as large as a long.  The portable way to print the result of
applying sizeof is:

        printf("sizeof(int) = %lu"\n, (unsigned long)sizeof(int));

For the basic types, it would certainly be safe to use

        printf("sizeof(int) = %d"\n, (int)sizeof(int));

instead.  But you'll need an explicit cast in any case;
there's no one printf format which is portably guaranteed to
print a size_t.

                                        Steve Summit



Wed, 06 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.


Quote:



>: > try this program.
>: >    printf("\nSizeof(int) = %d", sizeof(int));
>: >    printf("\nSizeof(char) = %d", sizeof(char));
>: >    printf("\nSizeof(long) = %l", sizeof(long));
>: >    printf("\nSizeof(short) = %d", sizeof(short));
>: >    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
>: >    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

That made coke come out my nose. Thanks a lot.


Thu, 07 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.



Quote:




>>: > try this program.
>>: >    printf("\nSizeof(int) = %d", sizeof(int));
>>: >    printf("\nSizeof(char) = %d", sizeof(char));
>>: >    printf("\nSizeof(long) = %l", sizeof(long));
>>: >    printf("\nSizeof(short) = %d", sizeof(short));
>>: >    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
>>: >    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

>That made coke come out my nose. Thanks a lot.

It is well established that this can make demons fly out of your nose,
but coke???

--
-----------------------------------------


-----------------------------------------



Thu, 07 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.

Quote:




> : > try this program.
> : >    printf("\nSizeof(int) = %d", sizeof(int));
> : >    printf("\nSizeof(char) = %d", sizeof(char));
> : >    printf("\nSizeof(long) = %l", sizeof(long));
> : >    printf("\nSizeof(short) = %d", sizeof(short));
> : >    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
> : >    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

> : Nice troll.  I presume you were inspired by one of Herb Schildt's
> : books?  And that, in the same vein, if we wanted to know the
> : sizes of types float and double, that'd be

> :       printf("\nSizeof(float) = %f", sizeof(float));
> :       printf("\nSizeof(double) = %lf", sizeof(double));
> [snip]

> In my HP-UX box:

> Sizeof(float) = 0.000000
> Sizeof(double) = 0.000000

> I think we've stumbled onto a Nobel prize here :).  Truly infinite
> storage must be the most important invention of the century.  ;)

I think the statement should be
       printf("\nSizeof(float) = %d", sizeof(float));
       printf("\nSizeof(double) = %d", sizeof(double));

instead of "%f" & "%lf". Then it should work. if that was intended to
be a joke, please forgive me for not laughing.

Thanks,
Hari



Fri, 08 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.


Quote:

>>Hello to all:

>>Someone here defined what the minimum size in bits must be for
>>char, short, int, and long. Does someone have this information?

>>Second question:
>>is it possible to define a 32 bit int type with no ambiguity
>>and in a portable way? I guess it is impossible when compiling,
>>one has to run a configure-like software to fix the makefile
>>before and have a list of common architectures and compilers, but
>>maybe someone found a practical way to do this at run-time?

>try this program.

>#include <stdio.h>

>int main(void)
>{

>    printf("\nSizeof(int) = %d", sizeof(int));
>    printf("\nSizeof(char) = %d", sizeof(char));

Huh?  What do you need the above printf for?

Quote:
>    printf("\nSizeof(long) = %l", sizeof(long));
>    printf("\nSizeof(short) = %d", sizeof(short));
>    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
>    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

The last two printf's are redundant.  The signed and unsigned flavours
are required to have the same size.

The values produced by the sizeof operator have ALWAYS the same type
(size_t) no matter the type of the argument.  Since all we know about
size_t is that it is an unsigned type, the value has to be cast to a
known type before being passed to printf.

Quote:
>return 0;
>}

Another potentially interesting piece of information, which should be
printed, is the value of the macro CHAR_BIT.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Fri, 08 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.

Quote:





> > : > try this program.
> > : >    printf("\nSizeof(int) = %d", sizeof(int));
> > : >    printf("\nSizeof(char) = %d", sizeof(char));
> > : >    printf("\nSizeof(long) = %l", sizeof(long));
> > : >    printf("\nSizeof(short) = %d", sizeof(short));
> > : >    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
> > : >    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

> > : Nice troll.  I presume you were inspired by one of Herb Schildt's
> > : books?  And that, in the same vein, if we wanted to know the
> > : sizes of types float and double, that'd be

> > :       printf("\nSizeof(float) = %f", sizeof(float));
> > :       printf("\nSizeof(double) = %lf", sizeof(double));
> > [snip]

> > In my HP-UX box:

> > Sizeof(float) = 0.000000
> > Sizeof(double) = 0.000000

> > I think we've stumbled onto a Nobel prize here :).  Truly infinite
> > storage must be the most important invention of the century.  ;)

> I think the statement should be
>        printf("\nSizeof(float) = %d", sizeof(float));
>        printf("\nSizeof(double) = %d", sizeof(double));

> instead of "%f" & "%lf". Then it should work. if that was intended to
> be a joke, please forgive me for not laughing.

What do you think the :) meant?  Of course it was a joke.

Is  your answer supposed to be a joke?  If so, it's not nearly as
funny as Szu-Wen Huang's.  %d is not the correct format item to print
out the result of sizeof which must be unsigned.  sizeof returns a
size_t which may be any unsigned integral type.  The only portable way
to print out the result of sizeof is with a cast.  Something like

  printf("\nSizeof(float) = %lu", (unsigned long) sizeof(float));

Michael M Rubenstein



Fri, 08 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.


Quote:
>Someone here defined what the minimum size in bits must be for
>char, short, int, and long. Does someone have this information?

8, 16, 16, 32.

Quote:
>Second question:
>is it possible to define a 32 bit int type with no ambiguity
>and in a portable way?

NO.  The language doesn't guarantee the existence of a 32-bit integral
type and there are implementations that don't provide such a type.

Quote:
>I guess it is impossible when compiling,
>one has to run a configure-like software to fix the makefile
>before and have a list of common architectures and compilers, but
>maybe someone found a practical way to do this at run-time?

At run-time is way too late.

Quote:
>Last: in case it is not possible to discover how long are int types
>during compilation or run-time, I would be glad to get a list of
>most common architectures which run a Unix and the associated
>ANSI C compilers, with all type definition so I would include
>this in Makefiles once for all.

A very bad idea.  More and more 64-bit implementations will appear,
so nothing is cast in concrete forever.

The sizes of the various types can be determined before the compilation,
by running a short and trivial program from the configuration script.

For the vast majority of the Unices in common use, the following
assumptions are true: char - 8 bits, short - 16 bits, int - 32 bits,
long - 32 or 64 bits.  However, UNICOS doesn't conform to this model.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Fri, 08 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.

Quote:



>> : Nice troll.  I presume you were inspired by one of Herb Schildt's
>> : books?  And that, in the same vein, if we wanted to know the
>> : sizes of types float and double, that'd be
>> :       printf("\nSizeof(float) = %f", sizeof(float));
>> :       printf("\nSizeof(double) = %lf", sizeof(double));
>> In my HP-UX box:
>> Sizeof(float) = 0.000000
>> Sizeof(double) = 0.000000
>> I think we've stumbled onto a Nobel prize here :).  Truly infinite
>> storage must be the most important invention of the century.  ;)

>I think the statement should be
>       printf("\nSizeof(float) = %d", sizeof(float));
>       printf("\nSizeof(double) = %d", sizeof(double));

>instead of "%f" & "%lf". Then it should work. if that was intended to
>be a joke, please forgive me for not laughing.

  Both Steve and Szu-Wen are joking, and I at least can forgive you
for not laughing. :-)  However, as one who has sinned many times himself, I
cannot forgive you for printing a size_t as an int without a cast.  The
only safe way to display the value of a size_t is by casting it to
unsigned long and printing it with %lu, eg:

  printf("\nSizeof(float) = %lu", (unsigned long)sizeof(float));

  For strings:
char *mystring = malloc(sizeof(input));   /* :-) */
printf("\nSizeof(mystring) = %s", sizeof(mystring)); /* :-) */

  or maybe not.

--
James Robinson            "OK_RRL, record locked against read but read anyway:


                           success status."        -- OpenVMS System Messages



Fri, 08 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.

[warning: getting off-topic]


Quote:






[snipped a bit]
>>>: >    printf("\nSizeof(int) = %d", sizeof(int));
>>>: >    printf("\nSizeof(long) = %l", sizeof(long));
>>>: >    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

>>That made coke come out my nose. Thanks a lot.

>It is well established that this can make demons fly out of your nose,
>but coke???

As we drop into the realms of un- and implementation- defined
behaviour, the operation

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SINUS_CAPACITY 99
#define nose strcpy
int main() {
        FILE* b; FILE* k;
        char i[SINUS_CAPACITY]; char o[SINUS_CAPACITY];
        if ((b= fopen("beverage", "r")) == 0) {
                fprintf(stderr, "\ago to the vending machine\n");
                exit(EXIT_FAILURE);
        }
        if ((k= fopen("keyboard", "w")) == 0) {
                fprintf(stderr, "\aread Usenet\n");
                exit(EXIT_FAILURE);
        }
        while (fgets(i, sizeof i, b)) {
                nose(o, i);
                fputs(o, k);
        }
        fclose(b); fclose(k);
        return 0;

Quote:
}

or as it's more commonly represented, in shell script:
cat beverage | nose > keyboard

becomes more common, especially among the denizens of
alt.sysadmin.recovery where I believe the verb describing the act is
"to snorkle".  I think this discussion should probably head there
instead :-).
--



Sat, 09 Jan 1999 03:00:00 GMT  
 sizeof(int) sizeof(long) etc.



Quote:






>>>: > try this program.
>>>: >    printf("\nSizeof(int) = %d", sizeof(int));
>>>: >    printf("\nSizeof(char) = %d", sizeof(char));
>>>: >    printf("\nSizeof(long) = %l", sizeof(long));
>>>: >    printf("\nSizeof(short) = %d", sizeof(short));
>>>: >    printf("\nSizeof(unsigned) = %u", sizeof(unsigned));
>>>: >    printf("\nSizeof(unsigned long) = %lu", sizeof(unsigned long));

>>That made coke come out my nose. Thanks a lot.

>It is well established that this can make demons fly out of your nose,
>but coke???

Anyway, I thought the coke was meant to go *into* your nose?

John

--
John Winters.  Wallingford, Oxon, England.



Sat, 09 Jan 1999 03:00:00 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

1. relation between sizeof(void*), sizeof(int)

2. Basic question on sizeOf variables int, long

3. Sizeof(long/int/char) on Crays

4. sizeof (struct) ! = sizeof struct elements

5. sizeof without sizeof

6. sizeof a/sizeof *a

7. sizeof(foo) -1 or sizeof(foo)

8. sizeof(void*) versus sizeof(foo*)

9. /Wp64 and sizeof int question

10. Why not sizeof int?

11. how to modify a pointer to an array one sizeof(int)

12. sizeof(int)

 

 
Powered by phpBB® Forum Software