Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question on Data Type Declarations

Reply
Thread Tools

Question on Data Type Declarations

 
 
phaedrus
Guest
Posts: n/a
 
      11-06-2009
Hi guys,

Clue me up, please.

When we declare variables like

float x;
int y;
char z;
long w;

and so on, are we doing anything more than just reserving a certain
number of bytes for these data types to occupy?
I mean, the compiler doesn't flag up an error if we try to store (for
example) a letter 'a' in an int, does it? Presumably we can store
*any* type of data in *any* variable type provided said declared type
has enough room to accommodate it?
Aren't the names 'float', 'int', 'char' and so forth simply there to
remind the programmer what data type the variable should be? The
compiler is blind to this human distinction, isn't it?
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-06-2009
On 2009-11-06, phaedrus <(E-Mail Removed)> wrote:
> Clue me up, please.


What you ask may be possible.

> When we declare variables like


> float x;
> int y;
> char z;
> long w;


> and so on, are we doing anything more than just reserving a certain
> number of bytes for these data types to occupy?


Yes.

We are reserving space and specifying how that space is to be interpreted.

> I mean, the compiler doesn't flag up an error if we try to store (for
> example) a letter 'a' in an int, does it?


No. But that's because:
1. 'a' is a constant of type int to begin with.
2. Even if you get the character (say, referring to "a"[0]), it's still got
an integral-type value.

> Presumably we can store
> *any* type of data in *any* variable type provided said declared type
> has enough room to accommodate it?


Nope.

> Aren't the names 'float', 'int', 'char' and so forth simply there to
> remind the programmer what data type the variable should be? The
> compiler is blind to this human distinction, isn't it?


Not in the least.

What might throw you off is that the compiler will, in many cases,
automatically perform the given conversion.

Consider:
#include <stdio.h>

int main(void) {
float f = 3.14;
int i;
i = f;
f = i;
printf("%f\n", f);
return 0;
}

This, on my system (where sizeof(float) happens to == sizeof(int)), prints
"3.000000". No warnings, no errors.

When f is assigned to i, the compiler automatically converts the float to
an int. When i is assigned back to f, the compiler automatically converts
back. But converting float to int truncates -- it drops fractional parts.

This can happen the other way, too:

#include <stdio.h>

int main(void) {
int i = 67108865;
float f;
f = i;
i = f;
printf("%d\n", i);
return 0;
}

On my system, this yields 67108864.

Perfectly reasonable, etcetera.

So, no. It's not just amount of storage; it's also representation that is
defined.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
phaedrus
Guest
Posts: n/a
 
      11-06-2009
On Nov 6, 1:26*pm, Seebs <(E-Mail Removed)> wrote:
> On 2009-11-06, phaedrus <(E-Mail Removed)> wrote:
>
> > Clue me up, please.

>
> What you ask may be possible.
>

I consider myself duly clued! THanks!
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      11-06-2009
On 2009-11-06, phaedrus <(E-Mail Removed)> wrote:
> I consider myself duly clued! THanks!


You didn't insult anyone, and you didn't make up an even less likely claim
and defend it to the last. I think they'll yank your Usenet license.

Seriously, though, interesting question; it wouldn't have occurred to me,
but it was interesting to think about.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-07-2009
Seebs <(E-Mail Removed)> writes:
> On 2009-11-06, phaedrus <(E-Mail Removed)> wrote:
>> Clue me up, please.

>
> What you ask may be possible.
>
>> When we declare variables like

>
>> float x;
>> int y;
>> char z;
>> long w;

>
>> and so on, are we doing anything more than just reserving a certain
>> number of bytes for these data types to occupy?

>
> Yes.
>
> We are reserving space and specifying how that space is to be interpreted.
>
>> I mean, the compiler doesn't flag up an error if we try to store (for
>> example) a letter 'a' in an int, does it?

>
> No. But that's because:
> 1. 'a' is a constant of type int to begin with.
> 2. Even if you get the character (say, referring to "a"[0]), it's still got
> an integral-type value.
>
>> Presumably we can store
>> *any* type of data in *any* variable type provided said declared type
>> has enough room to accommodate it?

>
> Nope.
>
>> Aren't the names 'float', 'int', 'char' and so forth simply there to
>> remind the programmer what data type the variable should be? The
>> compiler is blind to this human distinction, isn't it?

>
> Not in the least.
>
> What might throw you off is that the compiler will, in many cases,
> automatically perform the given conversion.

[snip]

On the other hand, in many cases there is no implicit conversion,
and the compiler will reject any attempt to perform an assignment.
(Well, actually it's only required to issue a diagnostic, but that
should tell you to reject it yourself if the compiler isn't kind
enough to do it for you.)

All the examples you used (float, int, char, long) are numeric types,
and any numeric type can be implicitly converted to any other numeric
type.

Try compiling this:

#include <stddef.h>
int main(void)
{
double d = 0.0;
int *p = NULL;
struct { int x; int y; } s = { 0, 0 };

d = 42; /* ok, implicit conversion of int to double */
p = 42; /* invalid, can't convert int to pointer (unless it's
a null pointer constant. */
s = 42; /* complete nonsense; you can't convert *anything* to a
struct type unless it's already of the same type */
return 0;
}

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
phaedrus
Guest
Posts: n/a
 
      11-07-2009
