473,503 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RunTime Datatype Determination in C

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 intetc..

manish

Nov 23 '06 #1
9 1934
manish wrote:
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.
Nov 23 '06 #2
manish wrote:
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 intetc..
Those are compile-time things.

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

--
Chris "VOOM? What means this VOOM?" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Nov 23 '06 #3
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? :-)");
}
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 23 '06 #4
Richard Heathfield wrote:
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.

--
Chris "VOOM? What means this VOOM?" Dollin
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Nov 23 '06 #5
In article <11**********************@l12g2000cwl.googlegroups .com>,
manish <83*******@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 23 '06 #6

Richard Tobin wrote:
In article <11**********************@l12g2000cwl.googlegroups .com>,
manish <83*******@gmail.comwrote:
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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
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));
}
}

Nov 24 '06 #7
dbansal wrote:
>
.... snip ...
>
I have a program. Please tell me what do you think about this.
ERROR: failure to #include <stdio.h>
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 24 '06 #8
Richard Tobin wrote:
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.

Nov 24 '06 #9
CBFalconer wrote:
dbansal wrote:
... snip ...

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

ERROR: failure to #include <stdio.h>

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.
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'.

Nov 28 '06 #10

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

Similar topics

1
1606
by: Christopher Pragash | last post by:
I have two subclass deriving from a common base class. The base class exposes two shared functions for serialization and deserialization in XML format and some other methods that are overrided by...
3
5567
by: chellappa | last post by:
Hi EveryBody! i need a information about creatting structure at runtime.... like example ..... int main() { char *a,*b; printf("Enter the structure"); scanf("%s",a); printf ("Enter The...
8
5954
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
0
1831
by: Bill Toulias | last post by:
I want to be able to display the tuples of a select query (which may change at runtime, so no prior knowledge for column characteristics) and edit them as well. I used this code, that executes...
6
1250
by: Pete | last post by:
Hi, Is it possible for an application to determine, at runtime, whether an arbitrary class supports a known interface? I can think of a couple of cludgy ways of doing this (the calling app...
0
827
by: Corno | last post by:
Hi all, I'm making a menu page in ASP.NET in which I present the user with a list of pages that are available to him. In the web.config I will add a list of url's of all the pages. Those pages...
2
2063
by: derekbarrett | last post by:
Hi, I found this article in DB2 magazine and learned about the Problem Determination Mastery Exam. I am very interested in taking the exam, however, following the links in the article leads to...
3
5224
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
0
1200
by: graju80 | last post by:
I am kind of new to Db2... Question: What are the rules that DB2 uses to determine the right datatype for a particular column for on-the-fly SQL generation? For example...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
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,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.