Problem with printing extended ascii using ncurses - c

I have got problem with printing extended ascii in terminal using wprintw function. This program prints letters instead squares. I was trying to change my locales but without effect. What should I change in my system to print it correctly?
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <unistd.h>
#include <locale.h>
#include <wchar.h>
int main(void)
{
setlocale(LC_ALL, "");
initscr();
WINDOW *game_window;
game_window=newwin(40,40,1,1);
wrefresh(game_window);
while (TRUE) {
wclear(game_window);
wprintw(game_window, "██████████████████████");
wrefresh(game_window);
sleep(3);
break;
}
endwin();
return 0;
}
I am working On Debian Jessie 10 and these are my locale:
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

Ok, I find the solution, when compiling the program you should use
this command:
gcc main.c -o main -lncursesw
Instead this:
gcc main.c -o main -lncurses

Related

strange behaviour with wine emulator, multithreading and C stdio

I've the following code:
#include <stdio.h>
#include <windows.h>
#include <process.h>
int main() {
/* DWORD WINAPI */void input(/* LPVOID */void* lpParam) {
Sleep(1000);
printf("hello mingw!\n");
// return 0;
}
_beginthread(input, 0, NULL);
getchar();
return 0;
}
I cross-compile from ubuntu 22.04.01 with mingw-w64 with:
...$ x86_64-w64-mingw32-gcc -o example.exe example.c
In a native Win7 box it works as expected:
...$ example.exe
hello mingw! (after 1 second)
a
...$
but in wine when entering 'a' from keyboard, the 'a' climbs up one line:
...$ example.exe
aello mingw!
...$
It seems as if printf output from a thread didn't update console cursor. This only happens when read code (getchar()) executes before the write code (printf), hence the Sleep.
Any pointers?

Latin Capital Letter 'E' with Circumflex (Ê)

In a C program in Windows 10, I should print the word TYCHÊ on the screen, but I cannot print the letter Ê (Hex code: \xCA):
#include <stdlib.h>
#include <stdio.h>
char *Word;
int main(int argc, char* argv[]){
Word = "TYCH\xCA";
printf("%s", Word);
}
What's wrong?
Windows is a pain when it comes to printing Unicode text, but the following should work with all modern compilers (MSVC 19 or later, g++ 9 or greater) on all modern Windows systems (Windows 10 or greater), in both Windows Console and Windows Terminal:
#include <iostream>
#include <windows.h>
int main()
{
SetConsoleOutputCP( CP_UTF8 );
std::cout << "TYCHÊ" << "\n";
}
Make sure your compiler takes UTF-8 as the input character set. For MSVC 19 you need a flag. I think it is the default for later versions, but I am unsure on that point:
cl /EHsc /W4 /Ox /std:c++17 /utf-8 example.cpp
g++ -Wall -Wextra -pedantic-errors -O3 -std=c++17 example.cpp
EDIT: Dangit, I misread the language tag again. :-(
Here’s some C:
#include <stdio.h>
#include <windows.h>
int main()
{
SetConsoleOutputCP( CP_UTF8 );
printf( "%s\n", "TYCHÊ" );
return 0;
}
You can try with this line
printf("%s%c", Word, 0x2580 + 82);
this can print your Ê.
I used CLion for resolve it, on another IDE it may not give the same result.
In the Windows Command Line you should choose the Code Page 65001:
CHCP 65001
If you want to silently do that directly from the source code:
system("CHCP 65001 > NUL");
In the C source code you should use the <locale.h> standard header.
#include <locale.h>
At the beginning of your program execution you can write:
setlocale(LC_ALL, "");
The empty string "" initializes to the default encoding of the underlying system (that you previously choose to be Unicode).
However, this answer of mine is just a patch, not a solution.
It will help you to print the french characters, at most.
Handling encoding in Windows command line is not straight.
See, for example: Command Line and UTF-8 issues

Printing emoji with wprintf() on windows

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main() {
setlocale(LC_CTYPE, "");
wprintf(L"٩(◕‿◕。)۶\n");
wprintf(L"😊\n");
return 0;
}
The code above works perfectly on linux exactly as expected:
$ ./a.out
٩(◕‿◕。)۶
😊
but completely breaks on windows:
PS> ./a.exe
()
why does this happen?
How do I then, print emojis on windows?
Im using powershell with gcc 9.2.0 on windows and WSL with gcc 9.3.0 on powershell for linux.

Simple C Program Lags [Homework]

For an assignment I have we are to find vulnerabilities in a certain C program and exploit them using various buffer overflow attacks. However when I run the .out file in the terminal with it's input argument it just stalls and doesn't do anything.
Even when I run GDB, that just lags too. I'm not looking for a solution to the assignment, I'm just looking for reasons why it's not running?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void partialwin()
{
printf("Achieved 1/2!\n");
}
void fullwin(){
printf("Achieved 2/2\n");
}
void vuln(){
char buffer[36];
gets(buffer);
printf("Buffer contents are %s\n",buffer);
}
int main(int argc,char**argv){
vuln();
}
Providing your sourc file is called assignment1.c and you're using gcc this should work, $ being your command prompt (which could be different on your platform)
$ gcc assignment1.c
$ a.out
Hello
Buffer contents are Hello
$

undefined reference to `libvlc_clock'

I'm trying to discover libVLC sdk in windows 7. Every time I compile the code, I'm getting this error
main.c:(.text+0xf): undefined reference to `libvlc_clock'
I've included the path. This is my code
#include <stdio.h>
#include <inttypes.h>
#include <vlc\vlc.h>
#include <vlc\libvlc.h>
int main()
{
int64_t time = libvlc_clock();
return 0;
}
and this is the line in prompt command in windows 7
gcc main.c -o test -I"C:\Program Files (x86)\VideoLAN\VLC\sdk\include"

Resources