1829596 Members
2253 Online
109992 Solutions
New Discussion

Sprintf Issues

 
SOLVED
Go to solution
Mallik S Pullela
New Member

Sprintf Issues

Can some body please help me here. I am just lost.

#include
#include
main (){
char mr[18],wmr[18];int imr = 0;char pcs[] = "H5146B2";char pi[]="210023610512918234";
printf("Id = %s, pcs = %s \n",pi,pcs);

imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;
printf("Appende = %s and i value = %s and imr = %d\n",mr,mr,imr);

/*sprintf(mr,"%d",imr);*/

imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;
printf("Appendf = %s and i value = %s and imr = %d\n",mr,pi+7,imr);

imr=atoi((char *)(strncpy((char *)(mr),pi+7,2)))+2000;
printf("Append2 = %s and i value = %d\n",mr,imr);
}

Output :
Id = 210023610512918234, pcs = H5146B2
Appende = 10 and i value = 10 and imr = 2010
Appendf = 10 and i value = 10512918234 and imr = 2010
Append2 = 10 and i value = 2010

As soon as I uncomment sprintf

Output :
Id = 210023610512918234, pcs = H5146B2
Appende = 10 and i value = 10 and imr = 2010
Appendf = 1010 and i value = 10512918234 and imr = 3010
Append2 = 1010 and i value = 3010

I was expecting the value of imr to be 2010 even at 2nd Printf command.

Thanks in advance.
5 REPLIES 5
Steven Schweda
Honored Contributor
Solution

Re: Sprintf Issues

> [...] I am just lost.

Well, you may know what you're trying to do,
which is more than I can say.

Around here, "#include " would be
nice, to get strncpy() declared.

Then, in:

strncpy((char *)(mr),(char *)pi+7,2)

why all the type casting? And who
NUL-terminates mr[] after you copy in the two
characters?

man strncpy

> /*sprintf(mr,"%d",imr);*/

If "imr" is 2010, then this would tend to set
mr[] to "2010" (and now it _is_
NUL-terminated). Replacing its first two
characters with "10" (from "pi+7") would seem
to leave "1010", and around here,
1010 + 2000 = 3010.

> I was expecting [...]

Why?

Note that on my convenient VMS system, I get
garbage until someone NUL-terminates "mr":

alp $ type spf2.c
#include
#include
#include

main (){
char mr[18],wmr[18];int imr = 0;char pcs[] = "H5146B2";char
pi[]="210023610512918234";
printf("Id = %s, pcs = %s \n",pi,pcs);

imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;
printf("Appende = %s and i value = %s and imr = %d\n",mr,mr,imr);

sprintf(mr,"%d",imr);

imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;
printf("Appendf = %s and i value = %s and imr = %d\n",mr,pi+7,imr);

imr=atoi((char *)(strncpy((char *)(mr),pi+7,2)))+2000;
printf("Append2 = %s and i value = %d\n",mr,imr);
}

alp $ cc spf2
alp $ link spf2
alp $ run spf2
Id = 210023610512918234, pcs = H5146B2
Appende = 10ü and i value = 10ü and imr = 2010
Appendf = 1010 and i value = 10512918234 and imr = 3010
Append2 = 1010 and i value = 3010

You may have luckier garbage than I have, but
using strncpy() to copy two characters, and
then not putting in a NUL ('\0') seems to me
to be rather risky.

If you tried doing less in one statement, and
looked at what you were feeding into atoi(),
then you might get some clues as to why your
code does what it does.
Mallik S Pullela
New Member

Re: Sprintf Issues

Steve, Thank you for pointing it out.

I tried to do too many things in one statment, and I missed the NULL termination of the strcpy guidelines of NULL termination.
Mallik S Pullela
New Member

Re: Sprintf Issues

Steve's answer was correct.
Steven Schweda
Honored Contributor

Re: Sprintf Issues

> [...] NULL termination [...]

NULL is a pointer (typically, (void *) 0).
NUL is a character ('\0').
Dennis Handly
Acclaimed Contributor

Re: Sprintf Issues

If you don't want to put a NUL on the end, you could use sscanf(3) and use %2d.