devxlogo

Function sscanf

Function sscanf

Question:
Could you tell me what the proper syntax is for the sscanf function, and what return/error codes exist. This was for a Sparc-compiled program.

Answer:
Though newer programmers seem to favor the stream I/O classes, both sprintf and sscanf are still available and are quite powerful.

There’s no way I can begin to cover all the options these two functions support, but they use similar format codes. sprintf formats one or more values to a string, and sscanf extracts one or more values from a formated string.

char lpstr[45] = “StringOne StringTwo 3 4”;char lpstr1[20], lpstr2[20];int i1, i2;sscanf(lpstr, “%s %s %d %d”, lpstr1, lpstr2, &i1, &i2);
This example shows how you can extract four fields from a string with a single call to sscanf. In addition, sscanf returns the number of fields successfully converted and so you can use the return value to flag errors in the input string.
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist