#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2

    Sizeof data types


    I am getting output for below code as 8 4 4 in gcc version 4.5.2. Can anyone explain why I am getting this output ?

    Code:
    #include<stdio.h>
    int main()
    {
        printf("%d\t",sizeof(6.5));
        printf("%d\t",sizeof(90000));
        printf("%d",sizeof('A'));
        return 0;
    }
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Because on your system, a floating point number is 8 bytes, an integer is 4 bytes, and a character is 4 bytes.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2
    Originally Posted by E-Oreo
    Because on your system, a floating point number is 8 bytes, an integer is 4 bytes, and a character is 4 bytes.
    How can integer and char can take same byte size ?

    I guess I found the answer. In sizeof('A'), 'A' is replaced by 65 and sizeof(65) which is integer and that's how it shows 4. Not very sure though.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    > printf("%d",sizeof('A'));
    In C, character constants are integers, but in C++, character constants are chars.

    If you compiled this as a C++ program, you would most likely see 8 4 1 as the output.
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2
    Originally Posted by salem
    > printf("%d",sizeof('A'));
    In C, character constants are integers, but in C++, character constants are chars.

    If you compiled this as a C++ program, you would most likely see 8 4 1 as the output.
    Thank you.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    size of a double
    sizeof an int
    sizeof a character constant

    In C a character constant in an int not a char.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Hi,

    you can get value of int but not get value of character.

    Thanks!
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    Originally Posted by miteshaegis
    you can get value of int but not get value of character.
    What does that mean? A character constant is an int, and a char is an integer type so also has a "value".

IMN logo majestic logo threadwatch logo seochat tools logo