1

I want to merge two single character constants into one like '1'+'2'='12'.So that I can add an integer value to this single character. I have tried a lot of things like used their ASCII values.Also the substring functions doesn't seem to work in this context.Help!

4 Answers4

1

If you want to compute the number from 2 digit characters, the formula is very simple:

char c1 = '1';
char c2 = '2';
int value = (c1 - '0') * 10 + (c2 - '0');  // value is 12

c1 - '0' evaluates to the number represented by the digit c1. It works because the digits are guaranteed to be consecutive from '0' to '9' in the execution character set.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

There are more 'beautiful' ways to do it, but this ought to be enough for a first attempt.

int value = 0;

if (firstcharacter == '1') value = 10;
if (firstcharacter == '2') value = 20;
/* ... */
if (firstcharacter == '9') value = 90;

if (secondcharacter == '1') value += 1;
/* ... */
if (secondcharacter == '9') value += 9;

printf("value + 42 is %d.\n", value + 42);
pmg
  • 106,608
  • 13
  • 126
  • 198
0

If the goal is to combine two chars which are digits to form an integer value that can be used in arithmetic expressions, then a compound literal can be used to form a string from the chars, and strtol() can be used to convert the string to an integer value. Here is an example that uses the function char_digits_int() to convert a pair of char inputs into an int value. If either of the arguments is not a digit, -1 is returned:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int char_digits_int(char x, char y);

int main(void)
{
    char a = '1';
    char b = '2';

    printf("%c and %c form: %d\n", a, b, char_digits_int(a, b));
    printf("The sum of %c%c and 1 is: %d\n",
           a, b, char_digits_int(a, b) + 1);

    int value = char_digits_int('0', 'x');
    if (value == -1) {
        fprintf(stderr, "Non-digit input\n");
    } else {
        printf("Value: %d\n", value);
    }

    return 0;
}

/* Returns -1 if x or y is not a digit */
int char_digits_int(char x, char y)
{
    int val = -1;

    if (isdigit((unsigned char) x) && isdigit((unsigned char) y)) {
        val = strtol((char []) { x, y, '\0' }, NULL, 10);
    }
    return val;
}

Program output:

1 and 2 form: 12
The sum of 12 and 1 is: 13
Non-digit input
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • for 2 characters ? Anonymous compound literals? The OP asked the **elementary** question, give him answer he will understand. He definitely is the beginer – 0___________ Aug 12 '17 at 08:29
  • 1
    @PeterJ-- compound literals are a fundamental feature of the language that everyone should know about. If OP doesn't know about them yet, now is a good time to learn. – ad absurdum Aug 12 '17 at 08:31
  • If you ask 20 people claiming they know C I bet 10 never heard about them :). And another 7 will struggle to write a correct one without the access to internet. – 0___________ Aug 12 '17 at 08:34
  • 1
    I am right - see the answer chosen - the one OP actually understands – 0___________ Aug 12 '17 at 08:36
0

If your goal is to combine the character constants '1' and '2' to get the value of the multi-character character constant '12', the answer is There is no portable way to do this and you should probably not write code that uses these things anyway.

Look at this answer for a historical perspective on this oddity: https://stackoverflow.com/a/45576458/4593267

It you are still curious (good for you), here is a test case:

#include <limits.h>
#include <stdio.h>

int main(void) {
    if ((('1' << CHAR_BIT) | '2') == '12')
        printf("(('1' << CHAR_BIT) | '2') == '12'\n");
    else
    if ((('2' << CHAR_BIT) | '1') == '12')
        printf("(('2' << CHAR_BIT) | '1') == '12'\n");
    else
        printf("No simple way to get '12' from '1' and '2'\n");
    return 0;
}

On my system, I get these warnings:

multichar.c:5:38: warning: multi-character character constant [-Wmultichar]
    if ((('1' << CHAR_BIT) | '2') == '12')
                                     ^
multichar.c:8:38: warning: multi-character character constant [-Wmultichar]
    if ((('2' << CHAR_BIT) | '1') == '12')
                                     ^
multichar.c:8:11: warning: code will never be executed [-Wunreachable-code]
    if ((('2' << CHAR_BIT) | '1') == '12')
          ^~~
multichar.c:9:9: warning: code will never be executed [-Wunreachable-code]
        printf("(('2' << CHAR_BIT) | '1') == '12'\n");
        ^~~~~~
4 warnings generated.

And this output:

(('1' << CHAR_BIT) | '2') == '12'

But other systems, notably big endian ones, may produce a different output.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Interesting link; is endianness the only implementation-defined aspect of multi-character constants? Also, on a pedantic note, isn't it possible for a character constant, e.g., `'1'`, to have a negative value, leading to UB with left bit-shifts? – ad absurdum Aug 12 '17 at 09:03
  • @DavidBowling: Endianness is the main aspect, but the Standard allows for other implementation-defined choices. Regarding the negative characters, the Standard does mandate that the digits and letters have positive values as confirmed by the responses to this question: https://stackoverflow.com/questions/45123003/do-a-and-0-always-have-positive-values-even-if-char-is-signed – chqrlie Aug 12 '17 at 12:01
  • Thanks for that link; I had forgotten about the caveat for members of the basic execution character set.... – ad absurdum Aug 12 '17 at 12:09