1char myConcatenation[80];
2 char myCharArray[16]="A variable name";
3 int myInt=5;
4 sprintf(myConcatenation,"%s = %i",myCharArray,myInt);
5 Serial.println(myConcatenation);
1
2There are already some comments on using sprintf to force leading leading zeros but the examples only include integers. I needed leading zeros on floating point numbers and was surprised that it didn't work as expected.
3
4Example:
5<?php
6sprintf('%02d', 1);
7?>
8
9This will result in 01. However, trying the same for a float with precision doesn't work:
10
11<?php
12sprintf('%02.2f', 1);
13?>
14
15Yields 1.00.
16
17This threw me a little off. To get the desired result, one needs to add the precision (2) and the length of the decimal seperator "." (1). So the correct pattern would be
18
19<?php
20sprintf('%05.2f', 1);
21?>
22
23Output: 01.00
24
25Please see http://stackoverflow.com/a/28739819/413531 for a more detailed explanation.
26
1<?php
2$num = 5;
3$location = 'tree';
4
5$format = 'There are %d monkeys in the %s';
6echo sprintf($format, $num, $location);
1int sprintf(char *str, const char *format, ...)
2
3Parameters:
4
5str − This is the pointer to an array of char elements where the resulting C string is stored.
6
7format − This is the String that contains the text to be written to buffer. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. Format tags prototype: %[flags][width][.precision][length]specifier
8
9EXAMPLE:
10
11#include <stdio.h>
12#include <math.h>
13
14int main () {
15 char str[80];
16
17 sprintf(str, "Value of Pi = %f", M_PI);
18 puts(str);
19
20 return(0);
21}
22
23OUTPUT:
24
25Value of Pi = 3.141593