473,656 Members | 2,997 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 1948
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************ **********@l12g 2000cwl.googleg roups.com>,
manish <83*******@gmai l.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
--
"Considerat ion 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************ **********@l12g 2000cwl.googleg roups.com>,
manish <83*******@gmai l.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
--
"Considerat ion 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
1614
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 the derived classes. I want to be able to serialize and deserialize the derived classes through the methods in the base class, without explicitly passing the type of the object. I want to determine at runtime the type of the derived class calling...
3
5581
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 Datatype");
8
5961
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; this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ApplicantName", dataset.Tables.Columns.DataType, 50,
0
1839
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 the select, gets schema from the datareader, creates a table that can host data of that type, fills it with data from the datareader and then binds it to a DataGrid. The problem is that although i seem to do everything correctly and although all...
6
1266
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 will know the details of the interface, so worst-case could just blindly try invoking a method) but I'm scanning through the reflection documentation to find a clean way of doing this, and haven't found anything thus far.
0
844
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 have different authorization settings and I would like the menu to only show the user the pages that he can really open. Authorization is role based (windows athentication in combination with 'allow'
2
2069
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 DB2 tutorials and exams, but no mention of the Problem Determination Mastery Exam. Now, the DB2 9 Viper exams just came out, so I was wondering if there is a new version of the Problem Determination exam coming out? I did search around the IBM...
3
5232
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 database and display it in a Image web control dynamically(at runtime). The process after being displayed in the web control, user click insert/add button, it converts the image(jpeg) file to bytes and store it the database with Image or VarBinary...
0
1210
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
8380
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
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 we have to send another system
2
1928
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.