#include <stdio.h>
const int str[1000] = {0};
int main(void)
{
printf("arr is %d\n", str[0]);
return 0;
}
Has the following output:
[-exercises/adam/stack2]:size a.out
text data bss dec hex filename
5133 272 24 5429 1535 a.out
Whereas:
#include <stdio.h>
static int str[1000] = {0};
int main(void)
{
printf("arr is %d\n", str[0]);
return 0;
}
Has the following output:
[-exercises/adam/stack2]:size a.out
text data bss dec hex filename
1080 4292 24 5396 1514 a.out
When the array is uninitialized — it again goes to text segment for “const” and to BSS for “static”.
The variable is global and should be accessible from anywhere in the executable it is part of (because of no “static”), but given its a variable I don’t know why it is placed in text segment instead of data segment?
You’re confused. There is no dichotomy between
constandstatic; the two are independent. Assuming all data is initialized, bothstatic constand external (global)constwill go intextand both non-const-qualifiedstaticand non-const-qualified external will go indata.As for
bss, modern binary formats like ELF actually have separatebssfor constant and nonconstant zero data. The output of thesizecommand just doesn’t show it.