Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is sscanf in C?

Behzad Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The sscanf() function allows us to read formatted data from a string rather than standard input through the keyboard.

Syntax

int sscanf( const char* buffer, const char* format, ... );

The sscanf() function reads data from the buffer and stores the values into their respective variables.

Code

#include<stdio.h>
#include<string.h>
int main()
{
char *str = "Behzad Ahmad 21";
char firstName[10], lastName[10];
int age, temp;
temp = sscanf(str, "%s %s %d", firstName, lastName, &age);
printf("First Name: %s\n", firstName);
printf("Last Name: %s\n", lastName);
printf("Age: %d\n", age);
return 0;
}

scanf() reads from the standard input stream stdin. In comparison, sscanf() reads from the character string s.

Both functions read characters, interpret them according to a format, and store the results in their arguments.

RELATED TAGS

c

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring