Print utf8 wchar_t in windows console - c

I'm running CodeBlocks in Windows 10 and even after following some of the indications found on SO, I did not manage to print in cosole utf8 chars.
_setmode(_fileno(stdout), 0x00020000); // _O_U16TEXT
setlocale(LC_ALL, "C.UTF-8");
wchar_t star = 0x2605;
wchar_t st = L'★';
if (printf("%lc\n", star) < 0) {
perror("printf");
}
if (printf("\n%lc\n", st) < 0) {
perror("printf");
}
gives the output
printf: Invalid argument
printf: Invalid argument
The console Properties >> Font is set to Lucida Console, similar to codeblocks code editor. I am able to see the star as a symbol inside my text editor but not in the console. What should I do?
EDIT:
even if I change the previous code to:
if (wprintf(L"%lc", star) < 0) {
perror("printf");
}
printf("\n");
if (wprintf(L"%lc", st) < 0) {
perror("printf");
}
I am not able to see more than two rectangles instead of the stars (which if I copy and try to paste them here, are drawn in the right way)

After trying all of my available fonts in the console, the only one which let me see the actual star is SimSun-ExtB.

Related

How do I make characters in C using the ncurses.h library "blink"

Before I make one of these posts, I look around at around 5-10 other forums to see if my question has been answered.
There are a lot of websites that explain that my compiler doesn't have blinking enabled, and that I just need to download the package to enable it, or something to that affect
However, of all the ones that I have seen, none of them go into detail about where and how to acquire the package I need to allow blinking, or if they do then it isn't with my compiler.
So if someone could assist me, how do I enable blinking on Ubuntu with a function such as
attron(A_BLINK);
I'm aware that similarly phrased questions will get down-votes. I do not care, I just want to know how to fix my problem.
Any feedback would be really appreciated
-Edit
#include <string.h>
#include <ncurses.h>
int main(void)
{
char text[] = "Please Blink";
size_t len = strlen(text);
int i, row, col;
initscr();
getmaxyx(stdscr, row,col);
keypad(stdscr, TRUE);
noecho();
curs_set(0);
move((row / 2), (col / 2) - (len / 2));
attron(A_BLINK);
for(i = 0; i < len; ++i)
{
printw("%c", text[i]);
}
refresh();
getch();
attroff(A_BLINK);
endwin();
return 0;
}
-Ryan
ansi escape sequences
as an example of usage:
from the terminal type:
echo -e "\x1b[5;32;43m"
this results in a blinking green foreground over a yellow background
then type:
echo "test"
to display the word test as green letters over a yellow background
then, if the screen has not returned to normal operation, type:
echo -e "\1b[0m"
to reset the screen to normal
Note: the same ansi escape sequences can be output using the printf() function via:
printf( "%s\n", "\x1b[5;32;43m" );
and so on.
Blinking is a property of your terminal emulator. If your terminal does not support it, it won't happen.
FWIW, a plain xterm window does support blinking for me.
However, if you want the text to blink, you can manually make it happen by periodically overwriting the text with blanks. Something along the lines of:
int toggle = 0;
halfdelay(5);
do {
toggle = !toggle;
move((row / 2), (col / 2) - (len / 2));
printw("%*.*s", len, len, toggle ? text : "");
refresh();
} while (getch() == ERR);
nocbreak();
The halfdelay() call will cause getch() to return after 5/10ths of a second. If no key was hit within that time, ERR is returned. The nocbreak() call will disable halfdelay().

C Program behaves differently if compiled using CLion rather than using gcc directly. Why?

