Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sprintf issue

Status
Not open for further replies.

DMMIG

Programmer
Jan 6, 2005
13
0
0
US
Can some body please help me here. I am just lost.

#include <stdio.h>
#include<stdlib.h>
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.

Thanks in advance.
 
Missed a point, this code is executed On HP-UX B.11.23 U ia64
 
Alas, this is a wrong code.

The mr array is not initialized, its contents is not defined. After the first atoi(strncpy(...)) leading chars of mr are '1' and '0' but others are not defined. So both printf prints undefined result (2010 in your case).

Now after uncommented sprintf mr contains "2010", after the second strncpy it contains "1010". Well, 1010 + 2000 == 3010...

That's all, folks...
 
ArkM,Thanks for replying.

1) If your reasoning is correct why does adding just sprintf change every thing.
It becomes more strange

after this.
#include <stdio.h>
#include<stdlib.h>
main (){
char mr[18],wmr[18];int imr = 0;char pcs[] = "H5146B2";char pi[]="210023610512918234";

strcpy(mr,"111111111111111111");
strcpy(mr,"");
printf("Id = %s, pcs = %s and mr=%s\n",pi,pcs,mr);
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 and mr=
Appende = 101111111111111111 and i value = 101111111111111111 and imr = 345995671
Appendf = 105995671 and i value = 10512918234 and imr = 105997671
Append2 = 105995671 and i value = 105997671

 
With Visual Studio 2008 your code fails whith this message :
"Run time check Failure #2 : Stack around the variable 'mr' was corrupted."

When you write strcpy(mr, "111111111111111111"); you copy all the chars 1 + the 0 ending the string so you write 19 chars and you corrupt the environment.
Sometimes that do nothing, sometimes that produce an error, enjoy C ! Visual Studio seems to be very strict about that.
 
Joel/ArkM, Thank you for the pointers. I just found the solution. Sorry for being so dumb, but I forgot the fundamentals of C. I did not NUL terminate the string after the strcpy call and if you don't C fills the memry with garbage.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top