c语言,如何声明一字符串数组并赋值1~30?(用循环)

推荐答案完全不合格,我要赋值字符串

/*

01 02 03 04 05 06 07 08 09 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

Press any key to continue

*/

#include <stdio.h>

int main() {
char str[31];
int i;
for(i = 0; i < 30; ++i)
str[i] = i + 1;
for(i = 0; i < 30; ++i) {
if(i && i % 10 == 0) printf("\n");
printf("%02d ",str[i]);
}
if(i % 9) printf("\n");
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-01-19
#include <stdio.h>
#include <stdlib.h>
#define N 30

int main()
{
  char *a[N];
  int i;
  for (i = 0; i < N; i++)
  {
    a[i] = (char *)malloc(3 * sizeof(char));
    sprintf(a[i], "%d", i + 1);
    puts(a[i]);
  }
  return 0;
}

第2个回答  2014-01-19
char s[30];
int i;
for (i=0;i<30;i++)
s[i]='i+1';
第3个回答  2014-01-19
#include<stdio.h>
int main(){
int a[30];
char b[30][2];
for(int i=0; i<30; i++){
a[i] = i+1;
sprintf(b[i], "%d", a[i]);
printf("%s\n",b[i]);
}
getchar();
return 0;
}

亲测可用的!本回答被提问者采纳
第4个回答  2014-01-19
#include<stdio.h>
int main()
{ int i,a[31]={};
for(i=1;i<=30;i++)
{
a[i]=i;
printf("%d ",a[i]);
}
system("pause");
return 0;
}
这是你想要的吧追问

不是,别这么自信

相似回答