I'm writing a program to play 4 in a row using C90.
I made a UI for the Console using ASCII characters.
If I compile the program using C Lion I get the following output:
If I compile it using gcc main.c and then run ./a.out I get the following result:
So obviously this dot character is sized differently if I compile it using gcc directly.
Has anyone any idea how this could possibly happen?
The code that is responsible for printing the game lines looks like this:
void printGameLine(int line[7]) {
int i;
printf("┃");
for (i = 0; i < 7; ++i) {
printColor(line[i]);
line[i] == 0 ? printf(" ") : printf(" ⬤ ");
printColor(0);
printf("┃");
}
printf("\n");
}
The code responsible for the colors looks like this: (If this makes any difference)
/**
* prints the color
* #param player -1 First player, 0 neutral, 1 Second Player
*/
void printColor(int player) {
switch (player) {
case 1:
printf("\033[0;31m"); /*red*/
break;
case -1:
printf("\033[0;33m");/*yellow*/
break;
default:
printf("\033[0m");/*neutral*/
break;
}
}
The issue is the font choice for the CLion Console.
Go to File/[Settings...] then Editor/[Color Scheme]/[Console Font]
Disable the "Use console font instead of the default" checkbox
Choose a proper monospaced font of your choice (I use Fira Code)
After that ensure to use same-length strings for both the empty and the "ball'd" positions.

CLion - Carriage return? \r

I am using the CLion IDE and I am trying to do a carriage return.
I am doing a print statement in C and have the following syntax:
printf("\rHello World!"); which is inside a loop. The loop still prints every Hello World on its own line. There are no \n's in my program. I've tried changing the line separators options to unix mac OS and windows and none of them change the functionality. Google has also led me to no useful answers.
int main()
{
int i = 0;
while (i < 5000000)
{
printf("\rThis is line number %d!", i++);
}
return 0;
}
My expected output is only a single line of text in the console window.
Thanks.
Your problem is PuTTY console, that is used in CLion by default.
You can switch it off in Registry:
Help | Find Action | Registry... =>
run.processes.with.pty [ ] <- uncheck
I recommend you modify the program:
#include <iostream>
int main() {
int i = 0;
while (i < 500) {
printf("\rThis is line number %d!", i++);
fflush(stdout); // <- add this call
}
return 0;
}

Eclipse printing symbols instead of 'chars' on console for C

