This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

.const section appears without any .const keyword

Dear all,

I am in lots of confusion when I wrote a C program without any keyword: const but when I compiled and linked it, I saw in the map file the presence of .const section. I made a guess that initialized global variables that never changed throughout the program were classified as const, is it right?

Could any of you explain this for me, i would appreciate it much

  • The compiler does not make such variables const.  It's more likely to be string constants and initializers for local variables.  It could also be const data from the library.  Please generate the linker map file with the --map_file linker option and look at the link map for the .const section; this will tell you what files contributed .const to your program.

  • Dear Archaeologist,

    Thank you for your prompt reply. I did as you suggested, generated a map file and check what contributed .const to my program, that pointed to a xxx.c file

    Strangely, I cant find any suspicious things like global/static const variable, string constant in that xxx.c file. I only saw some look-up table values declared as static variable that stay unchanged,

    I am look forward to hearing from you,

  • I assume this static variable is initialized in the C code.  For a static, non-const, initialized variable, the compiler stores its initial value in the .const section.  If the variable truly does not change during the course of the program, you should declare it "const" and you will save a bit of memory.

  • Dear  Archaeologist,

    Finally I found my program error when controlling debug process using printf with some string constants that all were put in  macros kind of #if #endif. I am so sorry for taking your time. However, through your posts, I have  strengthened more my programming skills

    I would like to thank you again for your support,

    Nguyen Anh Duc