Floating point number to 1 decimal place

MrChips

Joined Oct 2, 2009
30,824
A character string is NUL terminated.

An array of binary values has no terminator. Entries may be any value within the range of the data type chosen.
 

djsfantasi

Joined Apr 11, 2010
9,163
I'll have a go when I get home.

What would happen if you actually wanted an array of numbers. If one of the numbers stored in the array way 0, would the C routine that has the: while(*characters) nit think that it was the end of the array when it got to a number 0 which you may actually want stored in the array.
No! The character for the number 0 and the nul character are two different values.

The character for the number 0 is represented by 0x30.
The nul character is represented by 0x00.

Hence, it is easy for the C program to tell the difference.

The number 100.0 would be represented in your code as:
Rich (BB code):
str[0]=0x31;
str[1]=0x30;
str[2]=0x30;
str[3]=0x2E;
str[4]=0x30;
str[5]=0x00;
Play computer and visually scan through the characters in this string. You can easily see the end of the string without confusing it with the character '0'.

By the way, I like The_RB's suggestion. It makes for more readable code. I didn't use it in my example because I wanted to highlight the difference in ASCII values for '0' and nul.
 

MrChips

Joined Oct 2, 2009
30,824
There are a number of fundamental lessons to learn here.
Hope you paid attention to all the details along the way.

str[5] = 0;
str[5] = 0x00;

produce the same thing.

str[4] = '0';
str[4] = 0x30;
str[4] = 48;

produce the same thing.

str[3] = '.';
str[3] = 0x2E;
str[3] = 46;

produce the same thing.

&str[0] is the address of str

char str[6]; // declares the length of the array to have 6 bytes. This includes the terminating NUL (zero byte).

'A' is a character
"A" is a character string
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
Thanks again.

I've been programming assembly language and Objective C for the mac for a few years now, and now that I've moved over to C on the PIC, I'm finding it a little strange how stuff is donr. I know Objective C is derived from C, but it is such a more high level object orientated version.
 
Top