C sscanf() Function: A Comprehensive Guide for Beginners 🎯

beginner
22 min

C sscanf() Function: A Comprehensive Guide for Beginners 🎯

Welcome to the fascinating world of C programming! Today, we're going to delve into the sscanf() function, a powerful tool that allows you to read formatted data from a string. This function is an essential part of C programming, and understanding it will help you tackle many real-world projects. Let's get started!

What is the sscanf() Function? 📝

In simple terms, sscanf() is a function that scans a string and converts the input string into variables according to a specified format. It's like a reverse version of printf(), but instead of outputting data, it reads and stores data.

Syntax 📝

The syntax for sscanf() function is as follows:

c
int sscanf(const char *str, const char *format [, ...]);

Where:

  • str: A pointer to the input string that you want to read.
  • format: A format string that specifies how to convert the data in the input string.

Let's now explore how to use sscanf() with practical examples.

Example 1: Reading Integers 💡

Let's say we have a string "123 456 789" containing three integers. We can use sscanf() to read these integers and store them in variables:

c
#include <stdio.h> int main() { char str[] = "123 456 789"; int num1, num2, num3; sscanf(str, "%d %d %d", &num1, &num2, &num3); printf("num1 = %d\n", num1); printf("num2 = %d\n", num2); printf("num3 = %d\n", num3); return 0; }

In this example, we defined a string str containing three space-separated integers. We then use sscanf() to read these integers into variables num1, num2, and num3. The format string "%d %d %d" tells sscanf() to read three integers separated by spaces and store them in the provided variables.

When you run this code, you'll see the following output:

num1 = 123 num2 = 456 num3 = 789

Example 2: Reading Floating-Point Numbers 💡

Reading floating-point numbers with sscanf() is very similar to reading integers. Let's take a string containing two floating-point numbers:

c
#include <stdio.h> int main() { char str[] = "3.14159 2.71828"; float pi, e; sscanf(str, "%f %f", &pi, &e); printf("pi = %.4f\n", pi); printf("e = %.4f\n", e); return 0; }

In this example, we define a string str containing two floating-point numbers separated by a space. We use sscanf() to read these numbers into variables pi and e. The format string "%f %f" tells sscanf() to read two floating-point numbers separated by a space and store them in the provided variables.

When you run this code, you'll see the following output:

pi = 3.1416 e = 2.7183

Formatting with sscanf() 📝

The format string in sscanf() is a powerful tool that lets you specify how to read and convert the data in the input string. Here's a list of some common conversions:

  • %d: Convert the next sequence of characters to an integer.
  • %f: Convert the next sequence of characters to a floating-point number.
  • %s: Convert the next sequence of characters to a string, stopping at whitespace.
  • %c: Convert the next character to a character.

You can combine these conversions using spaces or commas to specify how to read multiple data items in the input string.

Quiz 🎯

:::quiz Question: In the following code snippet, what will the variables num1, num2, and num3 contain?

c
#include <stdio.h> int main() { char str[] = "123 456.789 10"; int num1, num2, num3; sscanf(str, "%d %f %d", &num1, &num2, &num3); printf("num1 = %d\n", num1); printf("num2 = %.2f\n", num2); printf("num3 = %d\n", num3); return 0; }

A: num1 = 123, num2 = 456.78, num3 = 10 B: num1 = 123, num2 = 456.789, num3 = 10 C: num1 = 123456, num2 = 78.9, num3 = 10 Correct: B Explanation: The format string "%d %f %d" tells sscanf() to read three items separated by whitespace. In this case, it correctly reads an integer (123), a floating-point number (456.789), and another integer (10). Option A and C are incorrect because they misinterpret the format string, causing incorrect conversions.