C语言 如何在c中添加char类型整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13344448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add char type integer in c
提问by Ravi
This is the sample code of my program, in which i've to add two string type integer (ex: "23568" and "23674"). So, i was trying with single charaddition.
这是我的程序的示例代码,我必须在其中添加两个字符串类型 integer (ex: "23568" and "23674")。所以,我正在尝试单char加。
char first ='2';
char second ='1';
i was trying like this..
我是这样尝试的..
i=((int)first)+((int)second);
printf("%d",i);
and i'm getting output 99, because, it's adding the ASCIIvalue of both. Anyone please suggest me, what should be the approach to add the char type number in C.
我得到了输出99,因为它添加了两者的ASCII值。任何人都请建议我,在C 中添加字符类型编号的方法应该是什么。
回答by WhozCraig
Since your example has two single chars being added together, you can be confident knowing two things
由于您的示例将两个单个字符加在一起,因此您可以确信知道两件事
- The total will never be more than 18.
- You can avoid any conversions via library calls entirely. The standard requires that '0' through '9' be sequential (in fact it is the only character sequence that is mandated by the standard).
- 总数永远不会超过 18。
- 您可以完全避免通过库调用进行任何转换。该标准要求“0”到“9”是连续的(实际上它是标准规定的唯一字符序列)。
Therefore;
所以;
char a = '2';
char b = '3';
int i = (int)(a-'0') + (int)(b-'0');
will alwayswork. Even in EBCDIC (and if you don't know what that is, consider yourself lucky).
将永远工作。即使在 EBCDIC(如果您不知道那是什么,请认为自己很幸运)。
If your intention is to actually add two numbers of multiple digits each currently in string form ("12345", "54321") then strtol()is your best alternative.
如果您的意图是实际添加两个当前以字符串形式(“12345”、“54321”)表示的多位数,那么strtol()是您最好的选择。
回答by MOHAMED
i=(first-'0')+(second-'0');
No need for casting char to int.
无需将 char 转换为 int。
回答by Peter Miehle
if you want to add the number reprensations of the characters, I would use "(first - '0') + (second - '0');"
如果你想添加字符的数字表示,我会使用“(first - '0') + (second - '0');”
回答by aaronps
The question seemed interesting, I though it would be easier than it is, adding "String numbers" is a little bit tricky (even more with the ugly approach I used).
这个问题看起来很有趣,我虽然它会比它更容易,但添加“字符串数字”有点棘手(我使用的丑陋方法更是如此)。
This code will add two strings of any length, they doesn't need to be of the same length as the adding begins from the back. Your provide both strings, a buffer of enough length and you ensure the strings only contains digits:
此代码将添加任意长度的两个字符串,它们不需要与从后面开始添加的长度相同。您提供两个字符串,一个足够长度的缓冲区,并确保字符串只包含数字:
#include <stdio.h>
#include <string.h>
char * add_string_numbers(char * first, char * second, char * dest, int dest_len)
{
char * res = dest + dest_len - 1;
*res = 0;
if ( ! *first && ! *second )
{
puts("Those numbers are less than nothing");
return 0;
}
int first_len = strlen(first);
int second_len = strlen(second);
if ( ((first_len+2) > dest_len) || ((second_len+2) > dest_len) )
{
puts("Possibly not enough space on destination buffer");
return 0;
}
char *first_back = first+first_len;
char *second_back = second+second_len;
char sum;
char carry = 0;
while ( (first_back > first) || (second_back > second) )
{
sum = ((first_back > first) ? *(--first_back) : '0')
+ ((second_back > second) ? *(--second_back) : '0')
+ carry - '0';
carry = sum > '9';
if ( carry )
{
sum -= 10;
}
if ( sum > '9' )
{
sum = '0';
carry = 1;
}
*(--res) = sum;
}
if ( carry )
{
*(--res) = '1';
}
return res;
}
int main(int argc, char** argv)
{
char * a = "555555555555555555555555555555555555555555555555555555555555555";
char * b = "9999999999999666666666666666666666666666666666666666666666666666666666666666";
char r[100] = {0};
char * res = add_string_numbers(a,b,r,sizeof(r));
printf("%s + %s = %s", a, b, res);
return (0);
}
回答by higuaro
In fact, you don't need to even type cast the chars for doing this with a single char:
实际上,您甚至不需要键入字符即可使用单个执行此操作char:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char f1 = '9';
char f2 = '7';
int v = (f1 - '0') - (f2 - '0');
printf("%d\n", v);
return 0;
}
Will print 2
会打印 2
But beware, it won't work for hexadecimal chars
但要注意,它不适用于十六进制字符
回答by Mike
Well... you arealready adding char types, as you noted that's 4910and 5010which should give you 9910
嗯......你都已经添加字符类型的,因为你注意到这是49 10和50 10这应该给你99 10
If you're asking how to add the reperserented value of two characters i.e. '1' + '2' == 3you can subtract the base '0':
如果您问如何添加两个字符的重复值,即'1' + '2' == 3您可以减去基数'0':
printf("%d",('2'-'0') + ('1'-'0'));
This gives 3 as an int because:
这将 3 作为 int 给出,因为:
'0' = ASCII 48<sub>10</sub>
'1' = ASCII 49<sub>10</sub>
'2' = ASCII 50<sub>10</sub>
So you're doing:
所以你在做:
printf("%d",(50-48) + (49-48));
If you want to do a longer number, you can use atoi(), but you have to use strings at that point:
如果你想做一个更长的数字,你可以使用atoi(),但此时你必须使用字符串:
int * first = "123";
int * second = "100";
printf("%d", atoi(first) + atoi(second));
>> 223
回答by graziano governatori
回答by Willy Pt
you can use atoi() function
您可以使用 atoi() 函数
#include <stdio.h>
#include <stdlib.h>
void main(){
char f[] = {"1"};
char s[] = {"2"};
int i, k;
i = atoi(f);
k = atoi(s);
printf("%d", i + k);
getchar();
}
Hope I answered you question
希望我回答了你的问题

