473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sequence point doubt

Hello,

Is the following code valid:

char buf[1024];
int size = 666;
size += sprintf(buf+size, "%d", size);
size += sprintf(buf+size, "%d", size);

(Expected behavior: at offset 666, buf contains "666669\0".)

Is it equivalent to the following code:

char buf[1024];
int size = 666;
int temp = sprintf(buf+size, "%d", size);
size += temp;
int temp = sprintf(buf+size, "%d", size);
size += temp;

AFAIU, the function call is a sequence point, therefore size can only be
updated after sprintf has been evaluated with the "old" value of size.

This reasoning might be incorrect if sprintf were a macro, right?
(Can it be?)

Regards.
Nov 19 '07 #1
2 1274
In article <47**********************@news.free.fr>,
Spoon <root@localhostwrote:
>Hello,

Is the following code valid:

char buf[1024];
int size = 666;
size += sprintf(buf+size, "%d", size);
size += sprintf(buf+size, "%d", size);

(Expected behavior: at offset 666, buf contains "666669\0".)

Is it equivalent to the following code:

char buf[1024];
int size = 666;
int temp = sprintf(buf+size, "%d", size);
size += temp;
int temp = sprintf(buf+size, "%d", size);
size += temp;

AFAIU, the function call is a sequence point, therefore size can only be
updated after sprintf has been evaluated with the "old" value of size.
That's correct, though the reason I'd give is that both of the places
where size is evaluated in the sprintf call are used to determine the
value that is eventually stored into size (they're needed for the call
to sprintf, and the return from sprintf is added to the old value of
size).

>This reasoning might be incorrect if sprintf were a macro, right?
(Can it be?)
Any standard library function is allowed to be a macro, but in most of
them are required to act enough like functions that you don't usually
need to care about the difference.

The only thing I can think of is that the return value doesn't depend
on the buffer you ask sprintf to print into, but I can't come up with a
reasonable macro implementation that would break this. So I'll go
ahead and claim that this is well-defined in every possible case,
secure in the knowledge that the language lawyers who have finished
their morning coffee will give several examples that will prove me
wrong.
dave

Nov 19 '07 #2
On 19 Nov, 14:25, Spoon <root@localhostwrote:
Hello,

Is the following code valid:

char buf[1024];
int size = 666;
size += sprintf(buf+size, "%d", size);
size += sprintf(buf+size, "%d", size);

(Expected behavior: at offset 666, buf contains "666669\0".)
Yes , it's valid. I would expect the same behaviour.
Is it equivalent to the following code:

char buf[1024];
int size = 666;
int temp = sprintf(buf+size, "%d", size);
size += temp;
int temp = sprintf(buf+size, "%d", size);
You are redefining temp.
size += temp;
Apart from the mistake I pointed out they are equivalent.
AFAIU, the function call is a sequence point, therefore size can only be
updated after sprintf has been evaluated with the "old" value of size.
You have a sequence point just after the arguments to sprintf()
have been evaluated and before the actual call and another
sequence point just before sprintf() returns.
Nov 19 '07 #3

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

Similar topics

7
by: Chinook | last post by:
OO approach to decision sequence? --------------------------------- In a recent thread (Cause for using objects?), Chris Smith replied with (in part): > If your table of photo data has...
3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
3
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema...
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
2
by: Divick | last post by:
Hi, I have written a function which converts from ARGB1555 to RGBA5551 , i.e. changes the position of the msb to lsb by shifting the other bits. The function is shown below, but I have doubt about...
5
by: vlsidesign | last post by:
The printf function returns "warning: unknown escape sequence: \040" for a backslash-space combination. If the ascii decimal number for space is 32 and the backslash is 92, why this particular...
3
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a...
21
by: bilgekhan | last post by:
After doing a succcessful insert() or find() on a set<Tcontainer is it possible to get the item number of this item in the set? (ie. its zero-based sequence number (position/location/rank/index)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.