C语言零散知识

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
// 传值调用(能更改全局变量的值)
int change1(int a,int b){
    int c=a+b;
    return c;
}

// 引用调用
int change2(int *a,int *b){
    int c=*a+*b;
    return c;
}
//  C++: & 实参变量所存放存储空间的地址(也就是能更改实参的值 )


// 静态变量 static 存储类指示编译器在程序的生命周期内保持局部变量的存在,
// 而不需要在每次它进入和离开作用域时进行创建和销毁

int fun(){ 
    static int x=1;
    x++;
    return x;
}


// x--和--x的差别
void f1(int x){
    int y=x;
    printf("%d\n",y--);
    y=x;
    printf("%d",--y);
}

// %  两边都是整数
int f2(int x){
    int y = x%10;
    return y;
}

// 指针函数
int *f3(){
    int *p,a[10]={1,2,3,4,5,6,7,8,9,0};
    p=a;
    return p;    
}
// 指针作为形参
int f4(int *p){
    int y=*p;
    return y;
}
// 回调函数(函数指针)
void f5(void){
    printf("hello this is f4");
}
void f6(void (*f5)(void)){
    printf("hello this is f5");
}

/**
    函数  目的
    1	strcpy(s1, s2);     复制字符串 s2 到字符串 s1。
    2	strcat(s1, s2);     连接字符串 s2 到字符串 s1 的末尾。
    3	strlen(s1);         返回字符串 s1 的长度。
    4	strcmp(s1, s2);     如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 0。
    5	strchr(s1, ch);     返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
    6	strstr(s1, s2);     返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。
*/	

// 字符数组和字符串的区别
void f7(){
    char s1[6]={'h','e','l','l','o','\0'};
    printf("%s\n",s1);
    char *s2="hello";
    printf("%s\n",s2);

    char s3[10];
    int i=0;
    for(;i<10;i++){
        scanf("%c",&s3[i]);
    }
    printf("%s\n",s3);
    printf("\n"); 
}
void f8(){
    char *s4,*p;
    s4=(char*)malloc(sizeof(char)*10);
    gets(s4);
    p=s4;
    printf("%s\n",p);
}

// 结构体的相关知识
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book = {"C 语言", "RUNOOB", "编程语言", 123456};
typedef struct 
{
	char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Books2;
typedef struct Books3
{
    char  title[50];
    char  author[50];
    char  subject[100];
    int   book_id;
} Books3;

//格式的输出
void f9(){
    int a=017; //一个八进制的数
    printf("十进制:%d\n",a);
    printf("八进制:%#o %o",a,a);
    printf("十六进制: %#x %x",a,a);
}

void f10(){
    int *p,a,b=9;
    p=&a;
    scanf("%d",p);
    printf("%d\n",*p);
    p=NULL; // 防止野指针
    p=&b;
    printf("%d\n",*p);
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值