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:
- Create a function int myscanf() with const char* and ‘…’ as its parameters. [Here ‘…’ enables the function to receive any number of arguments]
- Initialize a pointer of type va_list to be able to work with variable arguments.
- Run a loop through the received string str. Repeat Steps 4 and 5 till we reach the end of str.
- Store the token when delimiter ‘%’ is found.
- 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().
- End the va_list pointer.
- Return 0.
Code:
C
// C program to illustrate the your own scanf() function // using variable argument list #include <stdarg.h> #include <stdio.h> // custon scanf() int myscanf( const char * str, ...)
{ char token[100];
int k = 0;
// initializing list pointer
va_list ptr;
va_start (ptr, str);
// parsing the formatted string
for ( int i = 0; str[i] != '\0' ; i++) {
// copying str to token
token[k++] = str[i];
// When a format specifier of null character is
// found
if (str[i + 1] == '%' || str[i + 1] == '\0' ) {
token[k] = '\0' ;
k = 0;
// processing token
char ch1 = token[1];
// for integers
if (ch1 == 'i' || ch1 == 'd' || ch1 == 'u' ) {
fscanf (stdin, "%i" , va_arg (ptr, int *));
}
// for short ubt
else if (ch1 == 'h' ) {
fscanf (stdin, "%hi" , va_arg (ptr, short *));
}
// for characters
else if (ch1 == 'c' ) {
char c;
// using this loop to ignore some chars
while ((c = fgetc (stdin)) == '\n'
|| c == ' ' || c == EOF) {
}
* va_arg (ptr, char *) = c;
}
// for float
else if (ch1 == 'f' ) {
fscanf (stdin, "%f" , va_arg (ptr, float *));
}
else if (ch1 == 'l' ) {
char ch2 = token[2];
// for long int
if (ch2 == 'u' || ch2 == 'd'
|| ch2 == 'i' ) {
fscanf (stdin, "%li" ,
va_arg (ptr, long *));
}
// for double
else if (ch2 == 'f' ) {
fscanf (stdin, "%lf" ,
va_arg (ptr, double *));
}
}
else if (ch1 == 'L' ) {
char ch2 = token[2];
// for long int
if (ch2 == 'u' || ch2 == 'd'
|| ch2 == 'i' ) {
fscanf (stdin, "%Li" ,
va_arg (ptr, long long *));
}
// for long double
else if (ch2 == 'f' ) {
fscanf (stdin, "%Lf" ,
va_arg (ptr, long double *));
}
}
// for string
else if (ch1 == 's' ) {
fscanf (stdin, "%s" , va_arg (ptr, char *));
}
}
}
// closing va_list
va_end (ptr);
return 0;
} // driver code int main()
{ int a;
float b;
char c;
char s[20];
myscanf( " %i %c %s %f" , &a, &c, s, &b);
printf ( "%i %c %s %f" , a, c, s, b);
return 0;
} |
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
Article Tags :