Re: Something About Date Type
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 31 Mar 2006 06:17:34 GMT
"Donkey" <newsdonkey@xxxxxxxxxxx> writes:
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".
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Something About Date Type
- From: Donkey
- Something About Date Type
- Prev by Date: Re: Something About Date Type
- Next by Date: Re: How to Define High-precision Date type
- Previous by thread: Re: Something About Date Type
- Next by thread: How to Define High-precision Date type
- Index(es):
Relevant Pages
|