Combining a two digit number in c

Thread Starter

emoney123

Joined Jun 4, 2012
32
Hello,

I am attempting to combine a two digit number in c but I am not sure how exactly to do it. Each digit of the number is picked individually and I would like the end result to be the 2 digit representation of that number. For example, the final number is 23... first the 2 is picked and stored in a variable and then the 3 is picked and stored in another variable. How would one do this for a combined answer of 23?
Thanks
 

WBahn

Joined Mar 31, 2012
29,976
Hello,

I am attempting to combine a two digit number in c but I am not sure how exactly to do it. Each digit of the number is picked individually and I would like the end result to be the 2 digit representation of that number. For example, the final number is 23... first the 2 is picked and stored in a variable and then the 3 is picked and stored in another variable. How would one do this for a combined answer of 23?
Thanks
Thank back to second grade (or so) when you learned about "place value".

What does the string of digits "2" followed by "3" mean?

It means

value = (2 x 10) + 3

If the value 2 is stored in x and the value 3 is stored in y, then you have

value = 2*x + y;
 

Picbuster

Joined Dec 2, 2013
1,047
Hello,

I am attempting to combine a two digit number in c but I am not sure how exactly to do it. Each digit of the number is picked individually and I would like the end result to be the 2 digit representation of that number. For example, the final number is 23... first the 2 is picked and stored in a variable and then the 3 is picked and stored in another variable. How would one do this for a combined answer of 23?
Thanks
simple way
int array[3];
int number;
fill each element of array wit a number
number = array[0] *1 + array[1] *10+ array[2]*100 place low order @ array[0]
or use
number = atoi(array); check compiler some do need an other format.

Picbuster
 

WBahn

Joined Mar 31, 2012
29,976
simple way
int array[3];
int number;
fill each element of array wit a number
number = array[0] *1 + array[1] *10+ array[2]*100 place low order @ array[0]
or use
number = atoi(array); check compiler some do need an other format.

Picbuster
To use atoi() the array must be a NUL-terminated ASCII string with the most-significant digit in array[0].
 

Picbuster

Joined Dec 2, 2013
1,047
if you make an array[n] it's is terminated.
try
int array[3];
int res;
array[0]=65;
array[1]=66;
array[2]=67;
printf("%s",array);
res = atoi(array);
printf("%d",res);
But.... another compiler might act different I use xc8
but you can always invoke a '\0" (int array[4] array[3] ="\0";

Picbuster
 

WBahn

Joined Mar 31, 2012
29,976
if you make an array[n] it's is terminated.
try
int array[3];
int res;
array[0]=65;
array[1]=66;
array[2]=67;
printf("%s",array);
res = atoi(array);
printf("%d",res);
But.... another compiler might act different I use xc8
but you can always invoke a '\0" (int array[4] array[3] ="\0";

Picbuster
You have three problems.

First, treating it as a string, the printf() function must read past the array bounds even if it were properly terminated. That memory location may belong to another variable or may not be accessible.

Second, it is NOT properly terminated. If it behaves like it is, that is by pure coincidence that the contents of the next memory location just happens to be zero.

Third, a character array needs to be an array of bytes. You are using an array of type int. If int is a multibyte data type, then you have a bunch of bytes that are zero in the middle of your string. By the C standard, an int is supposed to be at least two bytes, but it is also intended to be the natural width of the hardware. So if the compiler is targeting an 8-bit architecture, it may well violate the standard and use a one-byte int.

What do you get if you execute

printf("%i\n"", sizeof(int));
 

JohnInTX

Joined Jun 26, 2012
4,787
You have three problems.

First, treating it as a string, the printf() function must read past the array bounds even if it were properly terminated. That memory location may belong to another variable or may not be accessible. To use printf on an unterminated array you can use the field length specifiers. That takes more code.

Second, it is NOT properly terminated. If it behaves like it is, that is by pure coincidence that the contents of the next memory location just happens to be zero.

Third, a character array needs to be an array of bytes. You are using an array of type int. If int is a multibyte data type, then you have a bunch of bytes that are zero in the middle of your string. By the C standard, an int is supposed to be at least two bytes, but it is also intended to be the natural width of the hardware. So if the compiler is targeting an 8-bit architecture, it may well violate the standard and use a one-byte int.
I can confirm that WBahn is absolutely correct here. Specifically:
1) The array as declared and loaded is not NUL terminated.
2) The standard XC8 C startup inits variable space to 0 per C standard and the only reason it is working is that the byte following the last int is 0.
3) XC8 ints are 16bits.

Compiling @Picbuster 's code gets a pointer type conflict at res = atoi(array) because atoi wants a pointer to type char.

The attached file is a section of the data memory in question. 'array' starts at 003dh and is initialized with 'A' 'B' 'C' (decimal ASCII 65, 66, 67) highlighted in yellow. You can see that ints are 16 bits and the NUL termination is a lucky side effect of the C startup code, highlighted in red.

Is the TS still around?
 

Attachments

Last edited:

Picbuster

Joined Dec 2, 2013
1,047
Part of xc8 manual
//----------------
ATOI
Synopsis
#include <stdlib.h>
int atoi (const char * s)
Description
The atoi() function scans the character string passed to it, skipping leading blanks
and reading an optional sign. It then converts an ASCII representation of a decimal
number to an integer.
Example
C:
#include <stdlib.h>
#include <stdio.h>
void
main (void)
{
char buf[80];
int i;
gets(buf);
i = atoi(buf);
printf(“Read %s: converted to %d\n”, buf, i);
}
See Also
xtoi(), atof(), atol()
Return Value
A signed integer. If no number is found in the string, 0 will be returned
//------------- end of manual part
Picbuster

Mod edit: code tags
 
Last edited by a moderator:

WBahn

Joined Mar 31, 2012
29,976
So? The manual is telling you just what we've been saying.

atoi() needs a pointer to a character string -- NOT a pointer to an int array.

If your string is not properly NUL terminated, the atoi() MIGHT not cause a bounds violation -- PROVIDED it find a character within the string bounds that cannot be interpreted as extending the legal representation of an integer. But if all of the characters in a non-NUL-terminated string are part of a legal integer representation, then atoi() WILL result in an array bounds violation -- whether that results in undesired behavior is undefined, because you have chosen to enter the world of undefined behavior.

In that world, anything is legal. It can cause your program to crash, it can start a global thermonuclear war, or it can do exactly what you wanted it to do. All are equally valid outcomes.
 
Top