c++ char type string format code example

Example 1: C printf to string

// int sprintf(char *str, const char *format, ...);
char *printf_to_string()
{
  char buffer[128] = {0} //Must be big enough

  // writing printf output in buffer
  sprintf(buffer, "some %s here...\n", "text");
  // duplicate if needed
  return strdup(buffer);
}

Example 2: sprintf

char myConcatenation[80];
    char myCharArray[16]="A variable name";
    int myInt=5;
    sprintf(myConcatenation,"%s = %i",myCharArray,myInt);
    Serial.println(myConcatenation);

Tags:

Java Example