# how to print "°C" within sprintf?

**URL:** <https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139>\
**Category:** Programming\
**Created:** [March 26, 2016, 3:49pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139 "2016-03-26T15:49:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [March 26, 2016, 3:49pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/1 "2016-03-26T15:49:00Z")

</div>

hi,  
I tried

```arduino
sprintf(txt,"%d °C", temp);
Serial.print(txt);

```

to print a temperature.  
But I get

> 15Â°

In the forum here I saw tips how to print "°" with Serial.print(), but I need it within sprintf.

What to do?

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [March 26, 2016, 3:53pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/2 "2016-03-26T15:53:34Z")

</div>

I suspect _txt_ is too small.

[http://snippets-r-us.com/](http://snippets-r-us.com/)

---

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [March 26, 2016, 4:14pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/3 "2016-03-26T16:14:25Z")

</div>

sorry, I missed one letter.  
The output is e.g.

> 15Â°C

The problem is the letter Â after 15.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [March 26, 2016, 4:38pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/4 "2016-03-26T16:38:06Z")

</div>

The problem is not how to print it, the problem is how you read it 😕

The "degree sign" character ° is not a normal readable character.  
For ASCII it is the extended ASCII character 248.  
For UTF-8 is it U+00B0, which is 0xC2, 0xB0.

When the 0xC2, 0xB0 are displayed as ASCII you get weird characters.

Even when everyone would use UTF-8, there are still problems. In Windows a text file with UTF-8 has often a 'BOM' identifier as the first bytes to indicate that it is a UTF-8 text file. But other operating system don't support that.  
That's not all, there is also ANSI and Unicode.

The Arduino IDE used UTF-8, but that is not completely supported and is different for Windows or linux. The serial monitor in Windows might not use UTF-8 yet.  
Why do you need the "degree sign" in the serial monitor ? Is it that important ? If you fix it, it might not work when the serial monitor is in an other mode or with another serial terminal program.

For a webpage, it is well defined. The "degree sign" in HTML is `&deg;`

For a LCD display it is also reliable. Create your own ° sign, for example as code 1. Print text to the lcd display with: `"25 \1C`

---

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [March 26, 2016, 4:44pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/5 "2016-03-26T16:44:22Z")

</div>

Principally I have a solution, but...

The new code is

```arduino
sprintf(txt,"%d%cC", temp, (char)248);
Serial.print(txt);

```

According to my ASCII table 248 should be °, but I get

> 15øC

The Arduino IDE seems to use a different ASCII table.

Does anybody know the wright code, instead of 248 what?

---

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [March 26, 2016, 4:50pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/6 "2016-03-26T16:50:53Z")

</div>

well, on the SerialMonitor it looks somewhat curiously.  
But what matters.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [March 26, 2016, 4:56pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/7 "2016-03-26T16:56:04Z")

</div>

We prefer that you give a test sketch.  
In linux I see UTF-8 characters in the serial monitor. If you give a test sketch, I could try it in Windows if you want.  
Perhaps you should give a screendump of what you see, since the character could be different after copying to this forum.

---

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [March 28, 2016, 9:18pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/8 "2016-03-28T21:18:43Z")

</div>

hi,  
the complete sketch is quite long and would need some special hardware. But the part where the problem comes up is here:

```cpp
char wdat[24];
    char wstr[8];
... // here is code to read temperature data from an external sensor
... //  and some Serial.print() the printout of which can be seen on the screen shot too
... // vf is a float value for temperature and rh an integer for relative humidity
      dtostrf(vf, 4, 1,wstr);
      sprintf(wdat," -> %s°C %d%%",wstr, rh);
      Serial.print(wdat);

```

A screenshot is attached as a png file. The relevant part is marked.

![ScreenShot.png](https://europe1.discourse-cdn.com/arduino/original/4X/e/e/0/ee0dd850782f76ed29f9f7c1c8509d6d63620686.png)

---

<div class="post-metadata">

**Author:** ![dev\_1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dev_1/32/215847_2.png) [@dev\_1](https://forum.arduino.cc/u/dev_1)\
**Post date:** [March 28, 2016, 9:22pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/9 "2016-03-28T21:22:42Z")

</div>

For our convenience:

![a73e7ce0ffd771abcd42aa3c6a7aa8eb3896413c.png](https://europe1.discourse-cdn.com/arduino/original/4X/e/e/0/ee0dd850782f76ed29f9f7c1c8509d6d63620686.png)  
(Embedding images described [here](http://forum.arduino.cc/index.php?topic=364156.0).)

To avoid any UTF-8 monkey business with the editor and/or IDE, I think you should try this,:

```cpp
      sprintf(wdat," -> %s\xF8C %d%%",wstr, rh);

```

That uses the "\x" hexadecimal escaping in a double-quoted string. Described [here](http://stackoverflow.com/questions/10220401/c-string-literals-escape-character), for example. That should work on your LCD, if (char)248 is indeed the degree sign. In the Serial Monitor window, `(char)248` will look like you described above: ø. It's just the difference between the Serial Monitor and your LCD.

Cheers,  
/dev

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [March 28, 2016, 9:41pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/10 "2016-03-28T21:41:53Z")

</div>

```cpp
  sprintf(wdat," -> %s \xB0 C %d%%",wstr, rh);

```

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [March 28, 2016, 9:46pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/11 "2016-03-28T21:46:35Z")

</div>

Hello,

There was [this workaround](http://forum.arduino.cc/index.php?topic=276477.msg1945984#msg1945984), unfortunately not working anymore since recents IDE updates... ☹ If you use older IDE like 1.0.X it should work.

And yes, "\xB0" will work too.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [March 28, 2016, 9:48pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/12 "2016-03-28T21:48:31Z")

</div>

In your Reply #7, when I copy that part of the sketch into the Arduino IDE, and save the file, and open it in a binary editor, it says : C2 B0. That is the same as I wrote before.

This webpage has the tag :   
That means the code C2 B0 in your Reply #7 reads as a ° when it is viewed in a browser, as you are doing this very moment.

The screenshot is from a program we don't know. But that obvious does not use UTF-8. It might use ASCII, but I don't know if it supports extended ASCII.  
I thougth it was code 248 in extended ascii, be Coding Badly says it is 0xB0 which is 176. I was wrong, it is indeed 176.  
If you want me to try that in Windows, please give a short test sketch.

My conclusion: we can not answer your question. It depends on the character encoding that the program uses which is used to view that data.

As I wrote before, it is well defined for a webpage, and no problem with a LCD display. But the degrees sign in a text file, that gets messy, and that is what you have encountered.

I suppose the extended ascii 176, 0xB0, 'xB0' will work. But what if the program to view that data is updated and no longer supports extended ascii ? Or another program is used to view that data which uses UTF-8 ?

---

<div class="post-metadata">

**Author:** ![oqibidipo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oqibidipo/32/221794_2.png) [@oqibidipo](https://forum.arduino.cc/u/oqibidipo)\
**Post date:** [March 28, 2016, 10:14pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/13 "2016-03-28T22:14:28Z")

</div>

> Koepel:  
> I thougth it was code 248 in extended ascii, be Coding Badly says it is 0xB0 which is 176. I was wrong, it is indeed 176.

"Extended ASCII" is such an ambiguous term...  
248 is degree in CP437 ([Wikipedia](http://url=https://en.wikipedia.org/wiki/Code_page_437)) and other IBM PC / MS-DOS code pages.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [March 28, 2016, 10:39pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/14 "2016-03-28T22:39:06Z")

</div>

Thank you oqibidipo 😕  
SupArdu, we could go on with this forever. Do you understand that one problem is how to write the data, and another problem is how it is printed/viewed ? Let us know what you have deciced.

---

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [March 28, 2016, 11:21pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/15 "2016-03-28T23:21:40Z")

</div>

thank you for so many tips 🙂

" -\> %s\xB0 C " works. The printout is: 18.4° C

But " -\> %s\xB0C " does not work. The printout then is: 18.4

The blank between \xB0 and C is necessary! And this is acceptable.

Nevertheless I will ask the supplier of the display. It is a MI0283Qt9.

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [March 28, 2016, 11:51pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/16 "2016-03-28T23:51:10Z")

</div>

You can solve it like this:

```cpp
"-> %s\xB0""C"

```

---

<div class="post-metadata">

**Author:** ![PaulMurrayCbr](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@PaulMurrayCbr](https://forum.arduino.cc/u/PaulMurrayCbr)\
**Post date:** [March 29, 2016, 1:06pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/17 "2016-03-29T13:06:27Z")

</div>

(tl;dr? Skip to the bottom of this post.)

sprintf, at the end of the day, produces a series of bytes. Numbers from 0-255. So why do we see letters on our serial monitor? Because the serial monitor displays those nubers as characters. How does it decide which numbers correspond to which characters?

Well, it's complicated.

For numbers from 32-126, it uses ASCII: the American Standard Code for Information Interchange. This was originally meant for teletype machines, and these days it is pretty ubiquitous. It has mostly replaced other standards such as EBCDIC.

But you'll notice that ASCII doesn't define anything above 128. Instead, they used bit 7 as a parity bit. But inside devices where noise in the transmission lines isn't such a problem, different platforms used these numbers in different ways. IBM PCs had a set of useful accented characters, and other things like characters with which you could draw boxes.

This evolved, in MS-DOS and the earlier versions of Windows, to sets of "code pages" for different languages. You would specify a code page on whatever device you are outputting to (a printer, a textbox) and the 128-256 character range would have a map of suitable characters for that language.

After many years of shenanigans, the computing world bit the bullet and developed a big set of all characters ever called Unicode. The price of Unicode is that all characters ever are described by a 16-bit number. (But we still have code pages for Chinese, in particular).

To cope with this new world, one needs to grok that a byte is not a character, and a character is not a byte. Rather, a stream or sequence of characters is encoded into a series of bytes.

The 'char = a machine byte' in the C programming language is a hangover of simpler times, and is not really the case anymore when we are talking about text. The Unicode standard never talks about bytes or chars, it is defined explicitly in terms of 'octets' - packets of exactly 8 bits. But for us (and most people), that's a byte, or a C char.

We have encodings like ASCII, which explicitly only has the original 127; we have various ISO encodings, which basically are the old MS-DOS code pages rebranded; we have UTF-16, where we just print out the full two-byte value for each character; and we have things like JS-16 and UTF-8, which handle the whole unicode character set efficiently and preserve the ASCII set of encodings at the price of characters being represented by variable length sequences of bytes.

So. To get a degree sign onto whatever output device you are using, you have to send a series of bytes which that particular output device recognises as being an encoded degree sign.

So there's your first problem: what encoding is your output device? If you are using an LCD, you'll need to look at that LCD's data sheet. If you just want to see it in the arduino serial monitor, well - the serial monitor is implemented in java. It might be using the default encoding on whatever platform it is running on (eg: Windows-1252), or it might be using UTF-8.

Let's assume UTF-8. A unicode degree sign is character 0xB0. Assuming that our serial monitor talks UTF-8, you need to output a UTF-8 encoded U+00B0 to the serial port. Googling 'UTF-8 calculator' sends me to [a page](http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=00B0&mode=hex) that tells me that the UTF-8 encoding for a unicode 00B0 is C2 B0.

Your next problem is explaining to sprintf that that's what you want to send. Embedding the codes in the string is awkward because 'C' is a hexadecimal digit, so you are probably better off just doing it as characters:

```cpp
sprintf(txt,"%d %c%cC", temp, 0xC2, 0xB0);

```

My sketch is

```cpp
void setup() {
  // put your setup code here, to run once:

  Serial.begin(57600);
  while(!Serial);
}

void loop() {
  static int temp = 0;
  char txt[256];
  sprintf(txt,"%d %c%cC\n", temp++, 0xC2, 0xB0);
  Serial.write(txt);
  delay(500);
}

```

And the output on the IDE serial monitor on my Mac is

```cpp
0 °C
1 °C
2 °C
3 °C
4 °C
5 °C
6 °C
7 °C
8 °C
9 °C
10 °C
11 °C

```

YMMV, particularly if you want this to got to an LCD or other output device, or if you are running on a widows box an the serial monitor uses the windows default encoding. There ought to be a way to specify the encoding in the IDE, but I can't find it.

EDIT: added this [in the suggestions subforum](http://forum.arduino.cc/index.php?topic=389891).

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [March 29, 2016, 1:42pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/18 "2016-03-29T13:42:03Z")

</div>

I recently dealt with this myself. On any keyboard with a number pad, you can type these \>128 characters by holding down the ALT key and pressing the numbers. ALT-167 is the degree symbol.

Type it in, upload the program and it will work. Unfortunately when you save the program, the IDE uses UTF-8 so it stores the character with two bytes. When you open that saved program, it looks the same but there are now two bytes in that string, representing that one printable character.

I haven't tested fully, but it seems like the serial monitor doesn't use UTF-8 so it will display the incorrect characters.

```cpp
  Serial.write(167); //write degree char

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:48pm UTC](https://forum.arduino.cc/t/how-to-print-c-within-sprintf/375139/19 "2021-05-05T21:48:19Z")

</div>

Closed on May 5, 2021, 9:48pm UTC


