Keil Logo


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.10a
  • C251 Version 1.24
  • C251 Version 2.14
  • C251 Version 3.00
  • C51 Version 5.50a
  • C51 Version 6.10a


How can I get the compiler to merge duplicate strings in my program?


The Keil C compilers already do this. For example, the following code:

#include <stdio.h>

void main (void)
printf ("This is a test\n");
printf ("This is a test\n");
printf ("This is a test\n");
printf ("This is a test\n");
printf ("This is a test\n");

Generates the following assembly listing:

             ; FUNCTION main (BEGIN)
                                           ; SOURCE LINE # 5
0000 7BFF              MOV     R3,#0FFH
0002 7A00        R     MOV     R2,#HIGH ?SC_0
0004 7900        R     MOV     R1,#LOW ?SC_0
0006 120000      E     LCALL   _printf
                                           ; SOURCE LINE # 6
0009 7BFF              MOV     R3,#0FFH
000B 7A00        R     MOV     R2,#HIGH ?SC_0
000D 7900        R     MOV     R1,#LOW ?SC_0
000F 120000      E     LCALL   _printf
                                           ; SOURCE LINE # 7
0012 7BFF              MOV     R3,#0FFH
0014 7A00        R     MOV     R2,#HIGH ?SC_0
0016 7900        R     MOV     R1,#LOW ?SC_0
0018 120000      E     LCALL   _printf
                                           ; SOURCE LINE # 8
001B 7BFF              MOV     R3,#0FFH
001D 7A00        R     MOV     R2,#HIGH ?SC_0
001F 7900        R     MOV     R1,#LOW ?SC_0
0021 120000      E     LCALL   _printf
                                           ; SOURCE LINE # 9
0024 7BFF              MOV     R3,#0FFH
0026 7A00        R     MOV     R2,#HIGH ?SC_0
0028 7900        R     MOV     R1,#LOW ?SC_0
002A 020000      E     LJMP    _printf
             ; FUNCTION main (END)

As you can see, the string constant at ?SC_0 is accessed for each call to printf. And, in the module information:

   CODE SIZE        =     45    ----
   CONSTANT SIZE    =     16    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----

only 16 bytes of memory are used for the constant space (the string, "This is a test\n").

As an alternative, you may specifically declare constant strings that you will use in multiple places as follows:

#include <stdio.h>

const char code strThisIsATest [] = "This is a test\n";

void main (void)
printf (strThisIsATest);
printf (strThisIsATest);
printf (strThisIsATest);
printf (strThisIsATest);
printf (strThisIsATest);

Last Reviewed: Monday, March 26, 2001

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.