明解c语言reading notes & practise

Posted on 2015-01-31 02:54  csappsicp  阅读(199)  评论(1)    收藏  举报

the codes according to 明解c语言
.............................................................................
//

include<string.h>

include<stdio.h>

int main(){

char a[5]="china";
char b[6]="china";
printf("the adress of a is %p\nthe adress of b is %p\n",a,b);
printf("len(a)=%lu\nlen(b) is %lu",strlen(a),strlen(b));
return 0;
}

change b[6] to b[5].and run the program again.

.............................................................................
1.1.0 100factorial

include<stdio.h>

int main(){
double num=1;
for (int i=1;i<100;i++)
{num=num*(i+1);}
printf("100!=%f",num);
}
100!=93326215443944102188325606108575267240944254854960571509166910400407995064242937148632694030450512898042989296944474898258737204311236641477561877016501813248.000000

analysis the number of 100!

include<stdio.h>

int main(){
int ch;
double i;
while((ch=getchar())!=EOF)
{
i++;
};
printf("i=%f",i);
return 0;
}
.................

1.1.3sizeof operator

include<stdio.h>

int main (){
int num[1]={0};
printf("the size of array is%lu\n",sizeof(num));
printf("size of num[0]=%lu\n",sizeof(num[0]));
printf( "the first num is =%d\n",num[0]);
printf("size of int is %lu\n",sizeof(int));
printf("size of char is %lu\n",sizeof(char));
printf("size of double is %lu\n",sizeof(double));
return 0;
}
//the size of array is4
size of num[0]=4
the first num is =0
size of int is 4
size of char is 1
size of double is 8
...........................

include<stdio.h>

/the code shows how to analysis the size of data/
int main (){
int num[1]={0};
printf("the size of array is%lu",sizeof(num));
return 0;
}

the size of array is 4//result

1.1.1 sizeof is gun c reference mannul

include <stddef.h>

 #include <stdio.h>
 
 static const int values[] = { 1, 2, 48, 681 };
 #define ARRAYSIZE(x) (sizeof x/sizeof x[0])
 
 int main (int argc, char *argv[])
 {
     size_t i;
     for (i = 0; i < ARRAYSIZE(values); i++)
     {
         printf("%d\n", values[i]);
     }
     return 0;
 }

.............................................................................

6.1 function
compare two integers and output the big one
code list6-1

include<stdio.h>

int big(int x,int y){

if(x>y)
return x;
else
return y;
}

int main(){
int num1,num2;

puts("please input two integers");
scanf("%d%d",&num1,&num2);

printf("the biger one is %d", big(num1,num2));
return 0;
}

codelist 6-2 compare three integers

include<stdio.h>

int big(int x,int y,int z){
int max;
if(x>y)
max=x;
else
max=y;

if(z>max)
return z;
else
return max;
}

int main(){
int num1,num2,num3;

puts("please input three integers");
scanf("%d%d%d",&num1,&num2,&num3);

printf("the biger one is %d", big(num1,num2,num3));
return 0;
}

//code list 8-10

include<stdio.h>

int main(){
//analyse the number of the char
int i,ch;
int cnt[10]={0};
puts("please input some integers");

while(1)
{
ch=getchar();
if(ch==EOF)
break;
if(ch>='0'&&ch<='9')
cnt[ch-'0']++;
}

for(i=0;i<=9;i++)
{
printf("'%d'=%d\t",i,cnt[i]);
}

}

8.4.2 escape sequence转义字符

\a alert
\b backspace
\t horizontal tab
\n newline
\v vertical tab
\f form feed
\r carriage return
' charactor'
" charactor"
? charactor?
\ charactor\

\ccc octal escape charactor , ccc are 1~3octonary [octal] number.
\xhh hexadecimal escape charactor , hh are any bites hexadecimal number.

number charactor '1'can use '\61'or'\x31' to express

execise 8-7

rewirte the program code list8-9,express the number of number charactor in '*' side by side

//code

include<stdio.h>

void prin(int x)
{
int i;
for(i=0;i<=x;i++)
{putchar('*');}
putchar('\n');
}

int main(){
//analyse the number of the char
int i,ch;
int cnt[10]={0};
puts("please input some integers");

while(1)
{
ch=getchar();
if(ch==EOF)
break;
if(ch>='0'&&ch<='9')
cnt[ch-'0']++;
}

for(i=0;i<=9;i++)
{
printf("'%d'=\t",i);
prin(cnt[i]);
}

}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3