1

I have a simple program which takes user input as a string and output the entered string. The only difference is that I have provided two option to the user,

First to input a basic string.

Second to input a wide string.

The scanf() successfully takes user input for basic string but wscanf() does not prompt for user input and just exit.

Why this is happening with wscanf() and not with scanf() ? How would I take take user input string from wscanf() in the same program.

#include <stddef.h>
#include <stdio.h>
#include <wchar.h>

void basic()
{
    char str[100];
    printf("Enter string with basic string char: ");
    scanf("%s", str);
    printf("Entered string : %s \n", str);
}

void wide()
{
    wchar_t str[100];
    wprintf(L"Enter string with wide string char: ");
    wscanf(L"%ls", str);
    wprintf(L"Entered string : %ls \n", str);
}


int main(int argc, char **argv)
{
    int option = 1;
    printf("\n Basic string (char*) vs Wide string (wchar_t*) \n\n");
    printf("1. Basic string \n2. Wide string \n");
    printf("Enter choice : ");
    scanf("%d", &option);
    switch(option)
    {
    case 1:
        basic();
        break;
    case 2 :
        wide();
        break;
    default:
        printf("Invalid choice \n");
    }

    return 0;
} 

Output:

1. Basic string :

Basic string (char*) vs Wide string (wchar_t*) 

1. Basic string 

2. Wide string 

Enter choice : 1

Enter string with basic string char: hello

Entered string : hello 

2. Wide string :

Basic string (char*) vs Wide string (wchar_t*) 

1. Basic string 

2. Wide string 

Enter choice : 2
Prateek Joshi
  • 3,929
  • 3
  • 41
  • 51
  • Check the return value of wscanf. Also it would be good to give details of your compiler and system, as there has been (and still is) a lot of variety in wide string function implementations, often the standard is not followed โ€“ M.M Oct 21 '19 at 00:43

2 Answers2

1

wscanf() behaving differently than scanf() when taking input

Orientation.

Code's first use of stdin is scanf("%d", &option) establishing a byte-oriented stream.

Following use of wscanf(L"%ls", str); is UB. Stick with one orientation or re-open files.

Each stream has an orientation. After a stream is associated with an external file, but before any operations are performed on it, the stream is without orientation. Once a wide character input/output function has been applied to a stream without orientation, the stream becomes a wide-oriented stream. Similarly, once a byte input/output function has been applied to a stream without orientation, the stream becomes a byte-oriented stream. Only a call to the freopen function or the fwide function can otherwise alter the orientation of a stream. (A successful call to freopen removes any orientation.) C11 ยง7.21.2 4.

Similar issue applies to stdout.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

The first scanf() command scanf("%d", &option); leaves \n in stdin, which wscanf() is catching. That is what causing the error, i think. I had a similar problem a week ago. You can read more about it here, where you can find good answers and workarounds i have get.

You can either put a getchar() after scanf() to pull out the \n or avoid the use of scanf() in total and use an alternative, f.e. sscanf().