c语言函数和递归

c语言api网站查询
https://en.cppreference.com/w/
https://devdocs.io/c/
http://www.cplusplus.com/
/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}//函数strcpy的用法
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
/* memset example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}//把str前六个设置成-
自定义函数
ret_type fun_name(paral,*)
{
    statement;
}
ret_type
ret_name
paral
int main(){
}
get_max函数
#include<stdio.h>
int get_max(int a, int b) {
    if (a > b) 
        return a;
    else
        return b;
    
}


int main() {
    int a = 0;
    int b = 20;
    int max = get_max(a,b);
    printf("%d", max);
    return 0;
}
没有交换值
#include<stdio.h>

void Swap(int x, int y) {
    int temp = 0;
    temp = x;
    x = y;
    y = temp;
}

int main()
{
    int a = 10;
    int b = 20;
    printf("%d%d", a, b);
    Swap(a, b);
    printf("%d%d", a, b);
    
        
    return 0;
}
正确的方法需要用指针
#include<stdio.h>

void Swap1(int *x, int *y) {
    int temp = 0;
    temp = *x;
    *x = *y;
    *y = temp;
}

int main()
{
    int a = 10;
    int b = 20;
    printf("%d%d", a, b);
    Swap1(&a, &b);
    printf("%d%d", a, b);
    
        
    return 0;
}

形参:只有在函数被调用的过程中才能实例化
传值调用
当实参传给形参的时候
形参其实是实参的临时拷贝
对形参的修改是不会改变实参的
传址调用
让函数内和函数外建立真正的联系
int is_prime(int n) {   
    int j = 0;
    for (j = 2; j < n; j++) {
        if (n % j == 0) {
            return 0;
        }
        return 1;

    }
}
传数组到函数其实是传的首个元素的地址
链式嵌套函数

int main(){
    printf("%d", printf("%d", printf("%d", 43)));


    
        
    return 0;
}
//printf返回的是数字的个数所以是4321

int max(int x, int y);//函数的声明,避免警告
int main() {
    int x = 3;
    int y = 7;
    int sum =0;
    sum=max(x,y);函数的调用
    return 0;
}
int max(int x, int y) {.//函数的定义
    if (x > y) {
        return 1
    };
}
include"add.h"
来调用add
递归
stack:局部变量
函数形参
heap:动态开辟的内存
malloc calloc
静态区
全局变量
static
hhtps://stackoverflow.bom/
递归:
把复杂问题转化成相似的小问题
#include <stdio.h>
void print(int n) {
    if (n>9) {
        print(n / 10);

    }
    printf("%d", n % 10);
}

int main() {
    unsigned inst mun = 0;
    scanf("%d",&mun);
    print(mun);

strlen:
int my_strlen(char* str){
    int count=0;
while(*str!='\0'){
    count++
    str++}
return count;
}
大事化小:my_strlen(bit)
1+my_strlen(it)
1+1+my_strlen(t)
1+1+1+my _strlen("")
1+1+1+0;
int my_strlen(char* str){
    if(*str!='\0'){
        return 1+my_strlen(str+1)
    else
        return0;}}
阶乘:
int Fact(int n ){
    int i=0;
    int ret =1;
    for(i=1;i<=n;i++){
        ret*=i;

}
return ret;
}
(递归)
int fact(int n ){
    if(n<=1
         reurn    1;
    else
        return n*fact(n-1);


}
斐裂那数列:
  int fib(int n){
    if(n<=2)
        return 1;
    else
        return fib(n-1)+fib(n-2)}
int fib(int n ){
int i =1; ;
int j =1;
int k=1;
while(n>2){k=i+
i=j
j=k
n--
}
return k}
递归会栈溢出

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值