wprintf () и wscanf в библиотеке C

Опубликовано: 12 Апреля, 2022

If you are dealing with the wide characters then printf() and scanf() function cannot be used. There are different functions for input and output C wide string.

  1. wprintf() : The wprintf() function writes the wide string pointed to by format to stdout. The wide string format may contain format specifiers starting with % which are replaced by the values of variables that are passed to the wprintf() function as additional arguments.

    Syntax :

    int wprintf (const wchar_t* format, ...);

    Parameters :

    • format : A pointer to a null terminated wide string that is written to stdout. It consists of wide characters along with optional format specifiers starting with %. The format specifiers are replaced by the values of respective variables that follows format.
    • … : Other additional arguments specifying the data to be printed. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
    • Return Value :If successful, the wprintf() function returns number of characters written. On failure it returns a negative value. If successful, the wprintf() function returns number of characters written.
      On failure it returns a negative value.
    // C Program to show the wprintf () function.
    #include <stdio.h>
    #include <wchar.h> // Header file containing wprintf function
    // Driver code
    int main()
    {
        wint_t x = 5;
        wchar_t name[] = L"GEEKS";
        wprintf(L"x = %d ", x);
        wprintf(L"HELLO %ls ", name);
        return 0;
    }

    Output:

    x = 5 
    HELLO GEEKS
    
  2. wscanf() : The wscanf() function reads the data from stdin and stores the values into the respective variables. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.

    Syntax :

    int wscanf (const wchar_t* format, ...);

    Parameters :

    • format : Pointer to a null-terminated character string that specifies how to read the input. It consists of format specifiers starting with %. Notice though, that all format specifiers have the same meaning as in scanf; therefore, %lc shall be used to read a wide character (and not %c), as well as %ls shall be used for wide strings (and not %s).
    • … : Other additional arguments for receiving data. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function.
    • Return Value : The wscanf() function returns the number of receiving arguments successfully assigned. We can count and match the expected number of items or be less due to a matching failure, a reading error is occured, or the reach of the end-of-file causes an error.
      If failure occurs before the first receiving argument was assigned, EOF is returned.
    // Program to show the wprintf () function.
    #include <stdio.h>
    #include <wchar.h> // Header file containing wscanf() function
    int main()
    {
        wchar_t str[80];
        int i;
      
        wscanf(L"%ls", str);
        wscanf(L"%d", &i);
        wprintf(L"I am a %ls of CSE in %d year. ", str, i);
        return 0;
    }

    Input:

    GEEK
    2
    

    Output:

    I am a GEEK of CSE in 2 year.
    
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
C