Something About Date Type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Donkey

    #1

    Something About Date Type

    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.232 121342;
    main()
    {
    Printf("the number occupies: %i, and it is 1123222.2321213 42
    \n",sizeof num);
    printf("char: %i ,the number is %c\n",sizeof(ch ar),(char)num);
    printf("widecha r: %i\n",sizeof(wc har_t));
    printf("singed char: %i\n",sizeof(si gned char));
    printf("unsigne d char: %i\n",sizeof(un signed char));
    printf("int: %i, the number is %i\n",sizeof(in t),(int)num);
    printf("long int: %i, the number is %li\n",sizeof(l ong int),(long
    int)num);
    printf("float: %i, the number is %f\n", sizeof(float),f loat(num));
    printf("double: %i, the number is
    %lf\n",sizeof(d ouble),(double) num);
    printf("long double: %i,the number is %Lf\n",sizeof(l ong
    double),(long double)num);
    printf("long long int: %i\n",sizeof(lo ng long int));
    printf("long long double: %i\n",sizeof(lo ng long double));
    }

    But the execution result of the program is very surprising:

    the number occupies: 12, and it is 1123222.2321213 42
    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.

  • Jonathan Burd

    #2
    Re: Something About Date Type

    Donkey wrote:[color=blue]
    > 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:
    >[/color]

    Which ``date'' type are you talking about?
    [color=blue]
    > #include <stdio.h>
    > const long double num=1123222.232 121342;
    > main()[/color]

    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.
    [color=blue]
    > {
    > Printf("the number occupies: %i, and it is 1123222.2321213 42
    > \n",sizeof num);[/color]

    Identifiers in C are case-sensitive. ``Printf'' is not the same as
    ``printf''
    [color=blue]
    > printf("char: %i ,the number is %c\n",sizeof(ch ar),(char)num);[/color]

    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.
    [color=blue]
    > printf("widecha r: %i\n",sizeof(wc har_t));
    > printf("singed char: %i\n",sizeof(si gned char));
    > printf("unsigne d char: %i\n",sizeof(un signed char));
    > printf("int: %i, the number is %i\n",sizeof(in t),(int)num);
    > printf("long int: %i, the number is %li\n",sizeof(l ong int),(long
    > int)num);
    > printf("float: %i, the number is %f\n", sizeof(float),f loat(num));
    > printf("double: %i, the number is
    > %lf\n",sizeof(d ouble),(double) num);
    > printf("long double: %i,the number is %Lf\n",sizeof(l ong
    > double),(long double)num);
    > printf("long long int: %i\n",sizeof(lo ng long int));
    > printf("long long double: %i\n",sizeof(lo ng long double));
    > }[/color]

    main returns int.
    [color=blue]
    >
    > But the execution result of the program is very surprising:[/color]

    Not really surprising.

    <snip>
    [color=blue]
    >
    > You may notice that:
    > the memory occupied by int Date type and long int Date type is the
    > same,[/color]

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

    <snip>
    [color=blue]
    > Can anyone tells me why do these problems happen?
    > Thanks a million.
    >[/color]

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


    Regards,
    Jonathan.

    --
    Meow!

    Comment

    • Keith Thompson

      #3
      Re: Something About Date Type

      "Donkey" <newsdonkey@hot mail.com> writes:[color=blue]
      > 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.232 121342;
      > main()[/color]

      This should be "int main(void)".
      [color=blue]
      > {
      > Printf("the number occupies: %i, and it is 1123222.2321213 42\n",sizeof num);[/color]

      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);
      [color=blue]
      > printf("char: %i ,the number is %c\n",sizeof(ch ar),(char)num);
      > printf("widecha r: %i\n",sizeof(wc har_t));
      > printf("singed char: %i\n",sizeof(si gned char));
      > printf("unsigne d char: %i\n",sizeof(un signed char));
      > printf("int: %i, the number is %i\n",sizeof(in t),(int)num);
      > printf("long int: %i, the number is %li\n",sizeof(l ong int),(long int)num);
      > printf("float: %i, the number is %f\n", sizeof(float),f loat(num));[/color]

      float(num) is a syntax error; you mean (float)num.
      [color=blue]
      > printf("double: %i, the number is %lf\n",sizeof(d ouble),(double) num);
      > printf("long double: %i,the number is %Lf\n",sizeof(l ong double),(long double)num);
      > printf("long long int: %i\n",sizeof(lo ng long int));
      > printf("long long double: %i\n",sizeof(lo ng long double));[/color]

      There is no "long long double" in standard C.
      [color=blue]
      > }[/color]

      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.
      [color=blue]
      > But the execution result of the program is very surprising:
      >
      > the number occupies: 12, and it is 1123222.2321213 42
      > 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.[/color]

      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_Keit h) kst-u@mib.org <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.

      Comment

      • Keith Thompson

        #4
        Re: Something About Date Type

        Jonathan Burd <jb@nomail.co m> writes:[color=blue]
        > Donkey wrote:[color=green]
        >> 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:[/color]
        >
        > Which ``date'' type are you talking about?[/color]

        Perhaps it was a typo for "data type".

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

        Comment

        • Peter Shaggy Haywood

          #5
          Re: Something About Date Type

          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!
          [color=blue]
          >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);[/color]

          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!


          "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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?

          Comment

          • Keith Thompson

            #6
            Re: Something About Date Type

            phaywood@alphal ink.com.au.NO.S PAM (Peter "Shaggy" Haywood) writes:[color=blue]
            > 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!
            >[color=green]
            >>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);[/color]
            >
            > No doubt Keith means this. (Note the %lu conversion specifier):
            >
            > printf("sizeof num = %lu\n", (unsigned long)sizeof num);[/color]

            Yes, thanks for the correction.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

            Comment

            Working...