473,544 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sprintf

Hi All,

I am using sprintf and getting starnge output in following case

char temp_rn[12];
memset(temp_rn, '\0',12);
sprintf(temp_rn ,"0%s",temp_rn) ;

the final value in temp_rn is 00
how it is behaving like this, when source string and output string is
same.

Jan 16 '07
66 3055
yeti wrote:
Keith Thompson wrote:
"yeti" <ro********@gma il.comwrites:
Nelu wrote:
C99 7.19.6.6-2: "... If copying takes place between objects that
overlap, the behavior is undefined."
>
Yes I agree that standard says that the behaviour is undefined but
leaving it at that won't answer the question.
There still should be an explination for the behaviour. Computers as
yet don't have free will ;-)
There is no C answer. The behavior is undefined; the reason for any
particular behavior is going to depend on the implementation, and
possibly on the phase of the moon.

Yes, there's likely to be a specific reason for whatever behavior you
see on a specific system under specific circumstances -- but we can't
guess what that reason might be. The meta-answer: don't worry about
it, just fix the code.

Ah I see. Perhaps then universal answer to all the questions would be
"Refer to C standard"
No. Just to questions regarding standard C.

Jan 17 '07 #11

santosh wrote:
yeti wrote:
Keith Thompson wrote:
"yeti" <ro********@gma il.comwrites:
Nelu wrote:
C99 7.19.6.6-2: "... If copying takes place between objects that
overlap, the behavior is undefined."

Yes I agree that standard says that the behaviour is undefined but
leaving it at that won't answer the question.
There still should be an explination for the behaviour. Computers as
yet don't have free will ;-)
>
There is no C answer. The behavior is undefined; the reason for any
particular behavior is going to depend on the implementation, and
possibly on the phase of the moon.
>
Yes, there's likely to be a specific reason for whatever behavior you
see on a specific system under specific circumstances -- but we can't
guess what that reason might be. The meta-answer: don't worry about
it, just fix the code.
Ah I see. Perhaps then universal answer to all the questions would be
"Refer to C standard"

No. Just to questions regarding standard C.
We are talking about question where standard says "I don't know"

Jan 17 '07 #12

yeti wrote:
Nelu wrote:
[...attribution lost...]]
sprintf(temp_rn ,"0%s",temp_rn) ;
>
the final value in temp_rn is 00
how it is behaving like this, when source string and output string is
same.
>
C99 7.19.6.6-2: "... If copying takes place between objects that
overlap, the behavior is undefined."
[...]
Yes I agree that standard says that the behaviour is undefined but
leaving it at that won't answer the question.
The answer is "Because that's the way that particular implementation
does it."
There still should be an explination for the behaviour. Computers as
yet don't have free will ;-)
The standard doesn't preclude that. Computers are even allowed to have
magical powers, and make demons fly from your nose. I'm not aware of
any that actually do that, however.

Regards,

-=Dave

Jan 17 '07 #13
yeti <ro********@gma il.comwrote:
>
Nelu wrote:
>gyan <gy********@tcs .comwrote:
Hi All,

I am using sprintf and getting starnge output in following case

char temp_rn[12];
memset(temp_rn, '\0',12);
sprintf(temp_rn ,"0%s",temp_rn) ;

the final value in temp_rn is 00
how it is behaving like this, when source string and output string is
same.

C99 7.19.6.6-2: "... If copying takes place between objects that
overlap, the behavior is undefined."
<snip signature>
Yes I agree that standard says that the behaviour is undefined but
leaving it at that won't answer the question.
That is the answer to the question. If it said that the behavior was
implementation defined then you could look for an explanation in the
implementation' s documentation. In this case it's undefined.
There still should be an explination for the behaviour. Computers as
yet don't have free will ;-)
How do you know? In the worst case scenario, for undefined behavior, they borrow it from the programmer :-).
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 17 '07 #14

Dave Hansen wrote:
yeti wrote:
Nelu wrote:
[...attribution lost...]]
sprintf(temp_rn ,"0%s",temp_rn) ;

the final value in temp_rn is 00
how it is behaving like this, when source string and output string is
same.

>
C99 7.19.6.6-2: "... If copying takes place between objects that
overlap, the behavior is undefined."
[...]
Yes I agree that standard says that the behaviour is undefined but
leaving it at that won't answer the question.

The answer is "Because that's the way that particular implementation
does it."
There still should be an explination for the behaviour. Computers as
yet don't have free will ;-)

The standard doesn't preclude that. Computers are even allowed to have
magical powers, and make demons fly from your nose. I'm not aware of
any that actually do that, however.
Sorry, in fact standard requires computers to have deterministic
behaviour. There would be no standard if if demons flew from my nose.
All lines in the standard would shrink to "How am I supposed to know.Go
ask your computer"
>
Regards,

-=Dave
Jan 17 '07 #15
In article <11************ **********@v45g 2000cwv.googleg roups.com>,
yeti <ro********@gma il.comwrote:
....
>Sorry, in fact standard requires computers to have deterministic
behaviour. There would be no standard if if demons flew from my nose.
All lines in the standard would shrink to "How am I supposed to know.Go
ask your computer"
The dogmatic position is that the behaviour is only required to be
deterministic in those areas covered by the standard. For anything
outside the standard (i.e., the various "XXX defined" categories; e.g.,
"un"defined ), the behaviour can, indeed, be random (or seemingly
determined by "free will").

Jan 17 '07 #16
In article <11************ **********@v45g 2000cwv.googleg roups.com>,
yeti <ro********@gma il.comwrote:
>Dave Hansen wrote:
>The standard doesn't preclude that. Computers are even allowed to have
magical powers, and make demons fly from your nose. I'm not aware of
any that actually do that, however.
>Sorry, in fact standard requires computers to have deterministic
behaviour. There would be no standard if if demons flew from my nose.
Within your own logic: if for a particular code situation, demons
-always- flew from your nose, then that would be deterministic,
and thus within the bounds of what you claim about the Standard.