new to C. Trying to get it to work with Eclipse since it seems a lot easier to use then vi (formatting, syntax warnings before compiling etc).
Now my program works fine in a terminal but in Eclipse console it does not seem to work well.
I have found the source of error but I need some assistance fixing it.
For some reason Eclipse console does not like to re-locate the cursor to print over existing text (or so it seems). Because when I print regularly using printf(...) it prints fine.
I am using these functions to re-print and Eclipse console does not like it. I first re-locate the cursor with set_cur_pos(..) and then in a for loop reprint my characters on top of existing text by calling put(..)
//
// Name: put
// Description: calls putchar to place a character, then flushes stdout
// #param character - character to print onto console
void put(char character) {
putchar(character);
fflush( stdout);
} // clear
//
// Name: set_cur_pos
// Description - sets cursor at a specific location on console
// #param rCursor - row location to place cursor
// #param cCursor - col location to place cursor
//
void set_cur_pos(int rCursor, int cCursor) {
printf("\033[%d;%dH", rCursor, cCursor);
} // set_cur_pos
EG: I'd use it like this
/**
* Description: Function prints a grid using cursor control.
*/
static void printOver(char grid[40][40]) {
int i, j;
for ( i = 0; i < size; i++ ) {
for ( j = 0; j < size; j++ ) {
set_cur_pos(i, j);
put(grid[i][j]);
}
}
Any ideas why Eclipse prints this out?
[2J[0;0H [0;1H*[0;2H [0;3H [0;4H [0;5HY[0;6H*[0;7H [0;8H [0;9H [1;0H [1;1H [1;2H [1;3H [1;4H [1;5HY[1;6H [1;7H [1;8H [1;9H*[2;0H [2;1H [2;2H*[2;3H [2;4H*[2;5HY[2;6H [2;7HY[2;8H [2;9H [3;0H [3;1H [3;2H [3;3H [3;4HY[3;5HY[3;6H [3;7H [3;8H [3;9H [4;0H*[4;1H [4;2H [4;3H [4;4HY[4;5H [4;6H [4;7H [4;8H [4;9H [5;0H*[5;1HY[5;2H [5;3H [5;4H [5;5H [5;6H [5;7H [5;8H*[5;9H [6;0HY[6;1HY[6;2H [6;3H [6;4H [6;5HY[6;6HY[6;7H*[6;8H [6;9H [7;0H [7;1HY[7;2H [7;3HY[7;4H [7;5H [7;6HY[7;7H [7;8H [7;9H*[8;0HY[8;1HY[8;2H [8;3H [8;4H [8;5H [8;6H [8;7H*[8;8H [8;9H [9;0H [9;1H [9;2H [9;3HY[9;4H [9;5H*[9;6H [9;7H [9;8H [9;9H [10;0H
[0;0H [0;1H_[0;2H [0;3H [0;4H [0;5HY[0;6H_[0;7H [0;8H [0;9H [1;0H [1;1H [1;2H [1;3H [1;4H [1;5HY[1;6H [1;7H [1;8H [1;9H*[2;0H [2;1H [2;2H*[2;3H [2;4H*[2;5HY[2;6H [2;7H*[2;8H [2;9H [3;0H [3;1H [3;2H [3;3H [3;4HY[3;5H*[3;6H [3;7H [3;8H [3;9H [4;0H*[4;1H [4;2H [4;3H [4;4HY[4;5H [4;6H [4;7H [4;8H [4;9H [5;0H*[5;1HY[5;2H [5;3H [5;4H [5;5H [5;6H [5;7H [5;8H*[5;9H [6;0HY[6;1H*[6;2H [6;3H [6;4H [6;5HY[6;6H*[6;7H_[6;8H [6;9H [7;0H [7;1HY[7;2H [7;3HY[7;4H [7;5H [7;6HY[7;7H [7;8H [7;9H_[8;0HY[8;1HY[8;2H [8;3H [8;4H [8;5H [8;6H [8;7H_[8;8H [8;9H [9;0H [9;1H [9;2H [9;3HY[9;4H [9;5H*[9;6H [9;7H [9;8H [9;9H [10;0H
These \033[... escape sequences are not terminal portable, eg. between bash and cmd.
So I think the eclipse IDE console does not interpret them as a cursor set command.
The single one portable cursor command without using PALs is '\r', wich will set
the cursor to the beginning of the line.

Why does Sublime Text report that my program terminates with exit code 1?

I was using Sublime Text to compile a code I made, but it gave me this error:
[Decode error - output not utf-8]
[Finished in 0.2s with exit code 1]
I though it was because I didn't save it to UTF-8, but when I save with UTF-8 it gives me the same error. I tried to compile it using IdeOne, it compiles fine, but it gives Time limit exceeded, but this is because of the code. Here is my code:
#include <stdio.h>
int main(void) {
int x, d;
for(x=1; ; x++){
for(d = 2; d <= 20; d++){
if(x%d != 0){
break;
}
}
if(d == 21){
break;
}
}
printf("%d", x);
return 0;
}
It may seem it has an infinite loop but it didn't.
Your program is fine but it takes a while (2.3s on my computer, yours might be slower) to terminate which is the reason why IdeOne complains. The first x to satisfy your test is 232792560.
See:
https://gist.github.com/clijiac/2814161
SASS on Sublime Text 3 - [Decode error - output not utf-8]
Sublime Text 3, Python 3 and UTF-8 don't like each other
https://www.sublimetext.com/forum/viewtopic.php?f=3&t=7809
A possibility is that it might be missing the directory of the compiler in the PATH variable. So, to do that, go to:
Control Panel > System and Security > System > Advanced system settings
Click on "Ambient Variables" and add the path on the system variable "PATH".

Resources