473,396 Members | 1,812 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,396 software developers and data experts.

sscanf behavoir

Hi folks

What would be the expected and correct output of the following

#include <stdio.h>

int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);

sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);

return 0;
}

On one platform I get:
(10,20,30) -10
(10,20,30 ) -12

on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.

Which is correct?
Bye, Jojo
Apr 19 '07 #1
7 2273
On 19 Apr, 07:58, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
Hi folks

What would be the expected and correct output of the following

#include <stdio.h>

int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);

sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);

return 0;

}

On one platform I get:
(10,20,30) -10
(10,20,30 ) -12

on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.

Which is correct?
sscanf returns an integer which is the count of values assigned. Your
program would be improved by showing this information, in my opinion.

On my gcc/glibc system I get your second result and in both cases 3
values are reported as assigned. The same result is obtained on AIX,
using the IBM xlc compiler and IBM's C library implementation. This is
the result I'd expect.
Apr 19 '07 #2
<ma**********@pobox.comschrieb im Newsbeitrag
news:11*********************@b58g2000hsg.googlegro ups.com...
On 19 Apr, 07:58, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
>Hi folks

What would be the expected and correct output of the following

#include <stdio.h>

int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);

sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);

return 0;

}

On one platform I get:
(10,20,30) -10
(10,20,30 ) -12

on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.

Which is correct?

sscanf returns an integer which is the count of values assigned. Your
program would be improved by showing this information, in my opinion.

On my gcc/glibc system I get your second result and in both cases 3
values are reported as assigned. The same result is obtained on AIX,
using the IBM xlc compiler and IBM's C library implementation. This is
the result I'd expect.
I did try that too and sscanf returns 3 on all cases. What I'm concerned
about thoug is the %n conversion that does not seem to work properly. So
sscanf did process at least 9 of it's input characters "(10,20,30" as the
corresponding variables are filled, but regardless %n doesn't show that.
From the sscanf man-page:

n Consumes no input. The corresponding pointer
parameter is a pointer to an integer into
which is written the number of characters
read from the input string so far by this
function. The assignment count returned at
the completion of this function is not
incremented.
Bye, Jojo
Apr 19 '07 #3
On Apr 19, 11:58 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
Hi folks

What would be the expected and correct output of the following

#include <stdio.h>

int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);

sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);

return 0;

}

On one platform I get:
(10,20,30) -10
(10,20,30 ) -12

on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.

Which is correct?
Bye, Jojo
I think second result is expected cause %n should yield the number of
characters consumed THUS far from the input and in second case input
consumption will end at ")" only and %n is coming later to that hence
it's undefined.
The C standard says: Execution of a %n directive does not increment
the assignment count returned at the completion of execution but the
Corrigendum seems to contradict this. Probably it is wise not to make
any assumptions on the effect of %n conversions on the return value.

-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.

Apr 19 '07 #4
"Gunvant Patil" <gu*************@gmail.comschrieb im Newsbeitrag
news:11**********************@n59g2000hsh.googlegr oups.com...
On Apr 19, 11:58 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
>Hi folks

What would be the expected and correct output of the following

#include <stdio.h>

int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);

sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);

return 0;

}

On one platform I get:
(10,20,30) -10
(10,20,30 ) -12

on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.

Which is correct?
Bye, Jojo

I think second result is expected cause %n should yield the number of
characters consumed THUS far from the input and in second case input
consumption will end at ")" only and %n is coming later to that hence
it's undefined.
I don't understand you here, in both cases the input ends at the ")" and %n
is immediatelly after that.
The C standard says: Execution of a %n directive does not increment
the assignment count returned at the completion of execution but the
Corrigendum seems to contradict this. Probably it is wise not to make
any assumptions on the effect of %n conversions on the return value.
Í've seen these 4 line in the Linux man-page, but only there. What does the
standard actually say? Chapter and verse?

