How to print root symbol with c in a file
ascii, c, file-io
Solution
The problem you are encountering is because you are attempting to use part of the extended ASCII set (ie: characters above 127 in value). The code page is something you set so that if the MSB of an 8-bit ASCII symbol is set, it can map to one of many different code pages depending on region/locale, OS, etc (ie: Greek, Latin, etc). In most cases, ASCII characters are generally considered to be 7-bit, ignoring the code page enable bit.
Attempting to use extended ASCII is not a portable approach, so your best alternative is to:
- Make use of unicode
- Make sure your C compiler is C99 compliant.
The following example resolves the original problem.
Source Code
#include <stdio.h>
void main(void) {
FILE *fptr;
fptr = fopen("output.txt","w+"); //open the file to write
fprintf(fptr, "\u221A\n");
fclose(fptr);
}
Output from Sample Run
√
References
- How to print Extended ASCII characters 127 to 160 in through a C program?, Accessed 2014-04-16, `<https://stackoverflow.com/questions/16359225/how-to-print-extended-ascii-characters-127-to-160-in-through-a-c-program>`
- Unicode Character 'SQUARE ROOT' (U+221A), Accessed 2014-04-16, `<http://www.fileformat.info/info/unicode/char/221a/index.htm>`
Problem
I have a file named output.txt and i want to print in a root (√) symbol in it. Here is my program ``` #include<stdio.h> #include<conio.h> void main(void) { FILE *fptr; fptr = fopen("output.txt","w+"); //open the file to write fprintf(fptr,"\xfb"); // \xfb is the hexadecimal code for root symbol fclose(fptr); } ``` but when i run the program (û) is printed in it