On Nov 6, 7:57*pm, Keith Thompson <(E-Mail Removed)> wrote:

>All the examples you used (float, int, char, long) are numeric types


Er, hang on. I'm obviously still not completely clued-up here. You say
the type 'char' is numeric?? Float, int and long are obviously
numeric, but char is obviously a single character like 'a' or 'b' or
'c' for example. A char, though capable of storing up to 0xFF of
values, is still just an alphabetical character, n'est pas?
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      11-07-2009
phaedrus <(E-Mail Removed)> writes:

> On Nov 6, 7:57*pm, Keith Thompson <(E-Mail Removed)> wrote:
>
>>All the examples you used (float, int, char, long) are numeric types

>
> Er, hang on. I'm obviously still not completely clued-up here. You say
> the type 'char' is numeric?? Float, int and long are obviously
> numeric, but char is obviously a single character like 'a' or 'b' or
> 'c' for example. A char, though capable of storing up to 0xFF of
> values, is still just an alphabetical character, n'est pas?


Yes. It is confusing, but in C all a char is is a particular sort of
small integer, and the single quotes around a character are just a way
of saying "the integer that represents that character in the character
set being used during compilation".

On an ASCII machine:
char a = 65;
and
int a = 'A';

both result in an integer variable (as distinct from an "int") holding
the same value.
--
Online waterways route planner: http://canalplan.org.uk
development version: http://canalplan.eu
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-07-2009
phaedrus <(E-Mail Removed)> writes:

> On Nov 6, 7:57*pm, Keith Thompson <(E-Mail Removed)> wrote:
>
>>All the examples you used (float, int, char, long) are numeric types

>
> Er, hang on. I'm obviously still not completely clued-up here. You say
> the type 'char' is numeric?? Float, int and long are obviously
> numeric, but char is obviously a single character like 'a' or 'b' or
> 'c' for example. A char, though capable of storing up to 0xFF of
> values, is still just an alphabetical character, n'est pas?


Non, mon amie. The name is confusing you. signed char and unsigned
char are simply two of C's integers types. Because characters in old
encodings like ASCII are all small numbers, char objects can be used
to hold them; but there are characters that C programmers need to
manipulate that don't fit in a char object, and char objects can be
used to manipulate things that aren't characters.

If I were manipulating pixels, I might use:

typedef unsigned char pixel[3];
pixel p = {128, 128, 128};

and do arithmetic on the components as with any other integer type:
p[2] = (p[0] + p[1]) / 2; and so on. Nowadays, I'd probably consider
using one of C99's size-specific types like uint_least8_t or
uint_fast8_t but that is a whole other issue.

On the other size, C provides wchar_t -- yet another integer type --
to hold characters that don't fit into a char.

There is no way to write "the letter A" in C simply because there is
no universal agreement about what number should be used to represent
an A. In C, 'A' is just an integer constant, and while it will be the
correct number to represent an A on the machine the C compiler is for,
the resulting code may not work if it is processing data coming from a
machine that has some other idea about the letter A.

--
Ben.
 
Reply With Quote
 
phaedrus
Guest
Posts: n/a
 
      11-07-2009
OK, all understood.

I'm not exactly a C noobie, btw. I'm just very rusty and trying to get
up to speed again. I was actually posting here some 15 years ago and
always remember this group had one of the best signal/noise ratios of
any I've ever seen and a extraordinarily erudite contributorship, and
not just in computer matters. I recognize some familiar names from the
past. Nice to see this group has survived all those years without too
much spam and personal attacks blighting it. Well done, chaps.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-08-2009
Seebs <(E-Mail Removed)> writes:

> On 2009-11-07, phaedrus <(E-Mail Removed)> wrote:
>> On Nov 6, 7:57*pm, Keith Thompson <(E-Mail Removed)> wrote:
>>>All the examples you used (float, int, char, long) are numeric types

>
>> Er, hang on. I'm obviously still not completely clued-up here. You say
>> the type 'char' is numeric?? Float, int and long are obviously
>> numeric, but char is obviously a single character like 'a' or 'b' or
>> 'c' for example. A char, though capable of storing up to 0xFF of
>> values, is still just an alphabetical character, n'est pas?

>
> No. It's a small (CHAR_BIT bits) integer.
>
> As it happens, that's sufficient to represent characters on most systems,
> but it's actually just another integer type. It's in no way special or
> different because of the use to represent characters.


But it is special! What other integer type has the property that
(T)-1 < 0 varies from implementation to implementation?

Whether char is special in the regard "because of the use to represent
characters" is certainly debatable, but I think a case can be that
char is different for exactly this reason. The fact that the
signedness of char is up to the compiler is a serious obstacle to
treating it as "just another integer type".

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does using generics complicate the use of interface-type declarations? jan V Java 15 09-12-2005 02:44 AM
question on generics in class type declarations Roger Levy Java 20 01-05-2005 04:07 AM
non-type template args and friend declarations Wolfgang Meyer C++ 1 04-23-2004 11:31 PM
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. luna ASP .Net 1 02-13-2004 01:15 PM
Mangled function type declarations? Marcus Lessard C Programming 1 10-17-2003 10:11 PM



Advertisments