But your claim that the Standard requires computers to have
deterministic behaviour is doubtful. The ISO C89 Rationale
(not normative) indicates,

Undefined behavior gives the implementor license to not catch
certain program errors that are difficult to diagnose. It also
identifies areas of possible conforming language extension:
the implementor may augment the language by providing a definition
of the officially undefined behavior.

Thus, for any particular "undefined behavior", the implementor could
choose to define the behavior as being non-deterministic, and that
would be within the scope of allowed designs.

Also, if the program does something like overwrites the end of
a local variable, and so doing bashes control information being
used by the implementation, then flow of control could end up
directed to any point in memory, through any -possible- machine
instruction sequence. That instruction sequence could include
invoking a machine-level instruction that was defined
non-deterministical ly, or even a machine-level instruction that
was not well defined and whose operation turned out to depend upon
random transient voltages. Similar things can happen if function
pointers are abused -- remember the old trick of taking an
array of integers and casting that to a function pointer and invoking
that. If you believe that the Standard actively disallows the
implementation from ever branching into arbitrary code, then we
will be wanting Chapter and Verse (C&V) of the sections of the
Standard that you believe nails down the behaviour.

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jan 17 '07 #17
"yeti" <ro********@gma il.comwrites:
santosh wrote:
yeti wrote:
Keith Thompson wrote:
"yeti" <ro********@gma il.comwrites:
Nelu wrote:
C99 7.19.6.6-2: "... If copying takes place between objects that
overlap, the behavior is undefined."
>
Yes I agree that standard says that the behaviour is undefined but
leaving it at that won't answer the question.
There still should be an explination for the behaviour. Computers as
yet don't have free will ;-)

There is no C answer. The behavior is undefined; the reason for any
particular behavior is going to depend on the implementation, and
possibly on the phase of the moon.

Yes, there's likely to be a specific reason for whatever behavior you
see on a specific system under specific circumstances -- but we can't
guess what that reason might be. The meta-answer: don't worry about
it, just fix the code.
>
Ah I see. Perhaps then universal answer to all the questions would be
"Refer to C standard"
No. Just to questions regarding standard C.
We are talking about question where standard says "I don't know"
And in that case, the correct answer is "I don't know".

What answer other than that did you have in mind?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 17 '07 #18
Christopher Benson-Manica <at***@ukato.fr eeshell.orgwrot e:
No. Annex J.2 of n869 describes undefined behavior, which includes
"The snprintf, sprintf, sscanf, vsnprintf, vsprintf, mbstowcs,
wcstombs, memcpy, strcpy, strncpy, strcat, strncat, strxfrm, or
strftime function, or any of the functions declared by <wchar.h>
(except where otherwise specified), is used to copy between
overlapping objects."
I should correct myself slightly - Annex J.2 is informative, and
Richard Heathfield's quotation of 4.9.6.5 (which I presume to be
normative) is therefore preferable.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Jan 17 '07 #19
Christopher Benson-Manica <at***@norge.fr eeshell.orgwrit es:
Christopher Benson-Manica <at***@ukato.fr eeshell.orgwrot e:
No. Annex J.2 of n869 describes undefined behavior, which includes
"The snprintf, sprintf, sscanf, vsnprintf, vsprintf, mbstowcs,
wcstombs, memcpy, strcpy, strncpy, strcat, strncat, strxfrm, or
strftime function, or any of the functions declared by <wchar.h>
(except where otherwise specified), is used to copy between
overlapping objects."

I should correct myself slightly - Annex J.2 is informative, and
Richard Heathfield's quotation of 4.9.6.5 (which I presume to be
normative) is therefore preferable.
That would be 4.9.6.5 in the ANSI C89 standard, which corresponds to
7.9.6.5 in the ISO C90 standard, and 7.19.6.6 in the C99 standard.

I've never even seen a copy of the 1989 ANSI C standard; it was
superseded a year later, when ANSI adopted the ISO version of the
standard. In general, sections 4 and 5 of C89 correspond to sections
6 and 7, respectively, of the C90 standard. C99 has the same section
numbers, but subsections differ.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 17 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4991
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution string. I all works fine, even the sprintf, when I write numbers. But if I want to write a character string, it all goes wrong. I am trying to print...
3
42141
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf, "0x%02x", n); The above code worked. Howeve, what I am trying to do is to convert an
6
2486
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make the character array(alerts.msg) with sprintf doesn't work for the obvious reasons in my first sentence with tmp having those control characters. Is...
1
3513
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some others are asked below: 1. #include <stdio.h> int main(){ char buffer;
2
4709
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
9
3059
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for "stack and heap space". So I'm running in a very memory constricted environment. The program "speaks" a proprietary protocol which sends ASCII...
12
4588
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ??? sprintf(&chData, "%+05.0f", -0.380f); --I get "-000" ??? sprintf(&chData, "%+05.0f", -3.800f); --I get "-0004" ok
15
3507
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it PrintOnConsole(), to get the output on a console. The problem with PrintOnConsole() is that it only takes strings as input arguments. On the other...
5
3457
by: Dave | last post by:
Hi, In awk I can do this: var1="x"; temp = sprintf("Variable 1: %s Variable 2: %%s", var1); # now the value of temp is "Variable 1: x Variable 2: %s" var2="y"; printf(temp,var2);
3
2605
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2", str); //Interesting stuff happens here printf(str2);
0
7413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7597
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
650
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.