# C-question about bytes

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!!!

• 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.
• 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
• 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
• 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
• 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