I'm using sprintf
to print int
variable which should display 1^2+2^2+3^2+4^2+5^2+6^2
. But my code only prints 1^2+6^2
. I don't know why the middle part is missing.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* formatSeries(int n)
{
char *tagstr = (char *)malloc(sizeof(char)*n*n);
int pos = 0 ;
int k;
for (k = 1; k <= n; k++)
{
pos = sprintf(&tagstr[pos], "%d^2+", k);
}
tagstr[strlen(tagstr) - 1] = '\0';
return tagstr;
}
void main()
{
int n = 6;
printf("%s \n", formatSeries(n));
}
There are two main issues in your code; the one - as already mentioned in the comments - is that you do not increase pos
but reassign it again and again with (almost) the same value. Hence, after the first iteration, you will constantly write to a position at 4
(or 5
if k
would become >=10 and the numbers would thereby get more digits),
Second, your malloc
will not work for small k
s, since each string like "+1^2"
takes at least 4 characters; so if you size the buffer with n * n
, then the buffer will be too small if n
is <= 3, and (probably much) too big if n
becomes larger.
I'd suggest to assume a maximum range of n
(let's say up to 7 digits) and dimension the buffer accordingly.
A minor thing (and not essential) is that you probably could avoid writing superfluous +
at all in that you use a different format string for the first iteration; just do show the idea, really not essential:
char* formatSeries(int n)
{
const size_t maxSize = n * (7+3) + 1;
char *tagstr = (char *)malloc(sizeof(char)*maxSize);
int pos = 0 ;
int k;
for (k = 1; k <= n; k++)
{
const char* formatStr = (k==1) ? "%d^2" : "+%d^2";
pos += sprintf(&tagstr[pos], formatStr, k);
}
return tagstr;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.