2011
11.1
#include<stdio.h>
int main(){
char ch='A';
printf("ch(1)=%d,ch(2)=%c \n", ch,ch+1);
}
執行結果:
11.2
#include<stdio.h>
int main(){
int k=1,t=3;
do{
// t=4,k=2; t=7,k=4; t=11,k=5; t=17,k=7
t+=k++;
if(t%7==0)
continue;
else
// k=3,k=6,k=8
++k;
}while(t<15);
printf("k is %d \n", k);
}
執行結果:
11.3
#include<stdio.h>
int main(){
int s[12]={1,2,3,4,4,3,2,1,1,1,2,3},a[5]={0},i;
for(i=0; i<12; i++)
//a[1]=4,a[2]=3,a[3]=3,a[4]=2
a[s[i]]++;
for(i=1;i<5;i++)
printf("%d",a[i]);
printf("\n");
}
執行結果:
11.4
#include<stdio.h>
int main(){
char str1[15]="good";
char str2[10]="morning";
// str2 追加到 str1
strcat(str1,str2);
puts(str1);
printf("length of str1 is %d \n",strlen(str1));
}
執行結果:
11.5
#include<stdio.h>
int main(){
char *s="13579";
s++;
// printf("%s%s%s\n",*s,*(s+1),*s+1); 錯誤
printf("%c%c%c\n",*s,*(s+1),*s+1);
}
執行結果:
11.6
#include<stdio.h>
#define MAX(A,B) (A)>(B)?(A):(B)
#define PRINT(Y) printf("Y=%d\n",Y)
int main(){
int a=1,b=2,c=3,d=4,temp;
// 3<7,temp=7
temp=MAX(a+b,c+d);
PRINT(temp);
}
執行結果:
11.7
#include<stdio.h>
int fun(int x,int y)
{
return(x+y);
}
int main(){
int a=2,b=5,c=8;
//a+c+b=15,a-c=-6
printf("%d\n",fun(fun(a+c,b),a-c));
}
執行結果:
11.8
#include<stdio.h>
int main(){
struct date{
int year,month,day;
}today;
// 4字節 * 3=12字節
printf("%d\n",sizeof(struct date));
}
執行結果:
11.9 執行下列程序後,file2.txt的內容爲
#include<stdio.h>
int main(){
FILE *in,*out;
//3+1+4+1+2+1+4=16
char *str1="YOU PLAN TO FAIL.";
//2+1+3+1+4+1+2+1+4=7+4+4+4=15+4
char *str2="IF YOU FAIL TO PLAN.";
if((fopen("/Users/vincent/Desktop/file1.txt","r"))==NULL) {
printf("file1.txt is no exist!");
return 0;
}
if((fopen("/Users/vincent/Desktop/file2.txt","r"))==NULL) {
printf("file2.txt is no exist!");
return 0;
}
in = fopen("/Users/vincent/Desktop/file1.txt","w");
while(*str1!='.')
// str1寫入in
putc(*str1++,in);
fclose(in);
in = fopen("/Users/vincent/Desktop/file1.txt","r");
out = fopen("/Users/vincent/Desktop/file2.txt","w");
while(!feof(in)){
// C語言讀取文件會自動在後面添加\377
printf("%c",fgetc(in));
fputc(*str2++,out);
}
printf("\n");
fclose(in);
fclose(out);
}
執行結果:
控制檯: