typecasting

Discussion in 'C Programming' started by C learner, Oct 13, 2013.

  1. C learner

    C learner Guest

    Which of the statements is correct about the program?

    #include<stdio.h>

    int main()
    {
    float a=3.14;
    char *j;
    j = (char*)&a;
    printf("%d\n", *j);
    return 0;
    }
    [A].
    It prints ASCII value of the binary number present in the first byte of a float variable a. @
    . It prints character equivalent of the binary number present in the first byte of a float variable a.
    [C]. It will print 3
    [D]. It will print a garbage value
    C learner, Oct 13, 2013
    #1
    1. Advertising

  2. C learner

    James Kuyper Guest

    With respect to your Subject: header: "typecasting" is something that
    happens to actors, it doesn't apply to C programs. The operation you're
    talking about is simply called "casting".

    On 10/13/2013 08:03 AM, C learner wrote:
    > Which of the statements is correct about the program?
    >
    > #include<stdio.h>
    >
    > int main()
    > {
    > float a=3.14;
    > char *j;
    > j = (char*)&a;
    > printf("%d\n", *j);
    > return 0;
    > }
    > [A].
    > It prints ASCII value of the binary number present in the first byte of a float variable a. @
    > . It prints character equivalent of the binary number present in the first byte of a float variable a.
    > [C]. It will print 3
    > [D]. It will print a garbage value


    Answers , [C], and [D] each reflect a different, very serious
    misunderstanding of C. Why were you unable to rule them out?
    Answer [A] is the one that comes closest to being correct, but it misses
    some subtleties. The correct answer is:

    [E] it will interpret the contents of the first byte of the 'a' as a
    char. That integer value will then be promoted to an int. If that value
    is greater than INT_MAX (which would require that CHAR_MAX > INT_MAX,
    and therefore CHAR_MIN==0, and that CHAR_BITS>=16, all of which are
    permitted to a conforming implementation of C, but very unlikely). then
    this promotion will produce an implementation-defined result. In all
    other cases it will simply change the type without changing the value
    itself.
    Because you're printing it with %d, you will simply get the number
    represented by that value.

    If you had used %c, answer would have been correct. The program
    would have printed the corresponding character encoded by that value
    using whatever encoding was chosen by the implementation, which need not
    be ASCII. Since you didn't use %c, the character encoding (whether or
    not it is ASCII) have nothing to do with this.
    --
    James Kuyper
    James Kuyper, Oct 13, 2013
    #2
    1. Advertising

  3. C learner

    Geoff Guest

    On Sun, 13 Oct 2013 05:03:12 -0700 (PDT), C learner
    <> wrote:

    >Which of the statements is correct about the program?
    >
    >#include<stdio.h>
    >
    >int main()
    >{
    > float a=3.14;
    > char *j;
    > j = (char*)&a;
    > printf("%d\n", *j);
    > return 0;
    >}
    >[A].
    >It prints ASCII value of the binary number present in the first byte of a float variable a. @
    >. It prints character equivalent of the binary number present in the first byte of a float variable a.
    >[C]. It will print 3
    >[D]. It will print a garbage value


    Do your own homework.
    Geoff, Oct 14, 2013
    #3
  4. C learner

    BartC Guest

    "C learner" <> wrote in message
    news:...
    > Which of the statements is correct about the program?
    >
    > #include<stdio.h>
    >
    > int main()
    > {
    > float a=3.14;
    > char *j;
    > j = (char*)&a;
    > printf("%d\n", *j);
    > return 0;
    > }
    > [A].
    > It prints ASCII value of the binary number present in the first byte of a
    > float variable a. @
    > . It prints character equivalent of the binary number present in the
    > first byte of a float variable a.
    > [C]. It will print 3
    > [D]. It will print a garbage value


    How you tried running it?


    --
    Bartc
    BartC, Oct 14, 2013
    #4
  5. James Kuyper wrote:
    > With respect to your Subject: header: "typecasting" is something that
    > happens to actors, it doesn't apply to C programs. The operation
    > you're talking about is simply called "casting".
    >
    > On 10/13/2013 08:03 AM, C learner wrote:
    >> Which of the statements is correct about the program?
    >>
    >> #include<stdio.h>
    >>
    >> int main()
    >> {
    >> float a=3.14;
    >> char *j;
    >> j = (char*)&a;
    >> printf("%d\n", *j);
    >> return 0;
    >> }
    >> [A].
    >> It prints ASCII value of the binary number present in the first byte
    >> of a float variable a. @ . It prints character equivalent of the
    >> binary number present in the first byte of a float variable a. [C].
    >> It will print 3 [D]. It will print a garbage value

    >
    > Answers , [C], and [D] each reflect a different, very serious
    > misunderstanding of C. Why were you unable to rule them out?
    > Answer [A] is the one that comes closest to being correct, but it
    > misses some subtleties. The correct answer is:
    >
    > [E] it will interpret the contents of the first byte of the 'a' as a
    > char. That integer value will then be promoted to an int. If that
    > value is greater than INT_MAX (which would require that CHAR_MAX >
    > INT_MAX, and therefore CHAR_MIN==0, and that CHAR_BITS>=16, all of
    > which are permitted to a conforming implementation of C, but very
    > unlikely). then this promotion will produce an implementation-defined
    > result. In all other cases it will simply change the type without
    > changing the value itself.
    > Because you're printing it with %d, you will simply get the number
    > represented by that value.
    >
    > If you had used %c, answer would have been correct. The program
    > would have printed the corresponding character encoded by that value
    > using whatever encoding was chosen by the implementation, which need
    > not be ASCII. Since you didn't use %c, the character encoding
    > (whether or not it is ASCII) have nothing to do with this.


    Wow. Talk about knowing your stuff! You know I've never used a float.
    This post has just made me think. I've always used doubles. Do people even
    use the float type anymore? I can't recall ever seeing a C89 or C99 standard
    function accepting a float. But then again I don't know much.

    Bill
    Bill Cunningham, Oct 15, 2013
    #5
  6. C learner

    Tim Rentsch Guest

    James Kuyper <> writes:

    > With respect to your Subject: header: "typecasting" is
    > something that happens to actors, it doesn't apply to C
    > programs. The operation you're talking about is simply
    > called "casting".
    >
    > On 10/13/2013 08:03 AM, C learner wrote:
    >> Which of the statements is correct about the program?
    >>
    >> #include<stdio.h>
    >>
    >> int main()
    >> {
    >> float a=3.14;
    >> char *j;
    >> j = (char*)&a;
    >> printf("%d\n", *j);
    >> return 0;
    >> }
    >> [A]. It prints ASCII value of the binary number present in the
    >> first byte of a float variable a.
    >> . It prints character equivalent of the binary number present
    >> in the first byte of a float variable a.
    >> [C]. It will print 3
    >> [D]. It will print a garbage value

    >
    > Answers , [C], and [D] each reflect a different, very
    > serious misunderstanding of C. Why were you unable to rule
    > them out? Answer [A] is the one that comes closest to being
    > correct, but it misses some subtleties. The correct answer is:
    >
    > [E] it will interpret the contents of the first byte of the 'a'
    > as a char. That integer value will then be promoted to an int.
    > If that value is greater than INT_MAX (which would require that
    > CHAR_MAX > INT_MAX, and therefore CHAR_MIN==0, and that
    > CHAR_BITS>=16, all of which are permitted to a conforming
    > implementation of C, but very unlikely) then this promotion will
    > produce an implementation-defined result. In all other cases it
    > will simply change the type without changing the value itself.


    The first sentence in the last paragraph is right, but the rest
    is messed up. The 'char' value will be promoted to an 'int' if
    CHAR_MAX <= INT_MAX, or to an 'unsigned int' otherwise. The
    type of the result will be different, but the converted value
    will always be the same as the original (char) value. There
    is no implementation-defined aspect to the conversion, except
    that the type depends on the relative values of CHAR_MAX and
    INT_MAX.

    > Because you're printing it with %d, you will simply get the number
    > represented by that value.


    If the argument value is no greater than INT_MAX it will be
    printed, regardless of whether CHAR_MAX > INT_MAX. If the
    argument value is greater than INT_MAX (which can happen only if
    CHAR_MAX > INT_MAX), then the result is undefined behavior.
    Tim Rentsch, Oct 22, 2013
    #6
  7. C learner

    Guest

    In article <Xd%6u.1$>,
    Bill Cunningham <> wrote:
    > James Kuyper wrote:


    [ snip ]

    > Wow. Talk about knowing your stuff! You know I've never used a float.
    > This post has just made me think. I've always used doubles. Do people even
    > use the float type anymore? I can't recall ever seeing a C89 or C99 standard
    > function accepting a float. But then again I don't know much.
    >


    Fairly old post, but possibly still worth replying ....

    At least some of the math-library functions seem to have a version
    that accepts and returns doubles, and another that accepts/returns
    floats. Example are fabs/fabsf and sin/sinf. So, yeah, in this
    respect you don't know much (though why should you, really).

    As for whether people use them ....

    I can imagine that there are situations in which single-precision
    ("float") is good enough and reducing storage requirements by a
    factor of two would make a difference, though I don't know of
    specific examples.

    With "GPGPU" (General-Purpose computing on Graphics Processing
    Units), processing on the GPU may be limited to single-precision,
    depending on the details of the hardware, and to me that suggests
    that the whole computation might as well be single-precision. And
    my impression is that this is an active area of R&D these days.

    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
    , Nov 7, 2013
    #7
  8. <> writes:
    > In article <Xd%6u.1$>,
    > Bill Cunningham <> wrote:
    >> James Kuyper wrote:

    >
    > [ snip ]
    >
    >> Wow. Talk about knowing your stuff! You know I've never used a
    >> float. This post has just made me think. I've always used
    >> doubles. Do people even use the float type anymore? I can't recall
    >> ever seeing a C89 or C99 standard function accepting a float. But
    >> then again I don't know much.
    >>

    >
    > Fairly old post, but possibly still worth replying ....
    >
    > At least some of the math-library functions seem to have a version
    > that accepts and returns doubles, and another that accepts/returns
    > floats. Example are fabs/fabsf and sin/sinf. So, yeah, in this
    > respect you don't know much (though why should you, really).


    C90's <math.h> only defined functions with arguments and results of type
    double. If you wanted a square root of a float, you could use the
    double function and convert the argument and result:

    float x = 2.0;
    float sqrt_x = sqrt(x); /* two implicit conversions */

    If you wanted long double, you were out of luck unless you had a
    non-standard library.

    C99 added float and long double versions of the math functions, so now
    we have:

    double sqrt(double x);
    float sqrtf(float x);
    long double sqrtl(long double x);

    It also added <tgmath.h>, which defines a number of "type-generic
    macros", so that sqrt(x) would invoke either sqrtl(), sqrt(), or
    sqrtf(), depending on the type of the argument.

    C11 added _Generic, which allows programmers to do the same kind of
    thing <tgmath.h> does.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 7, 2013
    #8
  9. On Thursday, November 7, 2013 8:43:42 PM UTC, blmblm @ myrealbox. com wrote:
    > In article <Xd%6u.1$>,
    >
    > I can imagine that there are situations in which single-precision
    > ("float") is good enough and reducing storage requirements by a
    > factor of two would make a difference, though I don't know of
    > specific examples.
    >

    Graphics typically use floats for object co-ordinates. It rarely matters if
    the end result is a pixel out, and you tend to need a lot of geometry.
    Malcolm McLean, Nov 8, 2013
    #9
  10. <> writes:

    > I can imagine that there are situations in which single-precision
    > ("float") is good enough and reducing storage requirements by a
    > factor of two would make a difference, though I don't know of
    > specific examples.


    I can provide one. Real-time applications employing feedback (or
    feed-forward) where the sensors only provide (and the controls only
    allow for) sufficiently limited precision that a double wouldn't give
    more precise results to the actual application. Storage limitations may
    be less important than data-transfer speeds (this is definitely the case
    in my experience, but I don't have a sense of how typical that is).

    Be well.
    --
    Lowell Gilbert, embedded/networking software engineer
    http://be-well.ilk.org/~lowell/
    Lowell Gilbert, Nov 8, 2013
    #10
  11. On Friday, November 8, 2013 11:22:33 PM UTC+8, Lowell Gilbert wrote:
    > <> writes:
    >
    >
    >
    > > I can imagine that there are situations in which single-precision

    >
    > > ("float") is good enough and reducing storage requirements by a

    >
    > > factor of two would make a difference, though I don't know of

    >
    > > specific examples.

    >
    >
    >
    > I can provide one. Real-time applications employing feedback (or
    >
    > feed-forward) where the sensors only provide (and the controls only
    >
    > allow for) sufficiently limited precision that a double wouldn't give
    >
    > more precise results to the actual application. Storage limitations may
    >
    > be less important than data-transfer speeds (this is definitely the case
    >
    > in my experience, but I don't have a sense of how typical that is).
    >
    >
    >
    > Be well.
    >
    > --
    >
    > Lowell Gilbert, embedded/networking software engineer
    >
    > http://be-well.ilk.org/~lowell/

    Yep, I agree with you in the precision requiredment of those mass-production AV products.

    Audio data in 14 to 16 bits and
    visual data of RGB in 6 to 8 bits
    per color component are quite commen.

    It is different in scitific and military computations.

    For example, the GPS and RADAR parts of SAM systems require much more expensive fast computations and long precesions to defend the war zone
    than the cheap AV propducts.
    88888 Dihedral, Nov 8, 2013
    #11
  12. On 08-Nov-13 09:20, Malcolm McLean wrote:
    > On Thursday, November 7, 2013 8:43:42 PM UTC, blmblm @ myrealbox. com
    > wrote:
    >> I can imagine that there are situations in which single-precision
    >> ("float") is good enough and reducing storage requirements by a
    >> factor of two would make a difference, though I don't know of
    >> specific examples.

    >
    > Graphics typically use floats for object co-ordinates. It rarely
    > matters if the end result is a pixel out, and you tend to need a lot
    > of geometry.


    There is a growing demand for half-precision (16-bit) FP for such uses;
    would that be a "short float" in C?

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Nov 8, 2013
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Smlt?=

    ArrayList typecasting from binary SQL data

    =?Utf-8?B?Smlt?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    514
    Bruce Barker
    Apr 11, 2005
  2. Kapil Khosla

    Understanding Typecasting in C++

    Kapil Khosla, Jul 19, 2003, in forum: C++
    Replies:
    3
    Views:
    7,346
    John Harrison
    Jul 20, 2003
  3. Nicolay Korslund
    Replies:
    7
    Views:
    463
    Nicolay Korslund
    Sep 30, 2003
  4. venkatesh
    Replies:
    1
    Views:
    8,135
    lallous
    Dec 6, 2003
  5. Robert Street

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    6,942
    Robert Street
    Feb 21, 2004
Loading...

Share This Page