- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Problem with sprintf in HP aC++/ANSI C B3910B ...
Operating System - Linux
1753936
Members
9956
Online
108811
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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
тАО05-25-2006 01:13 AM
тАО05-25-2006 01:13 AM
Problem with sprintf in HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]
Hello,
I use you compiler:
HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]
We have a problem with sprintf
I have this program:
int main()
{
char var2[21]="";
strcpy(var2,"");
strcpy(var2,"06");
sprintf(var2,"20%s",var2);
printf("%s\n",var2);
}
And the program shows this:
2020
Why? the program must be show:
2006
Please tell me how repair this problem.
Thank you!
I use you compiler:
HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]
We have a problem with sprintf
I have this program:
int main()
{
char var2[21]="";
strcpy(var2,"");
strcpy(var2,"06");
sprintf(var2,"20%s",var2);
printf("%s\n",var2);
}
And the program shows this:
2020
Why? the program must be show:
2006
Please tell me how repair this problem.
Thank you!
- Tags:
- sprintf
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 03:23 AM
тАО05-25-2006 03:23 AM
Re: Problem with sprintf in HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]
You "repair" the compiler by writing better code.
Look at your sprint():
sprintf(var2,"20%s",var2);
sprintf processes the format string from left to write. Note that the 1st thing that is does is copy "20" to var. Next your %s is instantiated using the contents of var2 (which are now "20") so that "2020" is trhe expected output. Please don't bother to explain how you want sprintf to behave; this is a case of that dumb computer (and function) doing exactly what it was told to do.
To convince yourself that I am correct, make this change:
sprintf(var2,"%s20",var2);
The output will be "0620" just as expected.
Note that arrays are passed by reference rather than by value so that there is no copy of var2 passed to sprintf, merely an address -- in this case, the same address twice pushed on the stack.
If it ain't broke, I can fix that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 06:57 AM
тАО05-25-2006 06:57 AM
Re: Problem with sprintf in HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]
Note: A.05.55 is obsolete, the latest is A.06.07, PHSS_34351.
> You "repair" the compiler by writing better code.
As Clay said, this code is illegal.
>sprintf processes the format string from left to right.
This is undefined. The C99 Standard says 7.19.6.5(2)
If copying takes place between objects that overlap, the behavior is undefined.
> You "repair" the compiler by writing better code.
As Clay said, this code is illegal.
>sprintf processes the format string from left to right.
This is undefined. The C99 Standard says 7.19.6.5(2)
If copying takes place between objects that overlap, the behavior is undefined.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP