wprintf() and wscanf in C Library

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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 \n", x);
        wprintf(L"HELLO %ls \n", name);
        return 0;
    }

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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.\n", str, i);
        return 0;
    }

    chevron_right

    
    

    Input:

    GEEK
    2
    

    Output:

    I am a GEEK of CSE in 2 year.
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.