代码 1 double balance[100]; 代码 2 balance[3] = 12.23; 代码 3 char p[10]; 代码 4 数组 #include <stdio.h> int main(void) { int x[100]; /* this declares a 100-integer array */ int t; /* load x with values 0 through 99 */ for(t=0; t<100; ++t) x[t] = t; /* display contents of x */ for(t=0; t<100; ++t) printf("%d ", x[t]); return 0; } 代码 5 int count[10], i; /* this causes count to be overrun(溢出) */ for(i=0; i<100; i++) count[i] = i; 代码 6 char a[7]; 代码 7 int sample[10]; 代码 8 int *p; int sample[10]; p = sample; 代码 9 int main(void) { int i[10]; func1(i); /* ... */ } 代码 10 void func1(int *x) /* pointer */ { /* ... */ } 代码 12 void func1(int x[]) /* unsized array */ { /* ... */ } 代码 13 void func1(int x[32]) { /* ... */ } 代码 14 char str[11]; 代码 15 #include <stdio.h> #include <string.h> int main(void) { char s1[80], s2[80]; gets(s1); gets(s2); printf("lengths: %d %d\n", strlen(s1), strlen(s2)); if(!strcmp(s1, s2)) /*字符串比较函数*/ printf("The strings are equal\n"); strcat(s1, s2); /*字符串连接函数*/ printf("%s\n", s1); strcpy(s1, "This is a test.\n"); /*拷贝字字符串*/ printf(s1); if(strchr("hello", 'e')) /*字符查找 */ printf("e is in hello\n"); if(strstr("hi there", "hi")) printf("found hi"); return 0; } 代码 16 int d[10][20]; 代码 17 d[1][2] 代码 18 #include <stdio.h> int main(void) { int t, i, num[3][4]; for(t=0; t<3; ++t) for(i=0; i<4; ++i) num[t][i] = (t*4)+i+1; /* now print them out */ for(t=0; t<3; ++t) { for(i=0; i<4; ++i) printf("%3d ", num[t][i]); printf("\n"); } return 0; } 代码 19 void func1(int x[][10]) { /* ... */ } 代码 20 x[2][4] 代码 21 /* A simple student grades database. */ #include <stdio.h> #include <ctype.h> #include <stdlib.h> #define CLASSES 3 #define GRADES 5 int grade[CLASSES][GRADES]; void enter_grades(void); void disp_grades(int g[][GRADES]); int get_grade(int num); int main(void) { char ch, str[80]; for(;;) { do { printf("(E)nter grades\n"); printf("(R)eport grades\n"); printf("(Q)uit\n"); gets(str); ch = toupper(*str); /*小写字母转换为大写字符*/ } while(ch!='E' && ch!='R' && ch!='Q'); switch(ch) { case 'E': enter_grades(); break; case 'R': disp_grades(grade); break; case 'Q': exit(0); } } return 0; } /* Enter the student's grades. */ void enter_grades(void) { int t, i; for(t=0; t<CLASSES; t++) { printf("Class # %d:\n", t+1); for(i=0; i<GRADES; ++i) grade[t][i] = get_grade(i); } } /* Read a grade. */ int get_grade(int num) { char s[80]; printf("Enter grade for student # %d:\n", num+1); gets(s); return(atoi(s)); /*字符串转换为整数*/ } /* Display grades. */ void disp_grades(int g[][GRADES]) { int t, i; for(t=0; t<CLASSES; ++t) { printf("Class # %d:\n", t+1); for(i=0; i<GRADES; ++i) printf("Student #%d is %d\n", i+1, g[t][i]); } } 代码 22 char str_array[30][80]; 代码 23 gets(str_array[2]); 代码 24 gets(&str_array[2][0]); 代码 25 /* A very simple text editor. */ #include <stdio.h> #define MAX 100 #define LEN 80 char text[MAX][LEN]; int main(void) { register int t, i, j; printf("Enter an empty line to quit.\n"); for(t=0; t<MAX; t++) { printf("%d: ", t); gets(text[t]); if(!*text[t]) break; /* quit on blank line */ } for(i=0; i<t; i++) { for(j=0; text[i][j]; j++) putchar(text[i][j]); putchar('\n'); } return 0; } 代码 26 int m[4][3][6][5]; 代码 27 void func1(int d[][3][6][5]) { /* ... */ } 代码 28 char p[10]; 代码 29 p &p[0] 代码 30 p == &p[0] 代码 31 int *p, i[10]; p = i; p[5] = 100; /* assign using index */ *(p+5) = 100; /* assign using pointer arithmetic */ 代码 32 a &a[0][0] 代码 33 int num[10][10]; /* ... */ void pr_row(int j) { int *p, t; p = (int *) &num[j][0]; /* get address of first element in row j */ for(t=0; t<10; ++t) printf("%d ", *(p+t)); } 代码 34 void pr_row(int j, int row_dimension, int *p) { int t; p = p + (j * row_dimension); for(t=0; t<row_dimension; ++t) printf("%d ", *(p+t)); } /* ... */ void f(void) { int num[10][10]; pr_row(0, 10, (int *) num); /* print first row */ } 代码 35 int i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 代码 36 char str[9] = "I like C"; 代码 37 char str[9] = {'I', ' ', 'l', 'i', 'k', 'e',' ', 'C', '\0'}; 代码 38 int sqrs[10][2] = { 1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36, 7, 49, 8, 64, 9, 81, 10, 100 }; 代码 39 int sqrs[10][2] = { {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; 代码 40 char e1[12] = "Read error\n"; char e2[13] = "Write error\n"; char e3[18] = "Cannot open file\n"; 代码 41 char e1[] = "Read error\n"; char e2[] = "Write error\n"; char e3[] = "Cannot open file\n"; 代码 42 printf("%s has length %d\n", e2, sizeof e2); 代码 43 int sqrs[][2] = { {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; 代码 44 /* A simple Tic Tac Toe game. */ /* 3*3棋子 */ #include <stdio.h> #include <stdlib.h> char matrix[3][3]; /* the tic tac toe matrix */ char check(void); void init_matrix(void); void get_player_move(void); void get_computer_move(void); void disp_matrix(void); int main(void) { char done; printf("This is the game of Tic Tac Toe.\n"); printf("You will be playing against the computer.\n"); done = ' '; init_matrix(); do { disp_matrix(); get_player_move(); done = check(); /* see if winner */ if(done!= ' ') break; /* winner!*/ get_computer_move(); done = check(); /* see if winner */ } while(done== ' '); if(done=='X') printf("You won!\n"); else printf("I won!!!!\n"); disp_matrix(); /* show final positions */ return 0; } /* Initialize the matrix. */ void init_matrix(void) { int i, j; for(i=0; i<3; i++) for(j=0; j<3; j++) matrix[i][j] = ' '; } /* Get a player's move. */ void get_player_move(void) { int x, y; printf("Enter X,Y coordinates for your move: "); scanf("%d%*c%d", &x, &y); x--; y--; if(matrix[x][y]!= ' ') { printf("Invalid move, try again.\n"); get_player_move(); } else matrix[x][y] = 'X'; } /* Get a move from the computer. */ void get_computer_move(void) { int i, j; for(i=0; i<3; i++) { for(j=0; j<3; j++) if(matrix[i][j]==' ') break; if(matrix[i][j]==' ') break; } if(i*j==9) { printf("draw\n"); exit(0); } else matrix[i][j] = 'O'; } /* Display the matrix on the screen. */ void disp_matrix(void) { int t; for(t=0; t<3; t++) { printf(" %c | %c | %c ",matrix[t][0],matrix[t][1], matrix [t][2]); if(t!=2) printf("\n---|---|---\n"); } printf("\n"); } /* See if there is a winner. */ char check(void) { int i; for(i=0; i<3; i++) /* check rows */ if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2]) return matrix[i][0]; for(i=0; i<3; i++) /* check columns */ if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i]) return matrix[0][i]; /* test diagonals */ if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) return matrix[0][0]; if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0]) return matrix[0][2]; return ' '; }