SPRINTF IN ARDUINO

 


In this tutorial we are going to use sprintf in Arduino

The use of sprintf is used to change any data type into string or character array

FORMAT

int a=5;

char arr[10]={};

sprintf(char_arr,%d,a);

Here the char arr is used to store the converted data and %d is the data type which is going to be converted and the variable name.

CODE

char arr[10] = {};
unsigned long int bank[2] = {123, 4542};
char name[10 + 1] = {"abcd"};
void setup() {
  Serial.begin(9600);
}

void loop() {
  sprintf(arr, "%d", bank[0]);
  Serial.println(arr);
  sprintf(arr,"%d", bank[1]);
  Serial.println(arr);
  sprintf(arr, "%s", name);
  Serial.println(arr);
  while (1);
}

CODE EXPLANATION

In the above code,integer array having two rows each row is converted and it is stored in char arr.

The name array should be maximum character size + 1 which is' \0' null character must be included.

If null char space is not included then the  char arr cannot be terminated.

Comments