w3resource

C sscanf() function

C library function - sscanf()

The sscanf() function is used to read data from buffer into the locations that are given by argument-list. Each argument must be a pointer to a variable with a type that corresponds to a type specifier in the format-string.

Syntax:

int sscanf(const char *buffer, const char *format, argument-list);

Parameters:

Name Description
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream.
format The format argument is a string containing C language conversion specifications. The conversion specification specifies the notation, alignment, significant digits, field width, and other aspects of the output format. To represent non-printing characters, such as newlines and tabs, the format string may contain escape characters. Below are the details.
additional arguments Depending on the format string, the function may require additional arguments.

The format is a character string, beginning and ending in its initial shift state, if any, composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters ( <space>, <tab>, <newline>, <vertical-tab>, or <form-feed>); an ordinary character; or a conversion specification. Each conversion specification is introduced by the character '%' or the character sequence "%n$", after which the following appear in sequence:

  • An optional assignment-suppressing character '*'.
  • An optional non-zero decimal integer that specifies the maximum field width.
  • An optional assignment-allocation character 'm'.
  • An option length modifier that specifies the size of the receiving object.
  • A conversion specifier character that specifies the type of conversion to be applied. The valid conversion specifiers are described below.

A format specifier for fscanf follows this prototype:

%[*][width][length]specifier

The following conversion specifiers are valid:

Character Type of input expected
d Matches an optionally signed decimal integer.
i Matches an optionally signed integer.
o Matches an optionally signed octal integer.
u Matches an optionally signed decimal integer.
x Matches an optionally signed hexadecimal integer,
a, e, f, g Matches an optionally signed floating-point number, infinity, or NaN
s Matches a sequence of bytes that are not white-space characters.
[ Matches a non-empty sequence of bytes from a set of expected bytes.
c Matches a sequence of bytes of the number specified by the field width.
p Matches an implementation-defined set of sequences.
n No input is consumed. The application shall ensure that the corresponding argument is a pointer to the integer into which shall be written the number of bytes read from the input so far by this call to the fscanf() functions. Execution of a %n conversion specification shall not increment the assignment count returned at the completion of execution of the function. No argument shall be converted, but one shall be consumed.
C Equivalent to lc.
S Equivalent to ls.
% Matches a single '%' character; no conversion or assignment occurs. The complete conversion specification shall be %%.

Following table shows the length modifiers and their meanings:

Length modifiers Description
hh Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to signed char or unsigned char.
h Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to short or unsigned short.
l Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long or unsigned long; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double.
ll Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long long or unsigned long long.
j Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to intmax_t or uintmax_t.
z Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to size_t or the corresponding signed integer type.
t Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to ptrdiff_t or the corresponding unsigned type.
L Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to long double.

Return value

  • On success, the function returns the number of variables filled. In the case of an input failure before any data could be successfully read, EOF is returned.

Example: sscanf() function

Following example uses sscanf() to read various data from the string data, and then displays that data.

#include <stdio.h>
#include <stddef.h>
int main(void)                                                         
{  
    char    *data = "Python Version 3.4";                                                               
    float fn;                                                          
    char  s1[81];                                                   
    char  s2[81];                                                   
    sscanf(data, "%s %s %f", s1, s2, &fn);                                                                                        
    printf("Display the data:\n");                                         
    printf("Data1 = %s",s1);                                    
    printf("\nData2 = %s",s2);                                                                          
    printf("\nFloating-point number = %f\n",fn);                                           
}                    

Output:

Display the data:
Data1 = Python
Data2 = Version
Floating-point number = 3.400000

C Programming Code Editor:

Previous C Programming: C scanf()
Next C Programming: C fgetc()



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What's an object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (-c is a common "just compile; don't link" option.)

Ref : https://bit.ly/3CbzF8M