Bye, Jojo
Apr 19 '07 #5
On 19 Apr, 12:25, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
"Gunvant Patil" <gunvant.k.pa...@gmail.comschrieb im Newsbeitragnews:11**********************@n59g2000h sh.googlegroups.com...
On Apr 19, 11:58 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
Hi folks
What would be the expected and correct output of the following
#include <stdio.h>
int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);
sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);
return 0;
}
On one platform I get:
(10,20,30) -10
(10,20,30 ) -12
on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.
Which is correct?
Bye, Jojo
I think second result is expected cause %n should yield the number of
characters consumed THUS far from the input and in second case input
consumption will end at ")" only and %n is coming later to that hence
it's undefined.

I don't understand you here, in both cases the input ends at the ")" and %n
is immediatelly after that.
That's not actually the case.

In the second case "(10,20,30 )", the scanning fails at the space
character.

If sscanf gives up at that point (it found a character which was
neither part of an integer nor a right parenthesis), it may well not
even consider the "%n".

Apr 19 '07 #6
Joachim Schmitz wrote:
<ma**********@pobox.comschrieb im Newsbeitrag
news:11*********************@b58g2000hsg.googlegro ups.com...
>On 19 Apr, 07:58, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
>>Hi folks

What would be the expected and correct output of the following

#include <stdio.h>

int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);

sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);

return 0;

}

On one platform I get:
(10,20,30) -10
(10,20,30 ) -12

on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.

Which is correct?
sscanf returns an integer which is the count of values assigned. Your
program would be improved by showing this information, in my opinion.

On my gcc/glibc system I get your second result and in both cases 3
values are reported as assigned. The same result is obtained on AIX,
using the IBM xlc compiler and IBM's C library implementation. This is
the result I'd expect.
I did try that too and sscanf returns 3 on all cases. What I'm concerned
about thoug is the %n conversion that does not seem to work properly. So
sscanf did process at least 9 of it's input characters "(10,20,30" as the
corresponding variables are filled, but regardless %n doesn't show that.
[...]
Because the %n is never processed. The ')' in the format
string does not match the space character after the "30", so
conversion stops. Three integers have been converted and
assigned successfully, so sscanf() returns the value 3; the
matching failure on the ')' does not affect what has already
happened.

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 19 '07 #7
On Apr 19, 4:25 pm, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
"Gunvant Patil" <gunvant.k.pa...@gmail.comschrieb im Newsbeitragnews:11**********************@n59g2000h sh.googlegroups.com...
On Apr 19, 11:58 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
Hi folks
What would be the expected and correct output of the following
#include <stdio.h>
int main (void)
{
char buffer1[] = "(10,20,30)";
char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
and ) */
int var1, var2, var3;
int i = 11111;
int j = 22222;
sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
printf ("%s -%d\n",buffer1,i);
sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
printf ("%s -%d\n",buffer2,j);
return 0;
}
On one platform I get:
(10,20,30) -10
(10,20,30 ) -12
on another I get:
(10,20,30) -10
(10,20,30 ) -22222
i.e. j is left unmodified.
Which is correct?
Bye, Jojo
I think second result is expected cause %n should yield the number of
characters consumed THUS far from the input and in second case input
consumption will end at ")" only and %n is coming later to that hence
it's undefined.

I don't understand you here, in both cases the input ends at the ")" and %n
is immediatelly after that.
In second case input scanning will fail at white space and not at ')'
The C standard says: Execution of a %n directive does not increment
the assignment count returned at the completion of execution but the
Corrigendum seems to contradict this. Probably it is wise not to make
any assumptions on the effect of %n conversions on the return value.

Í've seen these 4 line in the Linux man-page, but only there. What doesthe
standard actually say? Chapter and verse?
Yes indeed those lines are from linux man pages and here are some
references from C99

7.24.2.2#12

7.24.2.2#16

7.19.6.2 #10
Bye, Jojo- Hide quoted text -

- Show quoted text -
-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.
Apr 20 '07 #8

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

Similar topics

4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
10
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less...
22
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s",...
8
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a...
20
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since 2004? Thanks Mike
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
7
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.