Open In App

Inbuilt library functions for user Input | sscanf, scanf_s, fscanf_s, sscanf_s

The C Programming Language provides various Inbuilt Library Functions for User Input. In this article, we will learn about sscanf, scanf_s, fscanf_s, sscanf_s Library Functions in C.

1. sscanf() Function in C

sscanf() is used to read formatted input from the string. Both scanf() and sscanf() functions are similar, the only difference between them is that scanf() function reads input from the user from standard input like a keyboard, and sscanf() function reads input from a string and stores the input in another string.

Syntax

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

Parameters

Return Value

Note: There should be at least as many of these arguments as the number of values stored by the format specifiers.

Example: C Program to Illustrate sscanf Function




// C program to illustrate sscanf statement
#include <stdio.h>
 
int main()
{
    // declaring array s
    char s[] = "3 red balls 2 blue balls";
    char str[10], str2[10];
    int i;
 
    // %*s is used to skip a word
    sscanf(s, "%d %*s %*s %*s %s %s", &i, str, str2);
 
    printf("%d %s %s \n", i, str, str2);
 
    return 0;
}

Output
3 blue balls 

2. scanf_s() Function in C

This function is specific to Microsoft compilers. It is the same as scanf, except it does not cause buffer overload. scanf_s() function is more secure than scanf() function as it provides an additional parameter to specify the buffer size that can avoid buffer overflow.

Syntax

int scanf_s(const char *format [argument]...);

Parameters

Note: Here we can specify the buffer size and actually control the limit of the input so that the whole application don’t crash due to memory overflow.

Return Value

Why to use scanf_s()?

scanf just reads whatever input is provided from the console. C does not check whether the user input will fit in the variable that you’ve designated. If you have an array called color[3] and you use scanf for the string “Red”, it will work fine but if user enters more than 3 characters scanf starts writing into memory that doesn’t belong to colour array.

C won’t catch this or warn you and it might or might not crash the program, depending on if something tries to access and write on that memory slot that doesn’t belong to color array. This is where scanf_s comes into play. scanf_s checks that the user input will fit in the given memory space.

Note: scanf_s() will only work in Microsoft Visual Studio.

Example 1: C Program to Illustrate sscanf_s Function




// C program to illustrate sscanf_s statement
// scanf_s() will only work in Microsoft Visual Studio.
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a[5];
 
    // sizeof(a) is buffer size
    scanf_s("%s", a, sizeof(a));
 
    printf("\n%s ", a);
 
    return 0;
}

Input 1

Red

Output 1

Red

Input 2

Yellow

Output 2

No Output

Example 2: C Program to Illustrate the relation between buffer size and array size.




// C++ program
// consumes the Enter key
// (newline character) pressed after input
 
#include <stdio.h>
int main()
{
    // example
    char ch[100000];
    printf("Enter characters: ");
    scanf_s("%s", ch, 99999);
    getchar();
    return 0;
}




// C program
// consumes the Enter key
// (newline character) pressed after input
#include <stdio.h>
 
int main()
{
    char ch[100000];
    printf("Enter characters: ");
    scanf_s("%s", ch, 99999);
    getchar();
    return 0;
}

3. fscanf_s() Function in C

Difference between fscanf() and fscanf_s() is same as that of scanf() and scanf_s(). fscanf_s() is secure function that require the size of each c, C, s, S and [ ] type field to be passed as an argument immediately following the variable.

Syntax

int fscanf_s(   FILE *stream,  const char *format ,[argument ]... );

Parameters

Note: fscanf_s has an extra parameter to specify the buffer size and actually control the limit of the input.

Return Value

Note: fscanf_s will work only in MS Visual studio.

Example: C Program to Illustrate fscanf_s Function




// C program to illustrate fscanf_s statement
// This program will run on MS Visual studio
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char s1[10], s2[10], s3[10];
    int year;
 
    // file pointer
    FILE* fp;
 
    // Open file securely
    fopen_s(&fp, "file.txt", "w+");
 
    fputs("Hello World its 2017", fp);
 
    rewind(fp);
 
    // Using fscanf_s
    fscanf_s(fp, "%s", s1, sizeof(s1));
    fscanf_s(fp, "%s", s2, sizeof(s2));
    fscanf_s(fp, "%s", s3, sizeof(s3));
    fscanf_s(fp, "%d", &year, sizeof(year));
 
    printf("String1 |%s|\n", s1);
    printf("String2 |%s|\n", s2);
    printf("String3 |%s|\n", s3);
    printf("Integer |%d|\n", year);
 
    fclose(fp);
 
    return (0);
}

Output

String1 |Hello|
String2 |World|
String3 |its|
Integer |2017|

4. sscanf_s() Function in C

sscanf_s() is secure function of sscanf() and secure functions require the size of each c, C, s, S and [ type field to be passed as an argument immediately following the variable.

Syntax

int sscanf_s(const char *restrict buffer, const char *restrict format, ...);

Parameters

Note: sscanf_s has an extra parameter to specify the buffer size and actually control the limit of the input.

Return Value

Note: sscanf_s() will only work in Microsoft Visual Studio.

Example: C Program to Illustrate sscanf_s Function




// C program to illustrate sscanf_s statement
// This program will run on MS Visual studio
#include <stdio.h>
 
int main()
{
    char s[] = "3 red balls 2 blue balls";
    char str[10], str2[10];
    int i;
 
    // %*s is used to skip a word
    sscanf_s(s, "%d", &i, sizeof(i));
    sscanf_s(s, "%*d %*s %*s %*s %s", str, sizeof(str));
    sscanf_s(s, "%*d %*s %*s %*s %*s %s", str2,
             sizeof(str2));
 
    printf("%d %s %s \n", i, str, str2);
 
    return 0;
}

Output

3 blue balls

Article Tags :