How to show Bengali Language in Codeblocks - c

I am facing a problem to show Bengali language output in codeblocks . I want to write Bengali language .I know it can be done with the help of unicode . The unicode of "ঈ" is 2440 . So I write the following small program .
#include<stdio.h>
int main()
{
int i;
char ch = 2440;
printf("%c",ch);
return 0;
}
But the above program does not show "ঈ" . Why ? What should I do in order to show Bengali language in codeblocks .Plz guyz help me to solve this problem .

When you use char, it is stored in 1 byte, and thus can only store up-to 256 (or -127 - 128). This means that 2440 will get truncated, and that's one reason why it is not working.
Instead of printf and char you need to use functions and types from wchar.h, specifically wchar_t and something like wprintf .
wprintf (L"Character: %lc %lc \n", L'ঈ', 2440);
p.s. I realise they are c++ based resources, but they are talking about the C library, and they should work regardless.

Related

Use the letter ñ in C

I have to save in a char[] the letter ñ and I'm not being able to do it. I tried doing this:
char example[1];
example[0] = 'ñ';
When compiling I get this:
$ gcc example.c
error: character too large for enclosing
character literal type
example[0] = 'ñ';
Does anyone know how to do this?
If you're using High Sierra, you are presumably using a Mac running macOS 10.13.3 (High Sierra), the same as me.
This comes down to code sets and locales — and can get tricky. Mac terminals use UTF-8 by default and ñ is Unicode character U+00F1, which requires two bytes, 0xC3 and 0xB1, to represent it in UTF-8. And the compiler is letting you know that one byte isn't big enough to hold two bytes of data. (In the single-byte code sets such as ISO 8859-1 or 8859-15, ñ has character code 0xF1 — 0xF1 and U+00F1 are similar, and this is not a coincidence; Unicode code points U+0000 to U+00FF are the same as in ISO 8859-1. ISO 8859-15 is a more modern variant of 8859-1, with the Euro symbol € and 7 other variations from 8859-1.)
Another option is to change the character set that your terminal works with; you need to adapt your code to suit the code set that the terminal uses.
You can work around this by using wchar_t:
#include <wchar.h>
void function(void);
void function(void)
{
wchar_t example[1];
example[0] = L'ñ';
putwchar(example[0]);
putwchar(L'\n');
}
#include <locale.h>
int main(void)
{
setlocale(LC_ALL, "");
function();
return 0;
}
This compiles; if you omit the call to setlocale(LC_ALL, "");, it doesn't work as I want (it generates just octal byte \361 (aka 0xF1) and a newline, which generates a ? on the terminal), whereas with setlocale(), it generates two bytes (\303\261 in octal, aka 0xC3 and 0xB1) and you see ñ on the console output.
You can use "extended ascii". This chart shows that 'ñ' can be represented in extended ascii as 164.
example[0] = (char)164;
You can print this character just like any other character
putchar(example[0]);
As noted in the comments above, this will depend on your environment. It might work on your machine but not another one.
The better answer is to use unicode, for example:
wchar_t example = '\u00F1';
This really depends on which character set / locale you will be using. If you want to hardcode this as a latin1 character, this example program does that:
#include <cstdio>
int main() {
char example[2] = {'\xF1'};
printf("%s", example);
return 0;
}
This, however, results in this output on my system that uses UTF-8:
$ ./a.out
�
So if you want to use non-ascii strings, I'd recommend not representing them as char arrays directly. If you really need to use char directly, the UTF-8 sequence for ñ is two chars wide, and can be written as such (again with a terminating '\0' for good measure):
char s[3] = {"\xC3\xB1"};

Outputting √ symbol in C

Everytime I copy a (√) over from a word document my compiler (DEV C++) turns it into a v
Anyone know how to display a square root symbol in c?
It's for aesthetic purposes (I'm trying to display the quadratic formula and ()^1/2) isn't going to work.
Using windows 7
It is extended ASCII code. Please refer to Ascii & extended ascii chart
http://www.asciitable.com/index/asciifull.gif
http://www.asciitable.com/index/extend.gif
and you can try something like:
char chr = 251; //stores the extended ASCII of a symbol
printf("Character with an ascii code of 251: %c \n", chr);
//tries to print an ASCII symbol...
Your editor must support unicode source files, your source file must be in unicode because √ is not an ASCII symbol. And your output files or streams must be in the same unicode encoding to be able to display that symbol correctly.
On Linux you should be able to achieve that using UTF-8 encoded source files.
Try the following code:
#include <stdio.h>
int main()
{
printf("\xFB");
return 0;
}

What's wrong with Geany?

#include <cini.h>
int main() {
int a ;
a = 21 ;
printf(a);
return 0;
}
Questions :
1) on the toolbar, once I've written the code, the "execute" or "compile" functions on Geany (a C compiler) seem to be disabled
2) what's wrong with my code ?
I'm about to learn C, so please be nice with a rookie.
Geany is an editor, not a compiler. If the compile and execute functions are disabled it could be because it is unable to find your actual compiler (make sure you have one installed), or because you haven't saved your file as C code.
The first argument to printf must be the format string. If you want to print an integer as decimal, you need to use the %d conversion specifier, like this:
printf("%d\n", a);
The \n after the %d prints the linefeed character to the output. You can read about the printf function here.
You need to include at least stdio.h, because printf is declared in that header. The only reference I could find to cini.h was this header here which won't work in a C program because it is C++ code.

