The function sscanf () in C

The function int sscanf (const char * str, const char * format, ...) in Library C standard reads the input formatted from a string.

The function int sscanf (const char * str, const char * format, .) in Library C standard reads the input formatted from a string.

Declare the function sscanf () in C

Below is the declaration for sscanf () in C:

 int sscanf ( const char * str , const char * format , .) 

Parameters

str - This is the string that functions to process it as its source to retrieve data.

format - This is the string containing one of the following items: Character Whitespace, Non-whitespace and Format specifier

A format specifier will be of the form: [=% [*] [width] [modifiers] type =]

ParametersDescription * This is a starting asterisk (optional) to indicate that the data is read from the Stream but ignored, for example, it is not stored in the corresponding parameter width Specifies the number of characters maximum read in current readings modifiers Define a different size from int (in the case of d, I and n), unsigned int (in the case of o, u and x) or float (in case e, f and g) for data pointed by the corresponding additional parameter: h for int short (for d, i and n), or unsigned short int (for o, u and x); l for int long (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g); L for long double (for e, f and g) type A character specifying the type of data to be read and how it is expected to be read. You follow the next table

Type specifier for fscanf function

TypeInput Shock type Single character: Read the next character. If a width other than 1 is specified, this function reads the width of characters and stores them in consecutive array positions that have been passed as parameters. No null characters are appended to the end of char * d Decimal integer numbers: This number is arbitrarily preceded by a + sign or - int * e, E, f, g, G floating point numbers: The decimal number contains a decimal point, arbitrarily preceded by a + or - sign and followed arbitrarily by the letter e or E and a decimal number. Two valid examples are -732.103 and 7.12e4 float * o Octal integer int * s Character string. It will read consecutive characters until a whitespace is found (maybe blank, newline) and tab) char * u Decimal integers unsigned int * x, X Hexadecimal integers segment int *

Other parameters - This function expects a range of cursors as additional parameters, each pointing to an object of type defined by their respective% -tag within the format format string, in the same order.

For each format specifier in the format format string that accepts data, an additional parameter is specified. If you want to store the result of a sscanf operation on a regular variable, you should pre-set its identifier identifier to the reference operators, for example: an ampersand (&), like: int n; sscanf (str, "% d", & n);

Returns the value

If successful, the function returns the number of variables to be filled. In case there is a failed input stripped when any data is successfully read, the function returns EOF.

For example

The following program C illustrates the usage of the sscanf () function in C:

 #include #include #include int main () { int day , year ; char weekday [ 20 ], month [ 20 ], dtm [ 100 ]; strcpy ( dtm , "Tuesday July 12 2016" ); sscanf ( dtm , "%s %s %d %d" , weekday , month , & day , & year ); printf ( "%s %d, %d = %sn" , month , day , year , weekday ); return ( 0 ); } 

Compiling and running the above C program will result:

Picture 1 of The function sscanf () in C

According to Tutorialspoint

Previous article: Function scanf () in C

Next lesson: Function fgetc () in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile