On Thu, 19 Oct 2000, Lawrence Lile wrote: > I am trying to printf a series of strings in a program in CCS C on a > PIC16C715 (my circuit prints to a serial LCD) something like this: > > Switch (state){ > Case 0: > Printf("Pancakes"); > Break(); > Case 1: > Printf("Eggs"); > break(); > Case 3: > Printf("Bacon"); > break(); > Case 4: > Printf ("Biscuits"); > Break; > etc. etc. etc. > } > > (actually it is about a dozen cases - you get the idea.) Yeah, that will take a LOT of space. I just finished a project with a lot of string handling, and it's a beastly size. > The entire Switch Case Printf routine takes up a whopping 700 bytes in my > program. I thought it might be less memory intensive to do something like > this, but I can't figure out the correct syntax: > > char Stuff[16]; > > Switch (state){ > Case 0: > Stuff ="Pancakes"; // this is not the proper syntax - won't > compile > Break(); > Case 1: > Stuff="Eggs"; > break(); > Case 3: > Stuff = "Bacon"; > break(); > Case 4: > Stuff = "Biscuits"; > Break; > } > printf("%C", stuff); // this is not the proper syntax - won't compile > > > > You get the idea - set up a variable with the proper text string, then call > ONE printf statement at the end. What is the correct way to achieve this > result? > > This is actually two questions: > > 1. How do you set an array to a string? Switch (state){ Case 0: strcpy(Stuff,"Pancakes"); Break(); Case 1: strcpy(Stuff,"Eggs"); break(); Case 3: strcpy(Stuff,"Bacon"); break(); Case 4: strcpy(Stuff,"Biscuits"); Break; } puts(stuff); You can do puts(Stuff) or use printf(Stuff). puts() is samller in this case; sometimes it's not. It will put a CR/LF after the output though. By the way -- in my quick test, strcpy() in the switch/case bit will save you 2 bytes of code space per case over printf(). Oddly, though, if I used all puts() calls it was larger than using printf()! Go figure. A lot of the time I find I can do something one of several ways; I usually try all of them and see which one the compiler likes best, as evidenced by the smallest code. Yeah, I know, that may not be fastest; sue me, I usually don't care. Dale --- The most exciting phrase to hear in science, the one that heralds new discoveries, is not "Eureka!" (I found it!) but "That's funny ..." -- Isaac Asimov -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics