Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - Question on Data Type Declarations

 
Thread Tools Search this Thread
Old 11-06-2009, 07:18 PM   #1
Default Question on Data Type Declarations


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?


phaedrus
  Reply With Quote
Old 11-06-2009, 07:26 PM   #2
Seebs
 
Posts: n/a
Default Re: Question on Data Type Declarations
On 2009-11-06, phaedrus <> 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 / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-06-2009, 07:30 PM   #3
phaedrus
 
Posts: n/a
Default Re: Question on Data Type Declarations
On Nov 6, 1:26*pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-11-06, phaedrus <orion.osi...@virgin.net> wrote:
>
> > Clue me up, please.

>
> What you ask may be possible.
>

I consider myself duly clued! THanks!


phaedrus
  Reply With Quote
Old 11-06-2009, 08:07 PM   #4
Seebs
 
Posts: n/a
Default Re: Question on Data Type Declarations
On 2009-11-06, phaedrus <> 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 / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-07-2009, 01:57 AM   #5
Keith Thompson
 
Posts: n/a
Default Re: Question on Data Type Declarations
Seebs <usenet-> writes:
> On 2009-11-06, phaedrus <> 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) kst- <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"


Keith Thompson
  Reply With Quote
Old 11-07-2009, 12:01 PM   #6
phaedrus
 
Posts: n/a
Default Re: Question on Data Type Declarations
On Nov 6, 7:57*pm, Keith Thompson <ks...@mib.org> 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?


phaedrus
  Reply With Quote
Old 11-07-2009, 12:15 PM   #7
Nick
 
Posts: n/a
Default Re: Question on Data Type Declarations
phaedrus <> writes:

> On Nov 6, 7:57Â*pm, Keith Thompson <ks...@mib.org> 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


Nick
  Reply With Quote
Old 11-07-2009, 01:29 PM   #8
Ben Bacarisse
 
Posts: n/a
Default Re: Question on Data Type Declarations
phaedrus <> writes:

> On Nov 6, 7:57Â*pm, Keith Thompson <ks...@mib.org> 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.


Ben Bacarisse
  Reply With Quote
Old 11-07-2009, 07:03 PM   #9
phaedrus
 
Posts: n/a
Default Re: Question on Data Type Declarations
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.


phaedrus
  Reply With Quote
Old 11-08-2009, 12:01 AM   #10
Ben Bacarisse
 
Posts: n/a
Default Re: Question on Data Type Declarations
Seebs <usenet-> writes:

> On 2009-11-07, phaedrus <> wrote:
>> On Nov 6, 7:57Â*pm, Keith Thompson <ks...@mib.org> 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.


Ben Bacarisse
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Motherboard general question Jimmy Sledge Computer Information 8 03-10-2007 08:37 AM
Question on learning "Networking"... DoubleEntendre Computer Support 15 03-11-2006 11:28 AM
hd partition question rabbit Computer Support 6 01-12-2006 04:05 AM
Wireless PEAP/MSCHAPV2 client programming question Jim Howard Wireless Networking 6 07-02-2005 12:53 PM
xp question Barry Computer Support 18 12-31-2003 06:06 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46