22.10.14

ubuntu@ubuntu:~/yuyu/yu$ vim 21.c
ubuntu@ubuntu:~/yuyu/yu$ gcc 21.c
ubuntu@ubuntu:~/yuyu/yu$ ./a.out
26
ubuntu@ubuntu:~/yuyu/yu$ vim 21.c
ubuntu@ubuntu:~/yuyu/yu$ cat 21.c
//求长度
#include <stdio.h>

int mystrlen(char *s);


int main(int argc,const char *argv[])
{
    //char b[32]="";
    //gets(b);
    char brr[32]="abcdefghijklmnopqrstuvwxyz";
    char *s=brr;
    int res=mystrlen(s);
    printf("%d\n",res);
    return 0;

}


int mystrlen(char *s)
{

//    char arr[32]="abcdefghijklmnopqrstuvwxyz";
    //gets(arr);
  //  char *s=arr;
    int len=0;
    while( *s != '\0' )
    {
        s++;
        len++;
    }

    return len;
}
ubuntu@ubuntu:~/yuyu/yu$

传递和地址传递



ubuntu@ubuntu:~/yuyu/yu/2$ gcc 5.c
ubuntu@ubuntu:~/yuyu/yu/2$ ./a.out
*q = 0x56146dadd260
0x56146dadd260
ubuntu@ubuntu:~/yuyu/yu/2$ cat 5.c
#include <stdio.h>
#include <stdlib.h>



#if 0
//方式1 通过返回值方式
int *create_memory1(){
	int *temp = malloc(sizeof(int));
	printf("temp = %p\n", temp);
	return temp;
#endif

	//方式2 通过参数处理
	//注意,必须将实参 p 的地址传给函数
	//否则使用的是值传递 值传递的方式 分配空间的首地址
	void create_memory2(int **q){

		*q = (int *)malloc(sizeof(int));
		printf("*q = %p\n", *q);

	}






int main(int argc, const char *argv[]){


	int *p = NULL;
#if 0
	p = create_memory1();
	printf("p = %p\n",p);
#endif
	create_memory2(&p);
	printf("%p\n",p);
	free(p);
	p = NULL;
	return 0;
}

为什么输出一个结果


ubuntu@ubuntu:~/yuyu/yu/2$ gcc 7.c
ubuntu@ubuntu:~/yuyu/yu/2$ ./a.out
14
14
ubuntu@ubuntu:~/yuyu/yu/2$ cat
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/2$ cat 7.c
#include <stdio.h>
int main(int argc, const char *argv[])
{
	int a[]={2,4,6,8,10},  y=0, x, *p, w[10];
	p=&a[1];
		for(x=1; x<3; x++)
		
			y+=p[x];

		 w[x] = y;

	printf("%d\n",y );
	printf("%d\n",w[x] );
	return 0;
}


 

冒泡排序

ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7 
2 3 5 6 7 11 1 4 
  2  3  5  6  7 11  1ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7 
4
  43
  34
  46
  68
  84
  48
  8
  8  8  8  4  4  4  4
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
4 1 11 6 5 3 2 
  4  1 11  6  5  3  2
  2  3  5  6 11  4  4
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
 2  3  5  6 11  4  4
  2  3  5  6 11  4  4
  4 11 11  0  0  0  0
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4 
  2  3  5  6  7 11  1
  1 11  0  0  0  0  0
ubuntu@ubuntu:~/yuyu/yu/6$ cat  bubble_score.c
#include <stdio.h>
int main(int argc, const char *argv[])

{
	int i=1,j=0,temp,s[20],n;
	scanf("%d",&n);
	for(j=0;j<n;j++)
	{
		scanf("%d",&s[j]);
		printf("%3d",s[j]);
	}
	putchar(10);
	for(i=1;i<n;i++)
	{ 
		for(j=0;j<n-i;j++)
		{
			if(s[j]>s[j+1])
				temp=s[j];
			s[j]=s[j+1];
			s[j+1]=temp;
		}
	}
	for(j=0;j<n;j++)
	{
		printf("%3d",s[j]);
	}
	putchar(10);
	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4 
  2  3  5  6  7 11  1  2  3  5  6  7 11  1
  1 11  0  0  0  0  0
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4 
  2  3  5  6  7 11  1
  2  3  5  6  7 11  1
  1 11  0  0  0  0  0
ubuntu@ubuntu:~/yuyu/yu/6$ 
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4
  2  3  5  6  7 11  1
  1 1119701691591970169159197016915919701691591970169159
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2
  23
  3^[^C
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2
  2^C
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4
  2  3  5  6  7 11  1
  1 1119701691591970169159197016915919701691591970169159
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
bubble_score.c: In function ‘main’:
bubble_score.c:14:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
   printf("%d",&s[j]);
           ~^  ~~~~~
           %ls
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4

23567111
  1 1119701691591970169159197016915919701691591970169159
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4

23567111
  1 11  0  0  0  0  0
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4

23567111
  1 11  0  0  0  0  0
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4

  2  3  5  6  7 11  1
  1 11  0  0  0  0  0
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4
  2  3  5  6  7 11  1
  1  2  3  5  6  7 11
ubuntu@ubuntu:~/yuyu/yu/6$ 
ubuntu@ubuntu:~/yuyu/yu/6$ gcc bubble_score.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
7
2 3 5 6 7 11 1 4
  2  3  5  6  7 11  1
  1  2  3  5  6  7 11
ubuntu@ubuntu:~/yuyu/yu/6$ cat bubble_score.c
#include <stdio.h>
int main(int argc, const char *argv[])

{
	int i=1,j=0,temp,s[20],n,h;
	scanf("%d",&n);
	for(j=0;j<n;j++)
	{
		scanf("%d",&s[j]);
	}
		for(j=0;j<n;j++)
	{
		printf("%3d",s[j]);
	}
	putchar(10);
	for(i=1;i<n;i++)
	{ 
		for(j=0;j<n-i;j++)
		{
			if(s[j]>s[j+1])
				{
				temp=s[j];
			s[j]=s[j+1];
			s[j+1]=temp;
				}
			else
			{
				s[j]=s[j];
			}
		}
	}
	for(j=0;j<n;j++)
	{
		printf("%3d",s[j]);
	}
	putchar(10);
	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/6$ 

 

ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
10
12 9 30 23 53 62 16 28 32 18 12 193  3 1 1123
 12  9 30 23 53 62 16 28 32 18
  9 12 16 18 23 28 30 32 53 62
ubuntu@ubuntu:~/yuyu/yu/6$ 

 

斐波那契额


ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘main’:
fun.c:24:9: error: ‘a’ undeclared (first use in this function)
  s[n+2]=a;
         ^
fun.c:24:9: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
12
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘fun’:
fun.c:11:10: error: lvalue required as left operand of assignment
    fun(n)=1;
          ^
fun.c:13:9: error: lvalue required as left operand of assignment
   fun(n)=fun(n-1)+fun(n-2)
         ^
fun.c:14:2: error: expected ‘;’ before ‘return’
  return fun(n);
  ^~~~~~
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘fun’:
fun.c:11:10: error: lvalue required as left operand of assignment
    fun(n)=1;
          ^
fun.c:13:9: error: lvalue required as left operand of assignment
   fun(n)=fun(n-1)+fun(n-2)
         ^
fun.c:14:2: error: expected ‘;’ before ‘return’
  return fun(n);
  ^~~~~~
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘fun’:
fun.c:11:10: error: lvalue required as left operand of assignment
    fun(n)=1;
          ^
fun.c:13:9: error: lvalue required as left operand of assignment
   fun(n)=fun(n-1)+fun(n-2);
         ^
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘fun’:
fun.c:13:2: error: expected ‘}’ before ‘else’
  else
  ^~~~
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
10
Segmentation fault (core dumped)
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
10
Segmentation fault (core dumped)
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘main’:
fun.c:19:16: error: expected expression before ‘int’
   printf("%3d",int fun);
                ^~~
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘main’:
fun.c:19:16: error: expected expression before ‘int’
   printf("%3d",int fun(i));
                ^~~
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
fun.c: In function ‘main’:
fun.c:19:16: error: expected expression before ‘int’
   printf("%3d",int fun(i));
                ^~~
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
12
Segmentation fault (core dumped)
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
Segmentation fault (core dumped)
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
10
  2  3  5  8 13 21 34 55
ubuntu@ubuntu:~/yuyu/yu/6$ 
ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
10
  2  3  5  8 13 21 34 55
ubuntu@ubuntu:~/yuyu/yu/6$ cat fun.c
#include <stdio.h>
int fun(int n)
{
	if(n<=2)
	{
		return 1;
	}
	else
	{
		return fun(n-1)+fun(n-2);          //return 返回的是fun(n)
	}
}
int main(int argc, const char *argv[])
{
	int i,n;
	scanf("%d",&n);
	for(i=3;i<=n;i++)
	{
		printf("%3d",fun(i));
	}
	putchar(10);
	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/6$ 

 菲波那切数列

ubuntu@ubuntu:~/yuyu/yu/6$ gcc fun.c
ubuntu@ubuntu:~/yuyu/yu/6$ ./a.out
10
  1  1  2  3  5  8 13 21 34 55
ubuntu@ubuntu:~/yuyu/yu/6$ 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值