Advertisement

sscanf

Started by May 23, 2000 09:46 AM
6 comments, last by SKSlayer 24 years, 6 months ago
sscanf(cvar, "my string %?", &myvar); %? what else than %f and %d ? and which types are these
(you can find me on IRC : #opengl on undernet)
Look at the C Reference at www.delorie.com/djgpp
All of the %d, %f, etc should be listed.
Advertisement
MSDN description:

A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [.precision] [{h / l / I64 / L}]type

Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%.

The optional fields, which appear before the type character, control other aspects of the formatting, as follows:

type

Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see Table R.3).

flags

Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table R.4). More than one flag can appear in a format specification.

width

Optional number that specifies the minimum number of characters output. (See printf Width Specification.)

precision

Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see Table R.5).

Visit our homepage: www.rarebyte.de.st

GA
Visit our homepage: www.rarebyte.de.stGA
%f is for floating point variables and %d is for integer variables.

Morgan
Here''s the ones I know :

%s = string
%c = char
%i = integer (I thought d was for dword?)

Hope that helps some,

Justin Eslinger
~-=-=-=-=-=-=~~Justin Eslinger~~.."BlackScar"..~~-=-=-=-=-=-=~
Hi, this is not a complete list of code format (they say to your function how you want to display your info) but you should find the most common :
%d : int (base 10)
%o : int base 8
%x : int base 16
%hd : short (base 10)
%hx : short (base 16)
%ho : short (base 8)
%ld,%lx, %lo (long in base 10,16,8)
%f : float
%lf : double
%Lf : long double
%c : char
%s : string

ex :
char myName[256];
int howOldIam;
...
...
printf ("My name is %s and i am %d years old",myName,howOldIAm);
Advertisement

Thank You


That''s perfect (except is someone have the full list)

(you can find me on IRC : #opengl on undernet)
scanf Type Field Characters

The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number.



Table R.8 Type Characters for scanf functions





Character



Type of Input Expected



Type of Argument





c



When used with scanf functions, specifies single-byte character; when used with wscanf functions, specifies wide character. White-space characters that are ordinarily skipped are read when c is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws.



Pointer to char when used with scanf functions, pointer to wchar_t when used with wscanf functions.



C



When used with scanf functions, specifies wide character; when used with wscanf functions, specifies single-byte character. White-space characters that are ordinarily skipped are read when C is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws.



Pointer to wchar_t when used with scanf functions, pointer to char when used with wscanf functions.



d



Decimal integer.



Pointer to int.



i



Decimal, hexadecimal, or octal integer.



Pointer to int.



o



Octal integer.



Pointer to int.



u



Unsigned decimal integer.



Pointer to unsigned int.



x



Hexadecimal integer.



Pointer to int.



e, E, f, g, G



Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent ("e" or "E") followed by an optionally signed integer value.



Pointer to float.



n



No input read from stream or buffer.



Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf functions.



s



String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed following Table R.7.



When used with scanf functions, signifies single-byte character array; when used with wscanf functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.



S



String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed preceding this table.



When used with scanf functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.




The types c, C, s, and S are Microsoft extensions and are not ANSI-compatible.

Thus, to read single-byte or wide characters with scanf functions and wscanf functions, use format specifiers as follows.



To Read Character As



Use This Function



With These Format Specifiers





single byte



scanf functions



c, hc, or hC



single byte



wscanf functions



C, hc, or hC



wide



wscanf functions



c, lc, or lC



wide



scanf functions



C, lc, or lC




To scan strings with scanf functions, and wscanf functions, use the prefixes h and l analogously with format type-specifiers s and S.



scanf Width Specification

width is a positive decimal integer controlling the maximum number of characters to be read from stdin. No more than width characters are converted and stored at the corresponding argument. Fewer than width characters may be read if a white-space character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.

The optional prefixes h, l, I64, and L indicate the "size" of the argument (long or short, single-byte character or wide character, depending upon the type character that they modify). These format-specification characters are used with type characters in scanf or wscanf functions to specify interpretation of arguments as shown in the Table R.7. The type prefixes h, l, I64, and L are Microsoft extensions and are not ANSI-compatible. The type characters and their meanings are described in Table R.8.



Table R.7 Size Prefixes for scanf and wscanf Format-Type Specifiers





To Specify



Use Prefix



With Type Specifier





double



l



e, E, f, g, or G



long int



l



d, i, o, x, or X



long unsigned int



l



u



short int



h



d, i, o, x, or X



short unsigned int



h



u



__int64



I64



d, i, o, u, x, or X



Single-byte character with scanf



h



c or C



Single-byte character with wscanf



h



c or C



Wide character with scanf



l



c or C



Wide character with wscanf



l



c, or C



Single-byte – character string with scanf



h



s or S



Single-byte – character string with wscanf



h



s or S



Wide-character string with scanf



l



s or S



Wide-character string with wscanf



l



s or S




Following are examples of the use of h and l with scanffunctions and wscanf functions:




scanf( "%ls", &x ); // Read a wide-character string
wscanf( "%lC", &x ); // Read a single-byte character


To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

To store a string without storing a terminating null character (''\0''), use the specification %nc where n is a decimal integer. In this case, the c type character indicates that the argument is a pointer to a character array. The next n characters are read from the input stream into the specified location, and no null character (''\0'') is appended. If n is not specified, its default value is 1.

The scanf function scans each input field, character by character. It may stop reading a particular input field before it reaches a space character for a variety of reasons:



•The specified width has been reached.•The next character cannot be converted as specified.•The next character conflicts with a character in the control string that it is supposed to match. •The next character fails to appear in a given character set.



For whatever reason, when the scanf function stops reading an input field, the next input field is considered to begin at the first unread character. The conflicting character, if there is one, is considered unread and is the first character of the next input field or the first character in subsequent read operations on stdin.

This topic is closed to new replies.

Advertisement