Something About Date Type

D

Donkey

Hello, I want to find out how many digits does each date type have and
how many bytes does it occupy in memory, so I wrote a program below:

#include <stdio.h>
const long double num=1123222.232121342;
main()
{
Printf("the number occupies: %i, and it is 1123222.232121342
\n",sizeof num);
printf("char: %i ,the number is %c\n",sizeof(char),(char)num);
printf("widechar: %i\n",sizeof(wchar_t));
printf("singed char: %i\n",sizeof(signed char));
printf("unsigned char: %i\n",sizeof(unsigned char));
printf("int: %i, the number is %i\n",sizeof(int),(int)num);
printf("long int: %i, the number is %li\n",sizeof(long int),(long
int)num);
printf("float: %i, the number is %f\n", sizeof(float),float(num));
printf("double: %i, the number is
%lf\n",sizeof(double),(double)num);
printf("long double: %i,the number is %Lf\n",sizeof(long
double),(long double)num);
printf("long long int: %i\n",sizeof(long long int));
printf("long long double: %i\n",sizeof(long long double));
}

But the execution result of the program is very surprising:

the number occupies: 12, and it is 1123222.232121342
char: 1 ,the number is 
widechar: 2
singed char: 1
unsigned char: 1
int: 4, the number is 1123222
long int: 4, the number is 1123222
float: 4, the number is 1123222.250000
double: 8, the number is 1123222.232121
long double: 12,the number is -0.000000
long long int: 8
long long double: 8

You may notice that:
the memory occupied by int Date type and long int Date type is the
same,
print of long double number is not correct ,
and the memory long long double date type occupied is smaller (8 bytes)
than long double date type(12 bytes).
The Compiler I Use is GCC. The OS I use is Windows XP.
Can anyone tells me why do these problems happen?
Thanks a million.
 
J

Jonathan Burd

Donkey said:
Hello, I want to find out how many digits does each date type have and
how many bytes does it occupy in memory, so I wrote a program below:

Which ``date'' type are you talking about?
#include <stdio.h>
const long double num=1123222.232121342;
main()

int main (void) or
int main (int argc, char *argv[]) or
int main (int argc, char **argv).

Please read the newsgroup archive before posting code.
{
Printf("the number occupies: %i, and it is 1123222.232121342
\n",sizeof num);

Identifiers in C are case-sensitive. ``Printf'' is not the same as
``printf''
printf("char: %i ,the number is %c\n",sizeof(char),(char)num);

Loss of data. Why print a double value as a char?
sizeof evaluates to a value of type size_t. You can use
a cast to ``unsigned long'' here and %lu for the conversion
specifier.
printf("widechar: %i\n",sizeof(wchar_t));
printf("singed char: %i\n",sizeof(signed char));
printf("unsigned char: %i\n",sizeof(unsigned char));
printf("int: %i, the number is %i\n",sizeof(int),(int)num);
printf("long int: %i, the number is %li\n",sizeof(long int),(long
int)num);
printf("float: %i, the number is %f\n", sizeof(float),float(num));
printf("double: %i, the number is
%lf\n",sizeof(double),(double)num);
printf("long double: %i,the number is %Lf\n",sizeof(long
double),(long double)num);
printf("long long int: %i\n",sizeof(long long int));
printf("long long double: %i\n",sizeof(long long double));
}

main returns int.
But the execution result of the program is very surprising:

Not really surprising.

You may notice that:
the memory occupied by int Date type and long int Date type is the
same,

Again, which ``Date'' type? I don't see any such type in your code.

Can anyone tells me why do these problems happen?
Thanks a million.

They happen because you forget to read a good book on C.
Pick up K&R 2. :)


Regards,
Jonathan.
 
K

Keith Thompson

Donkey said:
Hello, I want to find out how many digits does each date type have and
how many bytes does it occupy in memory, so I wrote a program below:

#include <stdio.h>
const long double num=1123222.232121342;
main()

This should be "int main(void)".
{
Printf("the number occupies: %i, and it is 1123222.232121342\n",sizeof num);

printf, not Printf.

The "%i" format expects an int. sizeof yields a size_t.

To print the result of sizeof, convert it to unsigned long -- or use
"%zu" if your runtime library supports it (it's new in C99). For
example:

printf("sizeof num = %ld\n", (unsigned long)sizeof num);
printf("char: %i ,the number is %c\n",sizeof(char),(char)num);
printf("widechar: %i\n",sizeof(wchar_t));
printf("singed char: %i\n",sizeof(signed char));
printf("unsigned char: %i\n",sizeof(unsigned char));
printf("int: %i, the number is %i\n",sizeof(int),(int)num);
printf("long int: %i, the number is %li\n",sizeof(long int),(long int)num);
printf("float: %i, the number is %f\n", sizeof(float),float(num));

float(num) is a syntax error; you mean (float)num.
printf("double: %i, the number is %lf\n",sizeof(double),(double)num);
printf("long double: %i,the number is %Lf\n",sizeof(long double),(long double)num);
printf("long long int: %i\n",sizeof(long long int));
printf("long long double: %i\n",sizeof(long long double));

There is no "long long double" in standard C.

The code you posted isn't the code you compiled (unless your compiler
is entirely too forgiving). It would also be helpful if you kept your
line length down.
But the execution result of the program is very surprising:

the number occupies: 12, and it is 1123222.232121342
char: 1 ,the number is 
widechar: 2
singed char: 1
unsigned char: 1
int: 4, the number is 1123222
long int: 4, the number is 1123222
float: 4, the number is 1123222.250000
double: 8, the number is 1123222.232121
long double: 12,the number is -0.000000
long long int: 8
long long double: 8

You may notice that:
the memory occupied by int Date type and long int Date type is the
same,
print of long double number is not correct ,
and the memory long long double date type occupied is smaller (8 bytes)
than long double date type(12 bytes).
The Compiler I Use is GCC. The OS I use is Windows XP.

It's not uncommon for types int and long int to be the same size.

Possibly your runtime library (which is separate from your compiler)
doesn't handle "%Lf" properly.

Recent versions of gcc properly reject "long long double". A buggy
old version treats it as "long long".
 
P

Peter Shaggy Haywood

Groovy hepcat Keith Thompson was jivin' on Fri, 31 Mar 2006 06:17:34
GMT in comp.lang.c.
Re: Something About Date Type's a cool scene! Dig it!
To print the result of sizeof, convert it to unsigned long -- or use
"%zu" if your runtime library supports it (it's new in C99). For
example:

printf("sizeof num = %ld\n", (unsigned long)sizeof num);

No doubt Keith means this. (Note the %lu conversion specifier):

printf("sizeof num = %lu\n", (unsigned long)sizeof num);

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
K

Keith Thompson

Groovy hepcat Keith Thompson was jivin' on Fri, 31 Mar 2006 06:17:34
GMT in comp.lang.c.
Re: Something About Date Type's a cool scene! Dig it!


No doubt Keith means this. (Note the %lu conversion specifier):

printf("sizeof num = %lu\n", (unsigned long)sizeof num);

Yes, thanks for the correction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top