473,327 Members | 2,094 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,327 software developers and data experts.

help to explain sscanf()

Hi,

I run the following source code:

#include <stdio.h>

main()
{
int i;
unsigned int j;
char input[] = "10 0x1b aaaaaaaa bbbbbbb";
char s[5];
sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);
printf("%d %d %s\n", i, j, s);
}
And it prints out: 10 27 aaaaa

What I understand is sscan puts something into s twice. the first
time, s = "aaaaa". afterwards, sscanf put some floating number to s.
So when printf, s should be not aaaa.

can anybody help to explain?

Nov 5 '07 #1
5 3174
On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <pe*****@gmail.comwrote:
>Hi,

I run the following source code:

#include <stdio.h>

main()
{
int i;
unsigned int j;
char input[] = "10 0x1b aaaaaaaa bbbbbbb";
char s[5];
sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);
What exactly do you think a conversion specification means %5[a-z]
means? Did you perhaps want the two specifications to read %5[a-z]s
%*s instead of %5[a-z] %*s, a single specification which has two %
characters and an imbedded space?

Which argument corresponds to the %f? Does it in fact point to a
float?
printf("%d %d %s\n", i, j, s);
What type of argument does the second %d require? What type did you
give it?
>}
And it prints out: 10 27 aaaaa

What I understand is sscan puts something into s twice. the first
time, s = "aaaaa". afterwards, sscanf put some floating number to s.
What type is s? Is it capable of holding a float?
>So when printf, s should be not aaaa.

can anybody help to explain?
By definition, undefined behavior is unexplainable.
Remove del for email
Nov 6 '07 #2
Barry Schwarz <sc******@doezl.netwrites:
On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <pe*****@gmail.comwrote:
>>I run the following source code:

#include <stdio.h>

main()
{
int i;
unsigned int j;
char input[] = "10 0x1b aaaaaaaa bbbbbbb";
char s[5];
sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);

What exactly do you think a conversion specification means %5[a-z]
means? Did you perhaps want the two specifications to read %5[a-z]s
%*s instead of %5[a-z] %*s, a single specification which has two %
characters and an imbedded space?
A %[ format ends at "the matching ]" which is the first ] unless the
format starts [] or [^]. The original %5[a-z] is quite probably
right.

As you point out, the 's' argument supplied for the %f is wrong.

--
Ben.
Nov 6 '07 #3
On Tue, 06 Nov 2007 16:48:38 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
>Barry Schwarz <sc******@doezl.netwrites:
>On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <pe*****@gmail.comwrote:
>>>I run the following source code:

#include <stdio.h>

main()
{
int i;
unsigned int j;
char input[] = "10 0x1b aaaaaaaa bbbbbbb";
char s[5];
sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);

What exactly do you think a conversion specification means %5[a-z]
means? Did you perhaps want the two specifications to read %5[a-z]s
%*s instead of %5[a-z] %*s, a single specification which has two %
characters and an imbedded space?

A %[ format ends at "the matching ]" which is the first ] unless the
format starts [] or [^]. The original %5[a-z] is quite probably
right.
Yes. For some reason I had it in my mind that the [...] was a
modifier when it is actually a specification. Oddly enough, n1124
says the - makes the behavior implementation defined. I wonder if any
system (other than the DS9000) does something different from the
obvious.
Remove del for email
Nov 8 '07 #4
Barry Schwarz <sc******@doezl.netwrites:
>>On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <pe*****@gmail.comwrote:
sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);
Oddly enough, n1124 says the - makes the behavior
implementation defined. I wonder if any system (other than the
DS9000) does something different from the obvious.
It's probably implementation-defined because the characters in
between 'a' and 'z' are implementation-defined; 'a' might not
even precede 'z'. Only ranges of digits could be portable.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Nov 8 '07 #5
"Ben Pfaff" <bl*@cs.stanford.edua écrit dans le message de news:
87************@blp.benpfaff.org...
Barry Schwarz <sc******@doezl.netwrites:
>>>On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <pe*****@gmail.comwrote:
sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);
>Oddly enough, n1124 says the - makes the behavior
implementation defined. I wonder if any system (other than the
DS9000) does something different from the obvious.

It's probably implementation-defined because the characters in
between 'a' and 'z' are implementation-defined; 'a' might not
even precede 'z'. Only ranges of digits could be portable.
IIRC, a-z spans more than 26 values in EBCDIC.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return
0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case
1:putchar(a[i&15]);break;}}}
Your signature compiles with a warning:
bpsig.c:1: warning: control reaches end of non-void function

This is a corrected version, smaller of course ;-)

int putchar(int);int main(void){unsigned long b[]={0x67dffdff,0x9aa9aa6a
,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,4598} ,*p=b,i=24;for(;;p+=!(
*p/=4))switch(0[p]&3)case-0:{return 0;while(i-=2)case+2:{if(!++i)default
:continue;else break;case!0:putchar("\n .CJacehknorstu"[i&15]);break;}}}

--
Chqrlie.
Nov 8 '07 #6

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

Similar topics

3
by: Ramprasad A Padmanabhan | last post by:
Hello all I want to read into a string an input from QUERY_STRING how do I ensure that scanf reads more chars into the string that it can hold eg { char* s1; char* data; long n;
1
by: Andrej Hocevar | last post by:
Hello, below I've attached a test program reading data from a file and storing that information in a linked list. First problem is that current->p_name will print only the last item in the file...
2
by: Stu | last post by:
I have the following "C" program, which works fine. #include <stdio.h> #include <stdlib.h> #include <strings.h> int main() { char *buffer = "1234 - 5678";
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...
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...
10
by: nk | last post by:
Hi, I'm a newbie. The code below sorts names entered. ---------------------------------------------------------------------------- #include <stdio.h> #include <conio.h> #include <string.h>...
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
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.