Combining a two digit number in c

Discussion in 'Programmer's Corner' started by emoney123, Jul 10, 2017.

  1. emoney123

    Thread Starter Active Member

    Jun 4, 2012

    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?
  2. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    (Var1 x 10) + Var2.
  3. MrChips


    Oct 2, 2009
    Gee. I wonder how did emoney learn to count?
  4. WBahn


    Mar 31, 2012
    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;
  5. Picbuster

    Active Member

    Dec 2, 2013
    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.

  6. WBahn


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

    Active Member

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

  8. WBahn


    Mar 31, 2012
    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 likes this.
  9. JohnInTX


    Jun 26, 2012
    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?
    Last edited: Jul 17, 2017
  10. Picbuster

    Active Member

    Dec 2, 2013
    Part of xc8 manual
    #include <stdlib.h>
    int atoi (const char * s)
    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.
    Code (C):
    1. #include <stdlib.h>
    2. #include <stdio.h>
    3. void
    4. main (void)
    5. {
    6. char buf[80];
    7. int i;
    8. gets(buf);
    9. i = atoi(buf);
    10. printf(“Read %s: converted to %d\n”, buf, i);
    11. }
    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

    Mod edit: code tags
    Last edited by a moderator: Jul 17, 2017
  11. WBahn


    Mar 31, 2012
    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.