Outputting √ symbol in C - 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;
}

Related

How do I read from a file in C if the file has accented chararcters such as 'á'?

Another day, another problem with strings in C. Let's say I have a text file named fileR.txt and I want to print its contents. The file goes like this:
Letter á
Letter b
Letter c
Letter ê
I would like to read it and show it on the screen, so I tried the following code:
#include <stdlib.h>
#include <locale.h>
#include <clocale>
#include <stdio.h>
#include <conio.h>
#include <wchar.h>
int main()
{
FILE *pF;
char line[512]; // Current line
setlocale(LC_ALL, "");
pF = fopen("Aulas\\source\\fileR.txt", "r");
while (!feof(pF))
{
fgets(line, 512, pF);
fputs(line, stdout);
}
return 0;
}
And the output was:
Letter á
Letter b
Letter c
Letter ê
I then attempted to use wchar_t to do it:
#include <stdlib.h>
#include <locale.h>
#include <clocale>
#include <stdio.h>
#include <conio.h>
#include <wchar.h>
int main()
{
FILE *pF;
wchar_t line[512]; // Current line
setlocale(LC_ALL, "");
pF = fopen("Aulas\\source\\fileR.txt", "r");
while (!feof(pF))
{
fgetws(line, 512, pF);
fputws(line, stdout);
}
return 0;
}
The output was even worse:
Letter ÃLetter b
Letter c
Letter Ã
I have seen people suggesting the use of an unsigned char array, but that simply results in an error, as the stdio functions made for input and output take signed char arrays, and even if i were to write my own funtion to print an array of unsigned chars, I would not know how to be able to read something from a file as unsigned.
So, how can I read and print a file with accented characters in C?
The problem you are having is not in your code, it's in your expectations. A text character is really just a value that has been associated with some form of glyph (symbol). There are different schemes for making this association, generally referred to as encodings. One early and still common encoding is known as ASCII (American Standard Code for Information Interchange). As the name implies it is American English centric. Originally this was a 7 bit encoding (128 values), but later was extended to include other symbols using 8 bits. Other encoding were developed for other languages. This was non-optimal. The Unicode standard was developed to address this. It's a relatively complicated standard designed to include any symbols one might want to encode. Unicode has various schemes that trade off data size for character size, for example UTF7, UTF8, UTF16 and UTF32. Because of this there will not necessarily be a one to one relationship between a byte and a character.
So different character representations have different values and those values can be greater than a single byte. The next problem is that to display the associated glyphs you need to have a system that correctly maps the value to the glyph and is able to display said glyph. A lot of "terminal" applications don't support Unicode by default. They use ASCII or Extended ASCII. It looks like that is what you may be using. The terminal is making the assumption that each byte it needs to display corresponds a single character (which as discussed isn't necessarily true in Unicode).
One thing to try is to redirect your output to a file and use a Unicode aware editor (like notepad++) to view the file using a UTF8 (for example) encoding. You can also hex dump the input file to see how it has been encoded. Sometimes Unicode files are written with BOM (Byte Order Mark) to help identify the Unicode encoding and byte order in play.

C: Display special characters with printf()

I wanted to know how to display special characters with printf().
I'm doing a string conversion program from Text to Code128 (barcode encoding).
For this type of encoding I need to display characters such as Î, Ç, È, Ì.
Example:
string to convert: EPE196000100000002260500004N
expected result: ÌEPEÇ3\ *R 6\ R $ÈNZÎ
printf result typed: ╠EPEÇ3\ *R 6\ R $ÇNZ╬
printf result image: []
EDIT: I only can use C in this program no C++ at all. All the awnsers I've find so far are in C++ not C so I'm asking how to do it with C ^^
I've find it,
#include <locale.h>
int main()
{
setlocale(LC_ALL,"");
printf("%c%c%c%c\n", 'Î', 'Ç', ' È','Ì');
}
Thank you all for your awnsers it helps me a lot!!! :)
If your console is in UTF-8 it is possible just to print UTF-8 hex representation for your symbols. See similar answer for C++ Special Characters on Console
The following line prints heart:
printf("%c%c%c\n", '\xE2', '\x99', '\xA5');
However, since you print '\xCC', '\xC8', '\xCE','\xC7' and you have 4 different symbols it means that the console encoding is some kind of ASCII extension. Probably you have such encoding http://asciiset.com/. In that case you need characters '\x8c', 'x8d'. Unfortunately there are no capital version of those symbols in that encoding. So, you need some other encoding for your console, for example Latin-1, ISO/IEC 8859-1.
For Windows console:
UINT oldcp = GetConsoleOutputCP(); // save current console encoding
SetConsoleOutputCP(1252);
// print in cp1252 (Latin 1) encoding: each byte => one symbol
printf("%c%c%c%c\n", '\xCC', '\xC8', '\xCE','\xC7');
SetConsoleOutputCP(CP_UTF8);
// 3 hex bytes in UTF-8 => one 'heart' symbol
printf("%c%c%c\n", '\xE2', '\x99', '\xA5');
SetConsoleOutputCP(oldcp);
The console font should support Unicode (for example 'Lucida Console'). It can be changed manually in the console properties, since the default font may be 'Raster Fonts'.

How to use symbols of extended ASCII table in C?

I've been tried to print Extended ASCII characters:
http://www.theasciicode.com.ar/
But all those symbols were printed as question-character on the white background ?.
I use the following cycle to print that symbols:
for (i = 0; i <= 30; i++)
printf("%c", 201);
Question: Is there any way to print those Extended ASCII characters or not? Or maybe there is special library for these characters?
OS Linux Ubuntu 13.04, Code::Blocks 12.11 IDE.
It's better to use unicode than extended ASCII, which is non-standard. A thread about printing unicode characters in C :
printing-utf-8-strings-with-printf-wide-vs-multibyte-string-literals
But indeed you need to copy paste unicode characters..
A better way to start:
#include <stdio.h>
int main() {
printf("\u2500\u2501\n");
}
See https://en.wikipedia.org/wiki/Box-drawing_character#Unicode for unicode characters for this extended ASCII style box art..

How to show Bengali Language in Codeblocks

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.

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).

Resources