RunTime Datatype Determination in C

M

manish

we have avariablr

int bps; // bps determined during run time
-
-
-

how can we make conditional declaration of a variable y

we want appropriate declaration depending upon bps.

eg. short y; // for bps = 16
int y; // for bps = 32
long y; // for bps = 64

this is similar to Templates in C++..

like ClassX<int>, ClassX<short int> etc..

manish
 
M

Mathias Gaunard

manish said:
we have avariablr

int bps; // bps determined during run time
-
-
-

how can we make conditional declaration of a variable y

we want appropriate declaration depending upon bps.

Then you will have to do dynamic allocation, since it must occur at runtime.


this is similar to Templates in C++..

C++ templates work at compile-time.
You won't be able to choose between different templates instanciation
with a runtime value.
 
C

Chris Dollin

manish said:
we have avariablr

int bps; // bps determined during run time
-
-
-

how can we make conditional declaration of a variable y

You can't make a variable declaration conditional on a
runtime value [1].
we want appropriate declaration depending upon bps.

eg. short y; // for bps = 16
int y; // for bps = 32
long y; // for bps = 64

Why not just use `long y` in all cases?
this is similar to Templates in C++..
like ClassX<int>, ClassX<short int> etc..

Those are compile-time things.

[1] And doing it a compile-time needs to use the
preprocessor.
 
R

Richard Heathfield

Chris Dollin said:
You can't make a variable declaration conditional on a
runtime value [1].

if(x)
{
int i;
for(i = 0; i < 10; i++)
{
puts("You sure about that, Chris? :)");
}
}
 
C

Chris Dollin

Richard said:
Chris Dollin said:
You can't make a variable declaration conditional on a
runtime value [1].

if(x)
{
int i;
for(i = 0; i < 10; i++)
{
puts("You sure about that, Chris? :)");
}
}

Yes [1].

You're right that I was rather sloppy in what I /said/,
though. How about this:

The type of a variable declaration [and hence of
the variable] is not conditional on run-time
values.

I don't know the semantics of variable-length arrays
well enough [2] to know if they mess this one up.
Any offers?

[1] Being sure doesn't correlate well with being right,
either ...

[2] "At all" +- epsilon.
 
R

Richard Tobin

manish said:
we have avariablr
int bps; // bps determined during run time
we want appropriate declaration depending upon bps.

I don't really see why you want to do this, but perhaps you could use
a union of the possible types, and access the appropriate member
depending on bps?

-- Richard
 
D

dbansal

Richard said:
I don't really see why you want to do this, but perhaps you could use
a union of the possible types, and access the appropriate member
depending on bps?

-- Richard

Hi all,
I have a program. Please tell me what do you think about this.

int main(int x)
{
if(x==1)
{
int myval;
printf("size of integer myval: %d\n",sizeof (myval));
}
else
{
float myval;
printf("size of real myval: %d\n",sizeof (myval));
}
}
 
C

CBFalconer

dbansal said:
.... snip ...

I have a program. Please tell me what do you think about this.

ERROR: failure to #include said:
int main(int x)

ERROR: main can have two or no parameters. Not one. Usual is:

int main(int argc, char **argv)
{
if(x==1)
{
int myval;
printf("size of integer myval: %d\n",sizeof (myval));

ERROR: printf without a prototype.
ERROR: sizeof returns size_t, not int. Use
("%lu\n", (unsigned long) sizeof myval);
}
else
{
float myval;
printf("size of real myval: %d\n",sizeof (myval));

ERROR: printf without a prototype.
ERROR: sizeof returns size_t, not int. See above.

ERROR: main returns int. "return 0;" will do.

So, apart from not being a legal C program, all is well.
 
M

Mathias Gaunard

Richard said:
I don't really see why you want to do this, but perhaps you could use
a union of the possible types, and access the appropriate member
depending on bps?

The union will be at least as big as the biggest possible type.
So it's the same as using long everytime.
 
D

dbansal

CBFalconer said:
ERROR: main can have two or no parameters. Not one. Usual is:

int main(int argc, char **argv)


ERROR: printf without a prototype.
ERROR: sizeof returns size_t, not int. Use
("%lu\n", (unsigned long) sizeof myval);


ERROR: printf without a prototype.
ERROR: sizeof returns size_t, not int. See above.


ERROR: main returns int. "return 0;" will do.


So, apart from not being a legal C program, all is well.

Hi,
Thanks for the error checking in my code...but I think while doing this
you lost the objective. My wanted to know your comments in context of
runtime type determination of the variable 'myval'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top