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
    30
    0
    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
     
  2. AlbertHall

    Distinguished Member

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

    Moderator

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

    Moderator

    Mar 31, 2012
    19,493
    5,365
    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

    Member

    Dec 2, 2013
    472
    60
    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
     
  6. WBahn

    Moderator

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

    Member

    Dec 2, 2013
    472
    60
    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
     
  8. WBahn

    Moderator

    Mar 31, 2012
    19,493
    5,365
    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

    Moderator

    Jun 26, 2012
    2,715
    1,257
    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 at 1:08 PM
  10. Picbuster

    Member

    Dec 2, 2013
    472
    60
    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
    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
    Picbuster

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

    Moderator

    Mar 31, 2012
    19,493
    5,365
    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.
     
Loading...