开发者

how to print the microsecond symbol in C?

I am trying to print the microsecond symbol in C, but I don't get any data in the output.

printf("Micro second = \230");

I also tried using int i 开发者_如何学运维= 230;

printf("Character %c", i);

but in vain! Any pointers?


That depends entirely on the character encoding used by the console you're using. If you're using Linux or Mac OS X, then most likely that encoding is UTF-8. The UTF-8 encoding for µ (Unicode code point U+00B5) is 'C2 B5' (two bytes), so you can print it like this:

printf("Micro second = \xC2\xB5");  // UTF-8

If you're on Windows, then the default encoding used by the console is code page 437, then µ is encoded as 0xE6, so you would have to do this:

printf("Micro second = \xE6");  // Windows, assuming CP 437


Here is the standards-sanctioned way to print it in C:

printf("%lc", L'\u00b5');

If you're happy assuming UTF-8, though, I'd just hard-code "µ".


Since you work on Mac OS, you can rest assured that the terminal uses UTF-8. Therefore, bring up the characters palette (Edit -> Special characters...), find the microsecond symbol there, and put it right into your string.

int main()
{
    printf("µs\n");
}

It will work as long as your source file is UTF-8 too. Otherwise, you'll need to find the code point for it (which should also be indicated in the characters palette). Mouse over the character to find its UTF-8 value (and excuse my French system):

how to print the microsecond symbol in C?

This means you can use printf("\xc2\xb5") as an encoding-independant replacement for the character itself.


  printf("%c", 230);

works for me. I think this is OS dependent though. It is encoding dependent (thanks zneak). I'm on Windows.


I had the same issue, I found the solution based on Code page 437

"\xB5"

I just included that code into a string and it worked for me.

My environnement: Windows, C++. I was trying to convert a string to display in SFML.


Using a mix of C and C++14 for a work project, here's what worked for me:

printf("Micro second symbol = \xC2\xB5");

\xB5, and \230 produced ▒ on my output. My project equipment is apparently is picky as I tried it with elsewhere with some online IDE's and a version of Xcode on Mac, all worked fine.

Could be if you're trying to print elsewhere besides console, say like to an LCD screen, then you're screen would have to be programmed (created and mapped to xyz...) to include that character. Though from my experience that varies wildly depending on what you're creating.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