[OCLUG-devel] sizeof () works differently on char's, int's, etc.

John Heil johnhscs at scsoftware.sc-software.com
Wed Jun 2 21:49:09 PDT 2004


On Wed, 2 Jun 2004, James Colannino wrote:

> Date: Wed, 02 Jun 2004 21:34:59 -0700
> From: James Colannino <james at colannino.org>
> To: oclug-devel at oclug.org
> Subject: [OCLUG-devel] sizeof () works differently on char's, int's, etc.
>
> Hey everyone.  I used the following to test how the sizeof() function
> measures arrays of various variable types:
>
> #include <stdio.h>
>
> char  line[10];
> int   integer[10];
> float floaty[10];
>
> int main()
>
> {
> 	printf ("\nCharacter: %d\n", sizeof(line));
> 	printf ("Integer: %d\n", sizeof(integer));
> 	printf ("Floating: %d\n\n", sizeof(floaty));
>
> 	return 0;
> }
>
> My output came out as the following:
>
> Character: 10
> Integer: 40
> Floating: 40
>
> So my question is this: why does the sizeof() function work with
> character arrays but not with numerical ones?  Is the sizeof() function
> only intended to be used for characters and not for numbers?  This has
> me very confused.
>
> James


This is due to the intrinsic size of the data type.
The array count counts the number of occurances of the data type
which means that the total_size == data_type_size * count

data type       size    count      total array size
=========       ====    =====      ================

  char		 1	10		10
  int		 4	10		40
  float		 4	10		40


Note that the size of int is platform dependent and is _not_
guaranteed to be 4 everywhere. On Intel/AMD 32 chips, it is 4.
Other chips, maybe not.

IIRC ditto for float.


cheers
johnh

> --
> My blog: http://www.crazydrclaw.com/
> My homepage: http://james.colannino.org/
>
> "There are no uninteresting things; only uninterested people." --G.K.
> Chesterton
>
> _______________________________________________
> OCLUG-devel mailing list -- OCLUG-devel at oclug.org
> http://www.oclug.org/mailman/listinfo/oclug-devel
>

-
-----------------------------------------------------------------
John Heil
South Coast Software
Custom systems software for UNIX and IBM MVS mainframes
1-714-774-6952
johnhscs at sc-software.com
http://www.sc-software.com
-----------------------------------------------------------------


More information about the OCLUG-devel mailing list