473,406 Members | 2,549 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,406 software developers and data experts.

sizeof dataTypes at run time

Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Thanks and Regards,
Raman

Mar 26 '07 #1
34 1990
"Raman" <ramanchalo...@gmail.comwrote:
Is ut possible to calculate size of any standard data types at
run time i.e without using sizeof() operator
http://groups.google.com/group/comp....rch+this+group

--
Peter

Mar 26 '07 #2
Raman said:
Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
Sure - and this is especially easy since there is no such thing as the
sizeof() operator. All you have to do is instantiate the type, and use
the sizeof operator on the resulting object. This way, you avoid even
the appearance of using what someone has mistakenly described as a
sizeof() operator.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #3
On Mar 26, 10:54 am, "Raman" <ramanchalo...@gmail.comwrote:
Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Thanks and Regards,
Raman
Hi All,

U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).

Regards
jamsheed m(ja*******@gmail.com)
Mar 26 '07 #4
ja*******@gmail.com said:
U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).
Wrong wrong wrong wrong wrong, as you'd know if you'd tried the above
code yourself.

The correct answer, always, is: use sizeof. That is what it is *for*.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #5
In article <11**********************@l75g2000hse.googlegroups .com>,
ja*******@gmail.com <ja*******@gmail.comwrote:
>int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).
.... and always prints 1.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 26 '07 #6
Yes it's possible to know. use this macro

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

where 'x' is the variable.

sample.c:
......................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));

return 0;
}
..............................
Regards,
Ram

On Mar 26, 4:43 pm, "jamshe...@gmail.com" <jamshe...@gmail.comwrote:
On Mar 26, 10:54 am, "Raman" <ramanchalo...@gmail.comwrote:
Hi All,
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
Thanks and Regards,
Raman

Hi All,

U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).

Regards
jamsheed m(jamshe...@gmail.com)

Mar 26 '07 #7
yes. Its possible. Use the below macro.

sample program:
...................................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));

return 0;
}
..........................................

Regards,
Ram
Raman wrote:
Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Thanks and Regards,
Raman
Mar 26 '07 #8
ramana said:
yes. Its possible. Use the below macro.

sample program:
..................................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )
That doesn't calculate the size of a type.

Proof:

#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

int main(void)
{
printf("%d\n", (int)SIZE(double));
return 0;
}

