VOOZH about

URL: https://www.geeksforgeeks.org/c/how-to-create-your-own-scanf-in-c/

⇱ How to Create Your Own scanf() in C? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create Your Own scanf() in C?

Last Updated : 23 Jul, 2025

The scanf() function is used to take input from the console. It is defined in <stdio.h> header file and takes const char* and variable argument list as parameters.

In this article, we will learn how to implement your own custom scanf() function in C language. For this, we need to have a firm grasp of the following concepts:

Prerequisites:Variadic Functions in C, Strings in C, Format Specifiers in C

Problem Statement: Write your own scanf() in C

Example

Statement 1: scanf("%s %d", str, &in);
Input: Hello_world 10
str = Hello_world, in = 10

Statement 2: scanf("%d %s %f", &in, str, &fl);
Input: 10 Geeks 1.1
in = 10, str = Geeks, fl = 1.1

Algorithm:

  1. Create a function int myscanf() with const char* and '...' as its parameters. [Here '...' enables the function to receive any number of arguments]
  2. Initialize a pointer of type va_list to be able to work with variable arguments.
  3. Run a loop through the received string str. Repeat Steps 4 and 5 till we reach the end of str.
  4. Store the token when delimiter '%' is found.
  5. Check if the second character of the token is a format specifier of scanf(). If true then retrieve its pointer using va_arg and pass it to fscanf().
  6. End the va_list pointer.
  7. Return 0.

Code:


Input

2023 g Article 4.04

Output

2023 g Article 4.040000

Time Complexity

The time complexity of the following function is: O(n), where,

  • N: Length of the formatted string.

Must Read - scanf() in C

Comment
Article Tags: