views:

57

answers:

3

i know this sounds totally ridiculous at the moment but trust me, i want something like "$variable" in php or "def" in groovy, by means of my approach is an automatic variable "data type" identification to IMPLEMENT into c language.

for example:

"def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is needed to the make variable definitions detectable for the Groovy parser.

def dynamic  =  1
dynamic = "I am a String stored in a variable of dynamic type"

OR

Let's try creating a variable containing a string, and a variable containing a number:

<?php
$txt="Hello World!";
$x=16;
?>

PHP is a Loosely Typed Language In PHP, a variable does not need to be declared before adding a value to it. *In the example above, you see that you do not have to tell PHP which data type the variable is.* PHP automatically converts the variable to the correct data type, depending on its value. *In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.*

+1  A: 

There is no way of doing what you want in C. You need to know the type of a variable before you declare it, and you need to declare a variable before you use it.

GMan
agreed, and when did i asked for a standard way? all im saying is HOW TO IMPLEMENT this approach. maybe i should've put that in. sorry
abdulkareem_saad
@abd: What you're looking for, I think, is some sort of `typeof` operator. This exists as an *extension* on some compilers, but cannot be implemented by you. Maybe you should ask for help on the big picture and we can show you the steps, instead of ask for help on a step (that might not be a good one).
GMan
can you please name some compilers that i can try and get the implementation from? and how can i take it to the big picture? haha im still a noob here xDD
abdulkareem_saad
@abd: GCC has `typeof`. To tell us the big picture, tell us what problem you're trying to solve. I really doubt you need `typeof`, it just sounds like you don't want to program the C way. (But you have to.)
GMan
+1  A: 

You're asking for dynamic typing in a statically typed language. C has neither dynamic typing nor type inference, so this doesn't exist. It's quite possible to implement a tagged type system using C, and this is done by hundreds of languages with C interpreters, such as Python, PHP, Perl and so on - but as far as C is concerned, everything is still statically typed. C is not all that strongly typed, however, as you can cast pointers about without converting actual data.

If you want a language that resembles C, compiles to machine code, and has type inference, there's D with its reinterpretation of the auto keyword.

Yann Vernier
Note C++0x also uses `auto` this way.
GMan
i appreciate your effort in helping. trust me, i learned the right vocabs for that matter ^_^ lolso your saying its THAT hard to implement?
abdulkareem_saad
No, I'm saying it's not part of the language. The functionality has been implemented by many, with things like gobject etc. For instance, just as Groovy is built to implement such features on top of JVM, Vala uses gobject to do so with C - but C itself doesn't have the functionality.
Yann Vernier
A: 

Basically, you shouldn't do this in C. But, from what I understand, if you want to you can by declaring your variable as a void*. You will need to cast it back to the appropriate datatype in order to use it. It's been a little while since I wrote C so the following may not be 100% correct syntax but you could do something like this:

void* variable = "12345";
printf((char*) variable);

The downside to this approach is that you need to know what type the variable really is to cast it back to the correct type and if you get it wrong your application will come crashing down. From what I understand the id type in Objective-C is basically a struct that includes a void* type and some runtime type information but it can only point to objective-c classes so casting will be safe.

Blacktiger