1
2
3
0
[ ],
(1)
( )
int a[6];
a[0]0
1
4
5
a[1]
a[2]
a[3]
a[4]
a[5]
2
3
a
=*
sizeof()
5.1
4
[ ]
int i=15;
int data[i]; (?)
int a[10];
printf(“%d”,a); (?)
for(j=0;j<10;j++)
printf(“%d\t”,a[j]); (?)
int data[5];
data[5]=10; //C
5
(
int a[5]={1,2,3,4,5};
a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5;
static0
int a[5]={6,2,3};
a[0]=6; a[1]=2;a[2]=3; a[3]=0; a[4]=0;
int a[3]={6,2,3,5,1}; (?)
static int a[5];
a[0]=0; a[1]=0; a[2]=0; a[3]=0; a[4]=0;
int a[]={1,2,3,4,5,6};
6
1a[0]>a[1]
n-1
n——
2n-1
n-1
3n-1
10
7
38
49
65
76
13
27
30
97
38
49
65
13
27
30
76
38
49
13
27
30
65
38
13
27
30
49
13
27
30
38
13
27
30
49
38
65
97
76
13
27
30
n=8
38
49
76
9713
97
97
27
30
97
13
76
76
76
27
30
13
6527
6530
65
13
13
49
4930
4927
3827
3830
38
13
27
10
8
1n-1n
—
2n-2n-1
—
3n-1
10
9
[ 49 38 65 97 76 13 27 ]
k
j
i=1 13 49
13 [38 65 97 76 49 27 ]i=2 27 38
13 27 [65 97 76 49 38 ]
13 27 38 [97 76 49 65 ]
13 27 38 49 [76 97 65 ]
13 27 38 49 65 [97 76 ]
13 27 38 49 65 76 [97 ]
k
kk
k
j j j j j
j j j j j
10
10
[][]
:
int a[3][4];
float b[2][5];
int c[2][3][4];
int a[3,4]; (?)
=*
int a[3][2]
a[0][1]
a[1][0]
a[1][1]
a[2][0]
a[2][1]
0
1
4
5
2
3
a[0][0]
a[0][0] a[0][1]
a[1][0] a[1][1]
a[2][0] a[2][1]
int c[2][3][4]
0
1
2
34
5
6
7……….
..
20
21
22
23
c[0][0][0]
c[0][0][1]
c[0][0][2]
c[0][0][3]
c[0][1][0]
c[0][1][1]
c[0][1][2]
c[0][1][3]
c[0][2][0]
c[0][2][1]
c[0][2][2]
c[0][2][3]
c[1][0][0]
c[1][0][1]
c[1][0][2]
c[1][0][3]
c[1][1][0]
c[1][1][1]
c[1][1][2]
c[1][1][3]
c[1][2][0]
c[1][2][1]
c[1][2][2]
c[1][2][3]
5.2
11
int a[3][4];
2016
17
2018
19
2020
21
2022
23
2008
9
2010
11
2012
13
2014
15
2000
1
2002
3
2004
5
20006
7a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
a[i]4
a3
a[0]
a[1]
a[2]
0
1
4
5
2
3
a[0][1]
a[0][2]
a[0][3]
a[1][0]
a[1][1]
a[0][0]
a[1][3]
a[2][0]
a[2][1]
a[2][2]
a[2][3]
a[1][2]6
7
10
11
8
9
a[0]
a[1]
a[2]
12
[ ][ ]
int a[2][3]={{1,2,3},{4,5,6}};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 3 4 5 6
int a[2][3]={{1,2},{4}};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 0 4 0 0
int a[][3]={{1},{4,5}};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 0 0 4 5 0
int a[2][3]={1,2,3,4,5,6};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 3 4 5 6
int a[2][3]={1,2,4};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 4 0 0 0
int a[][3]={1,2,3,4,5};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 3 4 5 0
13
a= 1 2 34 5 6 b= 1 42 5
3 6
#include <stdio.h>
main()
{ int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2],i,j;
printf("array a:\n");
for(i=0;i<=1;i++)
{ for(j=0;j<=2;j++)
{ printf("%5d",a[i][j]);
b[j][i]=a[i][j];
}
printf("\n");
}
printf("array b:\n");
for(i=0;i<=2;i++)
{ for(j=0;j<=1;j++)
printf("%5d",b[i][j]);
printf("\n");
}
}
14
max=a[0][0]
for i=0 to 2
for j=0 to 3
a[i][j]>max
max=a[i][j]
row=i
colum=j
maxrow,colum
#include <stdio.h>
main()
{ int a[3][4]={{1,2,3,4},
{9,8,7,6},
{-10,10,-5,2}};
int i,j,row=0,colum=0,max;
max=a[0][0];
for(i=0;i<=2;i++)
for(j=0;j<=3;j++)
if(a[i][j]>max)
{ max=a[i][j];
row=i;
colum=j;
}
printf("max=%d,row=%d,\
colum=%d\n",max,row,colum);
}
15
char c[10],ch[3][4];
char ch[5]={?H?,?e?,?l?,?l?,?o?};
ch[0]
H e l l o
ch[1] ch[2] ch[3] ch[4]
!
char ch[5]={?H?,?e?,?l?,?l?,?o?};
ch[0]
H e l l o
ch[1] ch[2] ch[3] ch[4]
char ch[5]={?B?,?o?,?y?};
ch[0]
B o y \0 \0
ch[1] ch[2] ch[3] ch[4]
r [ =“Boy”;
[
\ \
[ [ [ [
char ch[6]={“H llo”};
char ch[6]=“Hello”;
char ch[]=“Hello”;
ch[0]
H e l l o
ch[1] ch[2] ch[3] ch[4]
\0
ch[5]
5.3
16
char diamond[][5]={{'.','.','*'},{'.','*','.','*'},
{'*','.','.','.','*'},{'.','*','.','*'},{'.','.','*'}};
.,* \0 \0
,*,* \0
*,,,*
,*,* \0
.,* \0 \0
diamond[0]
diamond[1]
diamond[2]
diamond[3]
diamond[4]
char fruit[][7]={“Apple”,”Orange”,
”Grape”,”Pear”,”Peach”};
fruit[0]
fruit[1]
fruit[2]
fruit[3]
fruit[4]
A p p l e \0 \0
O r a n g e \0
G r a p e \0 \0
P e a r \0 \0 \0
P e a c h \0 \0
17
\0?
hello”5 6 5
ASCII
18
I/O %c
I/O %s
main()
{ char str[5];
int i;
for(i=0;i<5;i++)
scanf(“%c”,&str[i]);
for(i=0;i<5;i++)
printf(“%c”,str[i]);
}
main()
{ char str[5];
scanf(“%s”,str);
printf(“%s”,str);
}
,&
<
\0?
,
\0?
19
main()
{
int i;
char a[5];
scanf("%s",a);
for(i=0;i<5;i++)
printf("%d,",a[i]);
}
1 hel,
2 hell,
3 hello,%s
h e l \0
h e l l \0
h e l l o
20
H o w \0
a r e \0
y o u? \0
#include <stdio.h>
main()
{ char a[15],b[5],c[5];
scanf("%s%s%s",a,b,c);
printf("a=%s\nb=%s\nc=%s\n",a,b,c);
scanf("%s",a);
printf("a=%s\n",a);
}
How are you?
a=How
b=are
c=you?
How are you?
a=How
How are you?
21
string.h
puts
puts()
\0?
gets
gets()
\0?
#include <stdio.h>
main( )
{ char string[80];
printf(“Input a string:”);
gets(string);
puts(string);
}
,How are you?
,How are you?
22
strcat
strcat(1,2)
21
1
1
,\0?;,1 \0?,
\0?
strcpy
strcpy(1,2)
21
1
1
\0?
char str1[20],str2[20];str1={“Hello!”}; (?)
str2=str1; (?)
1
23
strcmp
strcmp(1,2)
ASCII
\0?
inta,1< 2
b,1> 2
c,1== 2
==”,strcmp
strlen
strlen()
\0?
strlen(s)
1char s[10]={?A?,?\0?,?B?,?C?,?\0?,?D?};
2char s[ ]=“\t\v\\\0will\n”;
3char s[ ]=“\x69\082\n”; 1 3 1
2
24
#include <string.h>
#include <stdio.h>
main()
{ char str1[] =,Hello!",str2[] =,How are you?”,str[20];
int len1,len2,len3;
len1=strlen(str1); len2=strlen(str2);
if(strcmp(str1,str2)>0)
{ strcpy(str,str1); strcat(str,str2); }
else if (strcmp(str1,str2)<0)
{ strcpy(str,str2); strcat(str,str1); }
else strcpy(str,str1);
len3=strlen(str);
puts(str);
printf(”Len1=%d,Len2=%d,Len3=%d\n”,len1,len2,len3);
} How are you?Hello!
Len1=6,Len2=12,Len3=18
Ch6_23.c
strcmpstrlen
25
string
i=0 num=0 word=0
((c=string[i])!=?\0?)
c=
word=0 word=1
num=num+1
i=i+1
num
word==0
#include <stdio.h>
main()
{ char string[81];
int i,num=0,word=0;
char c;
gets(string);
for(i=0;(c=string[i])!='\0';i++)
if(c==' ') word=0;
else if(word==0)
{ word=1; num++; }
printf("There are %d words \
in the line\n",num);
}
26
=
word=0,num
(word==0),,
word=1,num1
(word==1),,num
0
1
1
1
0
1
0
1
2
1
1
2
1
0
2
0
1
3
1
0
3
0
1
4
1
1
4
1
1
4
1
1
4
I?am?a?boy.
word
word
num
I a m a b o y,
27
92 85 68 75 54 88 98 45 61 79
92
85
68
54
88
98
45
75
61
79
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]
a
#include <stdio.h>
main()
{ int k,a[10];
for(k=0;k<10;k++)
scanf(“%d”,&a[k]);
for(k=0;k<10;k++)
sum+=a[k];
printf(“Average is %d\n”,sum/10);
}
92 85 68 75
54 88 98 45
61 79 81 40