繁体   English   中英

如何合并两个字符类型变量

[英]How to combine two character type variables

考虑iam创建两个变量

              char a1,b1,c1; /*consider that im assigning*/

              a1='a',b1='b';

              /* now i want to combine a1 and b1  and assign to c1 
               i.e c1='ab' pls suggest me the code to do that. 
              Dont suggest any complex code im a begineer. 

您不能使用常规char变量。 要执行您想要的操作,您应该使用char数组:

char a1, b1, c1[2];

a1 = 'a';
b1 = 'b';

c1[0] = a1;
c1[1] = b1;

您不能在char variable存储两个字符(该char variable只有1个字节)。

使用char数组并使用sprintfsnprintf

char c1[3];
sprintf(c1,"%c%c",a,b);   // or snprintf(c1,sizeof c1,"%c%c",a,b);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM