Re: Something About Date Type



"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.
.



Relevant Pages

  • [PATCH] drm: Add GEM ("graphics execution manager") to i915 driver.
    ... * Binds a collection of pages into AGP memory at the given offset, ... new file mode 100644 ... * The above copyright notice and this permission notice (including the ... +static int ...
    (Linux-Kernel)
  • [PATCH] drm: Add GEM ("graphics execution manager") to i915 driver.
    ... * Binds a collection of pages into AGP memory at the given offset, ... new file mode 100644 ... * The above copyright notice and this permission notice (including the ... +static int ...
    (Linux-Kernel)
  • Re: Is this correct..??
    ... long int i; ... Almost everthing is defined in assembler. ... Easily defeated by compiler optimisations. ... printf and that the first assignment to i is never used and decides to ...
    (comp.lang.c)
  • Re: How does the compiler do with this code?
    ... int printf(const char * restrict format, ... to assume the first argument is a format string. ... might, depending on the compiler, allow the compiler to catch ...
    (comp.os.linux.development.apps)
  • Re: pid_t data type
    ... That's because the compiler generates machine code for the printf() ... pid_t foo; ... int, or cast the expression to an int. ...
    (comp.unix.programmer)