user8769: I'm trying to make my sketch smaller. Right now I use an array for the AM/PM part of my time display. My thinking is this makes it easy to change the formatting:
char* ampms[]= {"am", "pm"};

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; ampm=1; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }

      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      dataFile.print(ampms[ampm]);          
}

I could do this instead:
void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }

      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      if (hour>12) { lcd.print("am");} else {lcd.print("pm"); }        
}

No more am/pm array. But, I have to add an if... is one better than the other for memory? Why?

user3917: As Ignacio says, if the compiled size is the same both ways, look for something else.  (But note that having the constants in flash memory (program memory) may still use about the same amount of RAM, and typically will take more program space.)
When I compile the code you showed (together with minor additions like declaration of variables) I show the array method using 4 bytes less code and 5 bytes more RAM than the if method, so there are tradeoffs.
You can replace the clunky if statement and its two lcd.prints  with the following:
      lcd.print(hour>12? "am" : "pm");

Note that that line retains the two bugs that your if statement has, but it produces them more compactly. :) [One error is that your if statement prints pm for hours 0 through 12 and am for hours 13 through 23.  Another error is that it prints zeroes instead of twelves for the hours after midnight and noon.  More properly, a program should print like 12:xx am during hour 0 of the day, and like 12:xx pm, 1:xx pm, ... 11:xx pm during hours 12 through 23.]
Also note that as Gerben mentions, moving the "am" and "pm" strings into program memory via
lcd.print(hour>12? F("am") : F("pm"));

saves 6 bytes of RAM.  However, that improvement in RAM usage is offset by 74 additional bytes of program space! The following saves 6 bytes of RAM while increasing program size by only 12 bytes so may be preferable:
lcd.print(hour>12? 'p' : 'a');
lcd.print('m');

The following lines of C can replace much of the code you showed.  However, the printf library uses about 1300 bytes of program space, so the following compact-source-code approach cannot be recommended unless you are already loading some printf functions.  It replaces the lines from if (hour>12) { hr = hour - 12; } ... to the end with
enum { bufsize=8 };
char buf[bufsize];
snprintf (buf, bufsize, "%02:02%cm", hour%12, minute, hour>12? 'p' : 'a');
lcd.print(buf);


8769: <p>I'm trying to make my sketch smaller. Right now I use an array for the AM/PM part of my time display. My thinking is this makes it easy to change the formatting:</p>

<pre class="lang-c prettyprint-override"><code>char* ampms[]= {"am", "pm"};

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&amp;second, &amp;minute, &amp;hour); 

      //change to 12 hour format.
      if (hour&gt;12) { hr = hour - 12; ampm=1; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr&lt;10) { lcd.print("0"); }

      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute&lt;10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      dataFile.print(ampms[ampm]);          
}
</code></pre>

<p>I could do this instead:</p>

<pre class="lang-c prettyprint-override"><code>void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&amp;second, &amp;minute, &amp;hour); 

      //change to 12 hour format.
      if (hour&gt;12) { hr = hour - 12; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr&lt;10) { lcd.print("0"); }

      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute&lt;10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      if (hour&gt;12) { lcd.print("am");} else {lcd.print("pm"); }        
}
</code></pre>

<p>No more am/pm array. But, I have to add an if... is one better than the other for memory? Why?</p>
 3917: <p>As Ignacio says, if the compiled size is the same both ways, look for something else.  (But note that having the constants in flash memory (program memory) may still use about the same amount of RAM, and typically will take more program space.)</p>

<p>When I compile the code you showed (together with minor additions like declaration of variables) I show the array method using 4 bytes less code and 5 bytes more RAM than the <code>if</code> method, so there are tradeoffs.</p>

<p>You can replace the clunky <code>if</code> statement and its two <code>lcd.print</code>s  with the following:</p>

<pre><code>      lcd.print(hour&gt;12? "am" : "pm");
</code></pre>

<p>Note that that line retains the two bugs that your <code>if</code> statement has, but it produces them more compactly. :) [One error is that your <code>if</code> statement prints <code>pm</code> for hours 0 through 12 and <code>am</code> for hours 13 through 23.  Another error is that it prints zeroes instead of twelves for the hours after midnight and noon.  More properly, a program should print like 12:xx am during hour 0 of the day, and like 12:xx pm, 1:xx pm, ... 11:xx pm during hours 12 through 23.]</p>

<p>Also note that as Gerben mentions, moving the "am" and "pm" strings into program memory via</p>

<pre><code>lcd.print(hour&gt;12? F("am") : F("pm"));
</code></pre>

<p>saves 6 bytes of RAM.  However, that improvement in RAM usage is offset by 74 additional bytes of program space! The following saves 6 bytes of RAM while increasing program size by only 12 bytes so may be preferable:</p>

<pre><code>lcd.print(hour&gt;12? 'p' : 'a');
lcd.print('m');
</code></pre>

<p>The following lines of C can replace much of the code you showed.  However, the <code>printf</code> library uses about 1300 bytes of program space, so the following compact-source-code approach cannot be recommended unless you are already loading some <code>printf</code> functions.  It replaces the lines from <code>if (hour&gt;12) { hr = hour - 12; } ...</code> to the end with</p>

<pre><code>enum { bufsize=8 };
char buf[bufsize];
snprintf (buf, bufsize, "%02:02%cm", hour%12, minute, hour&gt;12? 'p' : 'a');
lcd.print(buf);
</code></pre>
