- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Sprintf Issues
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2010 06:12 PM
02-21-2010 06:12 PM
#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.
Solved! Go to Solution.
- Tags:
- sprintf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2010 08:03 PM
02-21-2010 08:03 PM
SolutionWell, you may know what you're trying to do,
which is more than I can say.
Around here, "#include
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2010 10:29 PM
02-21-2010 10:29 PM
Re: Sprintf Issues
I tried to do too many things in one statment, and I missed the NULL termination of the strcpy guidelines of NULL termination.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2010 10:30 PM
02-21-2010 10:30 PM
Re: Sprintf Issues
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2010 10:52 PM
02-21-2010 10:52 PM
Re: Sprintf Issues
NULL is a pointer (typically, (void *) 0).
NUL is a character ('\0').
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2010 02:19 AM
02-22-2010 02:19 AM