Ryan Weil Ryan Weil - 7 months ago 61
C Question

Place String Literal Before Variable

I'm trying to get the user's input and display it in a messagebox. The issue I'm having, is without the 'L' before the string, it comes out as gibberish. My char[] named 'input' stores the string the user enters, but how can I make it so there is an 'L' there? Everything I've tried so far has given me an error, including changing the char to wchar_t.

#include <stdio.h>
#include <windows.h>

int main()
printf("Enter message to be displayed\n");
char input[20];
fgets(input,20, stdin);
MessageBox(NULL, (LPCWSTR)input, (LPCWSTR)L"You wrote", MB_OK);
return 0;


Probably your environment defines _UNICODE for compilation process and MessageBox macro is expanded to MessageBoxW. Try to replace your code with this:

MessageBoxA(NULL, input, "You wrote", MB_OK);

This explicitly states that you want to use the ANSI version, that will handle result of fgets() properly.