No. of bytes?

This is a discussion on No. of bytes? within the C Programming forums, part of the General Programming Boards category; Need to confirm. Does float has 8 bytes in a 32 bit machine? That means: char - 1 byte int ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    16

    No. of bytes?

    Need to confirm.

    Does float has 8 bytes in a 32 bit machine?

    That means:
    char - 1 byte
    int - 4 bytes
    float - 8 bytes
    double - 16 bytes

    Am I correct?

    Thank you.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Just use sizeof( int ), sizeof( float ), etc.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Correct.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    have to throw this in here

    #include <limits.h>
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Originally posted by chrismiceli
    have to throw this in here

    #include <limits.h>
    How to use this function header? I never use b4 but see b4?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    same as any other header.

    It allows you to use things like INT_MAX and UINT_MAX directly.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    #include <stdio.h>
    int main(void)
    {
    float b;
    b = 65.0;
    printf("The value of b is %d\n", b);
    return 0;

    }
    OUTPUT: The value of b is 0.

    Why the output is not 65 with no decimal place?

    Pls help.

    Thanks

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    1. use %f (not %d)

    Some compilers will warn you of such format mis-matches
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Originally posted by Salem
    1. use %f (not %d)

    Some compilers will warn you of such format mis-matches
    Yup, I know. I want to know why using %d will cause value to be 0 and not 65?

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    What's the difference b/w exit(1) and exit(0)? When do we use them?

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use exit( 0 ) if the program is exiting normally, without any problems, otherwise, you can use exit( 1 ) if there was a problem.

    When you pass %d to printf, it attempts to read the variable as an integer. But when you pass a float to it, integers and floats are stored differently in memory, so it won't output the proper value.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Originally posted by XSquared
    Use exit( 0 ) if the program is exiting normally, without any problems, otherwise, you can use exit( 1 ) if there was a problem.

    When you pass %d to printf, it attempts to read the variable as an integer. But when you pass a float to it, integers and floats are stored differently in memory, so it won't output the proper value.
    I thought using %d the compiler will convert 65.0 to 65 which is an integer right? But why the value turn out to be 0? I still dun understand?

    Let's say if I use exit(1) if program exit normally while using exit(0) if there was a problem. What would happen? Will the program still function normally or abnormally? How abnormal?

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > I thought using %d the compiler will convert 65.0 to 65 which is an integer right?
    Wrong.
    Any function which expects a variable number of arguments (like printf) cannot perform any data type conversions on the variable arguments, since there is no parameter type information available.
    The "%d" in printf() is part of the "language" of printf, not part of the language of C.
    There's nothing to stop you writing
    Code:
    void my_print ( const char *fmt, ... );
    which uses "!d" to format a decimal integer.


    > But why the value turn out to be 0?
    Luck?
    As XSquared explained, the data formats are different, so what you get is anyones guess.

    > Will the program still function normally or abnormally? How abnormal?
    The program will be fine - however, anything trying to use the exit status will be in a sorry state.
    You should really use EXIT_SUCCESS and EXIT_FAILURE (in stdlib.h) for maximum portability
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by jnwk888hwq
    I thought using %d the compiler will convert 65.0 to 65 which is an integer right? But why the value turn out to be 0? I still dun understand?
    It's undefined behaviour. Your compiler could do pretty much anything, and there's no guarantee that any other compiler will do the same thing. The argument type should match the format specifier.

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Like others have said, this is undefined behaviour, what you need to do is this:

    printf("The value of b is %d\n", (int)b);

    Code:
     
    Which will give you the result you require.

    Originally posted by jnwk888hwq
    #include <stdio.h>
    int main(void)
    {
    float b;
    b = 65.0;
    printf("The value of b is %d\n", b);
    return 0;

    }
    OUTPUT: The value of b is 0.

    Why the output is not 65 with no decimal place?

    Pls help.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. Shared class members over Dll Boundary
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 11-13-2007, 12:43 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21