foo.c: In function `main':
foo.c:7: parse error before `double'
foo.c:7: parse error before `)'

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 26 '07 #9
On 26 Mar 2007 06:14:28 -0700, "ramana" <ra*************@gmail.com>
wrote in comp.lang.c:

Do not top post in this group, it is considered rude. Text you add
should be after or interspersed with quoted text you are responding
to. I have reformatted your post and added my comments.
On Mar 26, 4:43 pm, "jamshe...@gmail.com" <jamshe...@gmail.comwrote:
On Mar 26, 10:54 am, "Raman" <ramanchalo...@gmail.comwrote:
Hi All,
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
Thanks and Regards,
Raman
Hi All,

U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).

Regards
jamsheed m(jamshe...@gmail.com)

Yes it's possible to know. use this macro

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )

where 'x' is the variable.

sample.c:
.....................
#include <stdio.h>

#define SIZE(x) ( ((char*)(&x + 1)) - (char*)&(x) )
The macro is correct.
int main()
{
char c;
int i;
float f;
double d;

printf(" sizes of char = %d \t int = %d \t float = %d \t double = %d
\n", SIZE(c), SIZE(i), SIZE(f), SIZE(d));
The printf() statement above is non-portable at best, and causes
undefined behavior at worst. The subtraction of two pointers yields a
result with a type of ptrdiff_t, which is a signed integer type but
not necessarily a signed int.

If ptrdiff_t happens to be signed int on your implementation, or
signed short (unlikely!), then the code will do what you want.

On implementations where ptrdiff_t is a wider type than int, and I
work with some, the code produces undefined behavior.

The obvious solution is to add a cast to int in your macro. Less
obvious but slightly better is to add a cast to long, and change the
conversion specifiers to %ld.

Best, of course, is get rid of the macro and use sizeof.
return 0;
}
.............................
Kindly learn how to post a proper signature line. It is separated
from the body of your message by the character string "-- " appearing
on a line by themselves. See my signature below for an example.
Regards,
Ram
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Mar 27 '07 #10
On 26 Mar 2007 04:43:28 -0700, "ja*******@gmail.com"
<ja*******@gmail.comwrote in comp.lang.c:
On Mar 26, 10:54 am, "Raman" <ramanchalo...@gmail.comwrote:
Hi All,

Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Thanks and Regards,
Raman

Hi All,

U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.
U doesn't post here anymore, we killed him.
int a;
printf("size of variable is %d\n",(&a+1)-(&a));
The printf() call above produces undefined behavior on systems where
the underlying type of ptrdiff_t is wider than int. Many such systems
exist.
This is applicable for all datatypes(even for array).

Regards
jamsheed m(ja*******@gmail.com)
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Mar 27 '07 #11
"ja*******@gmail.com" wrote:
>
.... snip ...
>
U can get a sizeof any variable without even knowing what is its
type or where it is used. The way is explained below as an example.

int a;
printf("size of variable is %d\n",(&a+1)-(&a));

This is applicable for all datatypes(even for array).
U doesn't read this newsgroup any more, and even if he did, he
would be ill-advised to take your advice, which involves undefined
and implementation defined behaviour. I am NOT referring to the
missing includes.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #12
Raman wrote:
>
Is ut possible to calculate size of any standard data types at
run time i.e without using sizeof() operator
Maybe this should be in the FAQ under the heading 'Foolish
Questions'?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #13
On Mar 26, 4:53 pm, Richard Heathfield <r...@see.sig.invalidwrote:
jamshe...@gmail.com said:
U can get a sizeof any variable without even knowing what is its type
or where it is used. The way is explained below as an example.
int a;
printf("size of variable is %d\n",(&a+1)-(&a));
This is applicable for all datatypes(even for array).

Wrong wrong wrong wrong wrong, as you'd know if you'd tried the above
code yourself.

The correct answer, always, is: use sizeof. That is what it is *for*.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

its possible
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.

Mar 27 '07 #14
ja*******@gmail.com said:

<snip>
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.
He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #15
On Mar 27, 2:10 pm, Richard Heathfield <r...@see.sig.invalidwrote:
jamshe...@gmail.com said:

<snip>
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.

He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
Then there is no way to know the size of the type directly without
crating a variable of that type.
we can never use sizeof(double) or something like this.
Please let me know if anyone knows the solution for that.

Mar 27 '07 #16
On Mar 27, 3:20 pm, "jamshe...@gmail.com" <jamshe...@gmail.comwrote:
On Mar 27, 2:10 pm, Richard Heathfield <r...@see.sig.invalidwrote:
jamshe...@gmail.com said:
<snip>
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.
He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".
Sorry for my misunderstanding.
>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Then there is no way to know the size of the type directly without
crating a variable of that type.
we can never use sizeof(double) or something like this.
Please let me know if anyone knows the solution for that.
No its possible to know the size using the type itself. Check the
sample program below.

#include <stdio.h>

#define SIZE(x) ( (long)(((char*)((x*)0 + 1)) - (char*)((x*)0)) )

int main()
{

printf(" sizes of char = %ld \t int = %ld \t float = %ld \t double =
%ld\n", SIZE(char), SIZE(int), SIZE(float), SIZE(double));

return 0;
}

I am new to posting to the groups. So i don't know the rules/practices
to follow. Please don't hesitate to tell me any that sort of stuff.

Thanks in advance.

--
Regards,
Ram

Mar 27 '07 #17
ramana wrote:
No its possible to know the size using the type itself. Check the
sample program below.

#include <stdio.h>

#define SIZE(x) ( (long)(((char*)((x*)0 + 1)) - (char*)((x*)0)) )

int main()
{

printf(" sizes of char = %ld \t int = %ld \t float = %ld \t double =
%ld\n", SIZE(char), SIZE(int), SIZE(float), SIZE(double));

return 0;
}
The expression `(x*)0 + 1` (for whatever type `x`) is undefined.

So, although it may work on some (perhaps many; even, perhaps, all)
implementations, it's not guaranteed to work at all.

--
Is it a bird? It is a plane? No, it's: http://hpl.hp.com/conferences/juc2007/
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Mar 27 '07 #18
ja*******@gmail.com said:
On Mar 27, 2:10 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>>
He didn't ask for the size of a variable, but for the size of a type.
Check the subject line: "sizeof dataTypes at run time".

Then there is no way to know the size of the type directly without
crating a variable of that type.
Sure there is. It's called sizeof.
we can never use sizeof(double) or something like this.
Yes, we can.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #19
"ja*******@gmail.com" <ja*******@gmail.comwrites:
we can never use sizeof(double) or something like this.
It's always funny when people who don't know C try to instruct
others on how to use it.
--
Ben Pfaff
http://benpfaff.org
Mar 27 '07 #20
Ben Pfaff wrote:
"ja*******@gmail.com" <ja*******@gmail.comwrites:
>we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.
We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!
[1] Apart from C99's variable-length arrays: are there variable-length
types to go with them?

--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
Meaning precedes definition.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Mar 27 '07 #21
Chris Dollin said:
Ben Pfaff wrote:
>"ja*******@gmail.com" <ja*******@gmail.comwrites:
>>we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!
<shrugSo use an interpreter. That way, there isn't a compile time.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #22
Richard Heathfield wrote:
Chris Dollin said:
>Ben Pfaff wrote:
>>"ja*******@gmail.com" <ja*******@gmail.comwrites:

we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrugSo use an interpreter. That way, there isn't a compile time.
The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?

Using an interpreter is a detail outside the scope of the standard.

PS We both think the answer is "use sizeof, that's what it's for!".

--
Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.
"Go not to the Elves for counsel, for they will answer both no and yes."/tLotR/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Mar 27 '07 #23
Chris Dollin said:
Richard Heathfield wrote:
>Chris Dollin said:
>>Ben Pfaff wrote:

"ja*******@gmail.com" <ja*******@gmail.comwrites:

we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrugSo use an interpreter. That way, there isn't a compile time.

The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?
But they can still both be at runtime, which is all the question
requires.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #24
Richard Heathfield wrote:
Chris Dollin said:
>Richard Heathfield wrote:
>>Chris Dollin said:

Ben Pfaff wrote:

"ja*******@gmail.com" <ja*******@gmail.comwrites:
>
>we can never use sizeof(double) or something like this.
>
It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrugSo use an interpreter. That way, there isn't a compile time.

The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?

But they can still both be at runtime, which is all the question
requires.
Compile-time is when sizeof happens. The /interpreter's/ runtime isn't
visible to a C program.

Urm: equine, deceased, selling/whipping?

--
JUC 2007, submit: http://hpl.hp.com/conferences/juc2007/submission.html
"You're not supposed to /think/ about it, you're supposed to say NO!"
Jill Swinburn, /The Beiderbeck Connection/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Mar 27 '07 #25
Chris Dollin said:

<snip>
Urm: equine, deceased, selling/whipping?
<sequitur relevance="non">
I have an interesting new analogy for why casting is almost always a bad
idea - based on a true story.
</sequitur>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #26
ramana wrote:
>
.... snip ...
>
I am new to posting to the groups. So i don't know the rules/practices
to follow. Please don't hesitate to tell me any that sort of stuff.
Read the following links:

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/(C99)
<http://www.dinkumware.com/refxc.html (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #27
"ja*******@gmail.com" wrote:
>
.... snip ...
>
its possible
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.
No it won't, it involves undefined behaviour, even if you did
#include <stdio.h>. The type of a pointer subtraction is not int.
When you lie to a variadic function about the type of parameters
all rules are cancelled, and you may launch WWIII. In addition

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

is considerable shorter and more understandable, besides being
defined. From N869:

6.5.3.4 The sizeof operator

Constraints

[#1] The sizeof operator shall not be applied to an
expression that has function type or an incomplete type, to
the parenthesized name of such a type, or to an expression
that designates a bit-field member.

Semantics

.... snip ...

[#4] The value of the result is implementation-defined, and
its type (an unsigned integer type) is size_t, defined in
the <stddef.hheader.

....

6.5.6 Additive operators

.... snip ...

[#9] When two pointers are subtracted, both shall point to
elements of the same array object, or one past the last
element of the array object; the result is the difference of
the subscripts of the two array elements. The size of the
result is implementation-defined, and its type (a signed
integer type) is ptrdiff_t defined in the <stddef.hheader.
If the result is not representable in an object of that
type, the behavior is undefined. In other words, if the
expressions P and Q point to, respectively, the i-th and j-
th elements of an array object, the expression (P)-(Q) has
the value i-j provided the value fits in an object of type
ptrdiff_t. Moreover, if the expression P points either to
an element of an array object or one past the last element
of an array object, and the expression Q points to the last
element of the same array object, the expression ((Q)+1)-(P)
has the same value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and
has the value zero if the expression P points one past the
last element of the array object, even though the expression
(Q)+1 does not point to an element of the array object.79)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #28
"Raman" <ra***********@gmail.comha scritto nel messaggio
news:11**********************@l75g2000hse.googlegr oups.com...
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
Try this:
Write a file containing the line:

#define REINVENTING_THE_WHEEL sizeof

and save it as newwheel.h.

Then use this:

#include <stdio.h>
#include "newwheel.h"
int main(void)
{
printf("%d\n", (int)REINVENTING_THE_WHEEL(int));
return 0;
}
Mar 29 '07 #29
"Army1987" <pl********@for.itwrites:
"Raman" <ra***********@gmail.comha scritto nel messaggio
news:11**********************@l75g2000hse.googlegr oups.com...
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."

I was doing this excerise recently as I am new to C and have been
learning from K&R2. I do not know if the technique I used was the
best.

But this is what I did. I set the bit values of the basic types to ~0.
For unsigned basic types this gave me the range. And signed the
maximum negative value. I could also determine thus by a comparison if
2's complement or 1's complement was being used. I thus had my answer
through direct computation.

I have not yet attempted to the floating-point excercise.

--
ilAn
Mar 31 '07 #30
ilan pillemer <il**@pillemer.netwrites:
But this is what I did. I set the bit values of the basic types to ~0.
For unsigned basic types this gave me the range.
It won't necessarily work for unsigned integer types wider than
int. For these you'll need to either use the proper type for 0,
as in e.g. ~0UL. Or you can just assign -1 to an object of the
unsigned integer type, which doesn't require such additional
care.

By the way, it's best to think of C objects as having values, not
representations or "bit values". Most operations in C operate on
values, not on representations.
And signed the maximum negative value. I could also determine
thus by a comparison if 2's complement or 1's complement was
being used. I thus had my answer through direct computation.
I think that ~0 can actually yield a trap representation.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Mar 31 '07 #31
ilan pillemer <il**@pillemer.netwrites:
"Army1987" <pl********@for.itwrites:
>"Raman" <ra***********@gmail.comha scritto nel messaggio
news:11**********************@l75g2000hse.googleg roups.com...
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."
[...]

The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 31 '07 #32
Keith Thompson <ks***@mib.orgwrites:
ilan pillemer <il**@pillemer.netwrites:
"Army1987" <pl********@for.itwrites:
"Raman" <ra***********@gmail.comha scritto nel messaggio
news:11**********************@l75g2000hse.googlegr oups.com...
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator
I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."
[...]

The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)

...hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?

If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?

--
ilAn
Apr 1 '07 #33
ilan pillemer said:
Keith Thompson <ks***@mib.orgwrites:
<snip>
>>
The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)


..hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?
Yes, because a char is exactly one byte wide. Incidentally, it is also
exactly CHAR_BIT bits wide.
If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?
Yes, you could do that, or you could simply do this:

unsigned char x = 1;
int char_bit = 0;
while(x 0)
{
++char_bit;
x <<= 1;
}

or of course you could just use CHAR_BIT from <limits.h>.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 1 '07 #34
Richard Heathfield <rj*@see.sig.invalidwrites:
ilan pillemer said:
Keith Thompson <ks***@mib.orgwrites:
<snip>
>
The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)

..hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?

Yes, because a char is exactly one byte wide. Incidentally, it is also
exactly CHAR_BIT bits wide.
If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?

Yes, you could do that, or you could simply do this:

unsigned char x = 1;
int char_bit = 0;
while(x 0)
{
++char_bit;
x <<= 1;
}

or of course you could just use CHAR_BIT from <limits.h>.
I know this. I am also learning from "Advanced Programmig in the UNIX
environment." On page 27, figure 2.6 Stevens/Rago lists 19 limits that
can be found in <limits.h>. The K&R2 exercise said I should also as an
exercise determine this kind of information by direct
computation. (Excerise 2-1 on page 36). That is why I am trying to do
this without referring to <limits.h>.

--
ilAn
Apr 1 '07 #35

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

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
1
by: Jan Agermose | last post by:
Im writing information into an existing excel document using a connection string like: strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel...
2
by: Mark Gibson | last post by:
Hello, I've been experimenting with dblink recently, and have encountered some limitations I'd like to discuss. I've been trying to create views of remote tables, like so: CREATE VIEW stuff...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.