Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C-question about bytes

evindaevinda Posts: 12Member
Hello!!!I have a question...
How many bytes has-> int b[5]={0,0,0,0,0}???

My teacher said it has 18 bytes,although I think that it has 10 bytes!!!
Which is the right answer???
Thank you!!! :)

Comments

  • rgawronrgawron Posts: 1Member
    You can check it empirically with this program:

    [code]#include

    int main () {
    int b[5] = {0,0,0,0,0};
    printf("%ld, %ld
    ", sizeof(b), sizeof(b[0]));
    }
    [/code]

    On my computer it produces..

    [code]bash-3.2$ ./a.out
    20, 4
    [/code]

    ..so in my case, int is stored on 4 bytes, array has 5 elements, so it has size of 4*5=20 bytes.

    AFAIK neither C nor C++ defines size of int, it could be different depending on hardware architecture.
  • lutzhelllutzhell Posts: 6Member
    Yes, the size of int depends on computer architectrure

    E.g.
    Your program outputs "20, 4" on 32 Bit systems and "40, 8" on 64 Bit systems in most cases

    However the only guarantee you have when using a compiler complying to the C99 standard is:

    sizeof(short) <= sizeof(int) <= sizeof(long) < sizeof(long long)

    If you want to be sure that your numbers have the amount of bytes you want them to you can use the following:

    #include <inttypes.h>

    int8_t
    int16_t
    int32_t
    int64_t

    e.g.
    [code]
    #include
    #include

    int main ()
    {
    int32_t b[5] = {0,0,0,0,0};
    printf("%ld, %ld
    ", sizeof(b), sizeof(b[0]));
    }
    [/code]
    will always print:
    [code]
    20, 4
    [/code]
    no matter what machine your code runs on
  • lutzhelllutzhell Posts: 6Member
    Yes, the size of int depends on computer architectrure

    E.g.
    Your program outputs "20, 4" on 32 Bit systems and "40, 8" on 64 Bit systems in most cases

    However the only guarantee you have when using a compiler complying to the C99 standard is:

    sizeof(short) <= sizeof(int) <= sizeof(long) < sizeof(long long)

    If you want to be sure that your numbers have the amount of bytes you want them to you can use the following:

    #include <inttypes.h>

    int8_t
    int16_t
    int32_t
    int64_t

    e.g.
    [code]
    #include
    #include

    int main ()
    {
    int32_t b[5] = {0,0,0,0,0};
    printf("%ld, %ld
    ", sizeof(b), sizeof(b[0]));
    }
    [/code]
    will always print:
    [code]
    20, 4
    [/code]
    no matter what machine your code runs on
  • lutzhelllutzhell Posts: 6Member
    Yes, the size of int depends on computer architectrure

    E.g.
    Your program outputs "20, 4" on 32 Bit systems and "40, 8" on 64 Bit systems in most cases

    However the only guarantee you have when using a compiler complying to the C99 standard is:

    sizeof(short) <= sizeof(int) <= sizeof(long) < sizeof(long long)

    If you want to be sure that your numbers have the amount of bytes you want them to you can use the following:

    #include <inttypes.h>

    int8_t
    int16_t
    int32_t
    int64_t

    e.g.
    [code]
    #include
    #include

    int main ()
    {
    int32_t b[5] = {0,0,0,0,0};
    printf("%ld, %ld
    ", sizeof(b), sizeof(b[0]));
    }
    [/code]
    will always print:
    [code]
    20, 4
    [/code]
    no matter what machine your code runs on
  • lutzhelllutzhell Posts: 6Member
    Yes, the size of int depends on computer architectrure

    E.g.
    Your program outputs "20, 4" on 32 Bit systems and "40, 8" on 64 Bit systems in most cases

    However the only guarantee you have when using a compiler complying to the C99 standard is:

    sizeof(short) <= sizeof(int) <= sizeof(long) < sizeof(long long)

    If you want to be sure that your numbers have the amount of bytes you want them to you can use the following:

    #include <inttypes.h>

    int8_t
    int16_t
    int32_t
    int64_t

    e.g.
    [code]
    #include
    #include

    int main ()
    {
    int32_t b[5] = {0,0,0,0,0};
    printf("%ld, %ld
    ", sizeof(b), sizeof(b[0]));
    }
    [/code]
    will always print:
    [code]
    20, 4
    [/code]
    no matter what machine your code runs on
Sign In or Register to comment.