1.字符数组赋值:char c1[20] = "women";
2。将数转为字符赋值给数组
#include <stdio.h> #include <cstdlib> #include <stdlib.h> int main() { char c1[20] = "women"; int num=10,num1=101; char str[100]; itoa(num,str,16);//将num转换化16进制赋值给str printf("%s\n",str); itoa(num1,str,2);//将num转换化2进制赋值给str printf("%s\n",str); char s[10]; sprintf(s, "%d", 123); //产生"123",赋值给数组 printf("%s\n",s); sprintf(s, "%8d%8d", 123, 4567); //产生:" 123 4567" ,赋值给数组 printf("%s\n",s); sprintf(s, "%-8d%8d", 123, 4567); //产生:"123 4567" printf("%s\n",s); sprintf(s, "%8x", 4567); //小写16 进制,宽度占8 个位置,右对齐 printf("%s\n",s); sprintf(s, "%-8X", 4568); //大写16 进制,宽度占8 个位置,左对齐 printf("%s\n",s); sprintf(s, "%08X", 4567); //产生:"000011D7" printf("%s\n",s); sprintf(s, "%08x", 4567); //产生:"000011D7" printf("%s\n",s); sprintf(s,"%7.2f",23.35); printf("%s\n",s); sprintf(s,"%.2f",23.35); printf("%s\n",s); getchar(); }
原文:https://www.cnblogs.com/xkdn/p/15124099.html