Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C stdio sscanf() Function

❮ C stdio Library


Example

Extract a number from a string:

char number[] = "24 hours";
int num;
sscanf(number, "%d", num);
printf("%d", num);

Definition and Usage

The sscanf() function reads data from a char array and writes it into memory locations specified by the arguments.

The sscanf() function is defined in the <stdio.h> header file.

The format parameter is a string that describes the format of the data that is expected from the file. If the content of the array does not match the format then the function stops reading at the point where the first mismatch occurs.

The format string can contain format specifiers which specify which parts of the data should be written to the arguments. Each format specifier corresponds to one of the additional arguments of the function. Details about format specifiers can be found on the scanf() reference page.


Syntax

sscanf(char * source, const char * format, arg1, arg2...);

Parameter Values

Parameter Description
source Required. A char array used as the data source.
format Required. A string representing the format of the data expected from the array.
arg1, arg2... Optional. Any number of additional arguments which are pointers to memory where values can be written.

Technical Details

Returns: An int value representing the number of arguments that were written to. It returns the constant EOF if an error occurred.

More Examples

Example

Extract numbers the sequence "1 + 2 = 3":

int a, b, c;
char source[] = "1 + 2 = 3";
sscanf(source, "%i + %i = %i", &a, &b, &c);
printf("a = %d \n", a);
printf("b = %d \n", b);
printf("c = %d \n", c);

Example

Read a hexadecimal number and output its value in decimal:

int num;
char hex[] = "FFAD01";
sscanf(hex, "%x", &num);
printf("%d", num);

Example

Search for either "x", "y" or "z" in the user input:

char c;
char source[] = "yes";
int found = sscanf(source, "%[xyz]", &c);
if(found > 0) {
  printf("Found %c", c);
} else {
  printf("Character not found");
}

❮ C stdio Library

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.