Frequently Asked Questions

How do I do a printf to a string?

This section is now obsolete: The CCS C compiler now supports sprintf(), a printf() function that formats the string as desired but stores the result into a string. See the CCS manual for documentation of sprintf().

The following example shows how to printf to a string for users who do not have sprintf() or would still like to see the old method.

The following is an example of how to direct the output of a printf to a string. We used the \f to indicate the start of the string.

This example shows how to put a floating point number in a string.

char string[20]; 
byte stringptr=0;
	
tostring(char c) { 
   if(c=='\f') 
      stringptr=0; 
   else
      string[stringptr]=0; 
   
   string[stringptr++]=c;
}

main() { 
   float f;
   
   f=12.345;
   
   printf(tostring,"\f%6.3f",f);
}

C-Aware IDE Demo
Embedded C Learners Kit
C Workshop Compiler