How do I print the infinity symbol in C using printf [duplicate]

This question already has answers here:
Showing characters in extended ASCII code (Ubuntu)
(3 answers)
Closed 9 years ago.
I tried the following
printf ("%c", 236); //236 is the ASCII value for infinity
But I am just getting garbage output on the screen.
printf was working correctly for ASCII values less than 128. So I tried the following
printf ("%c", 236u); //unsigned int 236
Still I am just getting garbage only. So, what should I do to make printf display ASCII values from 128 to 255.
Like everyone else in the comments already mentioned, you would not be able to reliably print characters after 127 (and assuming it as ASCII) since ASCII is only defined upto 127. Also the output you see very much depends on the terminal settings (i.e. which locale it is configured to).
If you're fine using UTF-8 to print, you could give wprintf a try as shown below:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main()
{
setlocale( LC_ALL, "en_US.UTF-8" );
wprintf (L"%lc\n", 8734);
return 0;
}
It would produce the following output:
∞
8734 (or 0x221E) is the equivalent of the UTF-8 UNICODE character for the symbol ∞.
Standard C does not have a symbol for infinite. That's for your implementation (eg. your compiler, your operating system, your terminal and your hardware) to define. Consider that C was designed with portability for systems that use non-ASCII character sets in mind (eg. EBCDIC).

Looking for UTF8-aware formatting functions like printf(), etc

I discovered an interesting problem when processing UTF-8 strings containing non-ASCII chars with C standard library formatting functions like sprintf():
The functions of the printf() family are not aware of utf-8 and process everything based on the number of bytes, not chars. Therefore the formatting is incorrect.
Simple example:
#include <stdio.h>
int main(int argc, char *argv[])
{
const char* testMsg = "Tääääßt";
char buf[1024];
int len;
sprintf(buf, "|%7.7s|", testMsg);
len = strlen(buf);
printf("Result=\"%s\", len=%d", buf, len);
return 0;
}
The result is:
Result="|Täää|", len=7
Most probably some of you will recommand to convert the application from char to wchar_t and use fwprintf(), etc., but that's absolutely impossible because of huge existing applications. I could imagine writing a wrapper that uses these functions internally, but this would be tricky and very inefficient.
So the best solution would be a UTF-8-aware replacement for the formatting functions of the Standard C Library.
Currently I'm working on QNX 6.4, but replies for other operating systems. e.g. Linux, are also very welcome.
Well, once you ask printf to do intelligent padding of Unicode characters, you run into major problems. As they say,
w͢͢͝h͡o͢͡ ̸͢k̵͟n̴͘ǫw̸̛s͘ ̀́w͘͢ḩ̵a҉̡͢t ̧̕h́o̵r͏̵rors̡ ̶͡͠lį̶e͟͟ ̶͝in͢ ͏t̕h̷̡͟e ͟͟d̛a͜r̕͡k̢̨ ͡h̴e͏a̷̢̡rt́͏ ̴̷͠ò̵̶f̸ u̧͘ní̛͜c͢͏o̷͏d̸͢e̡͝?͞
How many Unicode characters are in Tääääßt? Well, it could be anywhere from 7 to 11, depending on how it's encoded. Each ä can be written as U+00E4, which is one character, or it could be written as U+0061 U+0308, which is two characters. So your next hope is to count grapheme clusters. (No, normalization won't make the problem go away.)
But, how wide is a grapheme cluster? Obviously, a is one column wide. U+200B should be zero columns wide, it's a "zero-width" space. Should each ひらがな be two columns wide? They usually are in terminal emulators. What happens when you format ひらがな as 7 columns, do you get "ひらが ", which adds a space, or do you get "ひらが", which is only 6 columns?
If you cut something up which mixes RTL and LTR text, should you reset the text direction afterwards? What are you going to do? (Some terminal emulators, such as Apple's, support a mixture of left-to-right and right-to-left text.)
What is your goal by truncating text? Are you trying to show the user a string in limited space, or are you trying to write a format that uses fixed-width fields?
Basically, if you want to cut Unicode text into chunks, you shouldn't be doing it with something as simple as printf (or wprintf, which is quite possibly worse). Use LibICU (website) to iterate over the breaks you want. Writing a UTF-8 aware version of printf is asking for all sorts of trouble that you don't want.
The following C99 code snippet defines the function u8printf where format specifiers such as %10s yield 10 utf-8 code points, that is characters rather than bytes. Don't forget to set the locale with setlocale(LC_ALL,"") somewhere before this routine is called. This works because the wprintf uses wchar_t internally. You can define u8fprintf and u8sprintf in a similar way. If you want to write this without C99 variable length arrays than a suitable combination of malloc/free is also possible.
int u8printf(char *fmt,...){
va_list ap;
va_start(ap,fmt);
int n=mbstowcs(0,fmt,0);
if(n==-1) return -1;
wchar_t wfmt[n+1];
mbstowcs(wfmt,fmt,n+1);
for(int m=128;m<=32768;m*=2){
wchar_t wbuf[m];
int r=vswprintf(wbuf,m,wfmt,ap);
if(r!=-1) {
char buf[m*4];
wcstombs(buf,wbuf,m*4);
fputs(buf,stdout);
return r;
}
}
return -1;
va_end(ap);
}

Resources