簡體   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