C++Learn C++

What Is The sscanf Function In C++ And How Can I Use It?

What is sscanf function? How can I use sscanf in C++? Where can I find format specifiers for the sccanf function? What is the syntax of sscanf ? What is a simple example of sscanf function in C++? Is There Full Examples to sscanf Function in C++? Let’s answer these questions by learning sscanf and then using the C++ IDE to easily build C++ applications in the future.

What is the sscanf function?

sscanf function is an old C function (since C95) that reads data from a variety of sources, scans and formats input from a string. In other terms, sscanf interprets it according to format and stores the results into given locations. We can use sscanf in C++. sscanf function reads the data from null-terminated character string buffer and defined in header <stdio.h> can be used in C++ applications. The end of the string is same as the end-of-file condition for fscanf. Generally sscanf function is good to convert parameters listed in a single string or a string from a data file, thuss it can be very useful to obtain values from a string data, and we can save these values to a modern database.

What is the syntax of the sscanf function?

Syntax of sscanf (Since C99):

What does the sscanf function do?

The sscanf function scans a series of input fields, one character at a time, reading from a string. Then each field is formatted according to a format specifier passed to sscanf in the format string pointed to by format. Finally, sscanf stores the formatted input at an address passed to it as an argument following format. There must be the same number of format specifiers and addresses as there are input fields. Format specifiers can be found here. For example, we can use “%d” or “%i” to read (scan) decimal or integer numbers, we can use “%f” to read floating-point numbers.

sscanf might stop scanning a particular field before it reaches the normal end-of-field (whitespace) character, or it might terminate entirely, for a number of reasons. See scanf for a discussion of possible causes.

On success, sscanf returns the number of input fields successfully scanned, converted, and stored; the return value does not include scanned fields that were not stored.

If sscanf attempts to read at end-of-string, it returns EOF. On error (If no fields were stored), it returns 0.

Note: sscanf considers the “s” specifier to refer to a two-byte character (wchar_t), rather than requiring the capital “s” or “ls” to indicate that a pointer to a two-byte character is being passed.

What is a simple example of the sscanf function in C++?

Here is an example of sscanf,

Are there any full examples of using the sscanf function in C++?

We can use sscanf in different input formats. For example if there is a string with two numbers separated by a space, we can read them as below,

If there is a string with two numbers separated by a coma, we can read them as below,

If there is a string with two floating numbers separated by a space, we can read them as below,

In C programming, there are scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s functions for the different requirements, can be used in C++.

Note that in C++ we can use std::cin to read values from an input. In a modern way, we can convert string to std::stringstream then we can get parameters that we need.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?