[英]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數組並使用sprintf
或snprintf
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.