C语言学习notice

C语言学习笔记(补充学习,遇到新知识及时更新)

一、关键字

  1. extern
    声明外部已经定义的变量,
    #include <stdio.h>
 
// 函数外定义变量 x 和 y
int x;
int y;
int addtwonum()
{
    // 函数内声明变量 x 和 y 为外部变量
    extern int x;//如果不加extern则为局部变量
    extern int y;
    // 给外部变量(全局变量)x 和 y 赋值
    x = 1;
    y = 2;
    return x+y;
}
 
int main()
{
    int result;
    // 调用函数 addtwonum
    result = addtwonum();
    
    printf("result 为: %d",result);
    printf("x的值:%d" , x);
    printf("y的值:%d" , y);
    return 0;
}
  1. #define
    定义一个常量
    #define PI 3.1415926
  2. const
    定义一个常量
    const long double PI = 3,1415926
  3. auto
    存储类是所有局部变量默认的存储类。
    定义在函数中的变量默认为 auto 存储类,这意味着它们在函数开始时被创建,在函数结束时被销毁
  4. static(重要,可以和extern对照学习)
    指示编译器在程序的生命周期内保持局部变量的存在
#include <stdio.h>
 
/* 函数声明 */
void func1(void);
 
static int count=10;        /* 全局变量 - static 是默认的 */
 
int main()
{
  while (count--) {
      func1();
  }
  return 0;
}
 
void func1(void)
{
/* 'thingy' 是 'func1' 的局部变量 - 只初始化一次
 * 每次调用函数 'func1' 'thingy' 值不会被重置。
 */                
  static int thingy=5;
  thingy++;
  printf(" thingy 为 %d , count 为 %d\n", thingy, count);
}

二、运算符

  1. 算数运算符
    pFn0szj.png
  2. 关系运算符
    pFn0cyn.png
  3. 逻辑运算符
    pFn0gLq.png
  4. 位运算符(单片机常用)
    pFn0DJg.png
    pFn06Qs.png
    示例代码
#include <stdio.h>
int main()
{
 
   unsigned int a = 60;    /* 60 = 0011 1100 */  
   unsigned int b = 13;    /* 13 = 0000 1101 */
   int c = 0;           
 
   c = a & b;       /* 12 = 0000 1100 */ 
   printf("Line 1 - c 的值是 %d\n", c );
 
   c = a | b;       /* 61 = 0011 1101 */
   printf("Line 2 - c 的值是 %d\n", c );
 
   c = a ^ b;       /* 49 = 0011 0001 */
   printf("Line 3 - c 的值是 %d\n", c );

}

三、指针与数组(参考江科大的视频复习)

  1. 指针的定义与加减的含义
    指针:实际为指针变量,代表地址的变量
    指针的定义: char *p;
    pFn0Re0.png
    指针的加减:加减单位与数据类型有关,加减单位为数据长度。
    pFn0WwV.png
#include<st>
  1. 数组名即为数组的首地址,也即为指针
  2. 多级指针之间的赋值:pFn0rWQ.png
  3. 指针在作为输入,传递数组数据(编写一个寻找最大值的函数的案例)
#include<stdio.h>

char FindMAX(const char* a,int count)//这里的const是为防止a数组中的数据被改变
{
    char max = a[0];
    char i;
    for ( i = 0; i < count; i++)
    {
        if (max < a[i])
        {
            max = a[i];
        } 
    }
    return max;
}


int main()
{
    char a[] = {11,11,12,23,56};
    char max;
    char count  = sizeof(a) ;
    max = FindMAX(a,count);
    printf("a数组中最大值为-->%d",max);
    return 0;
}
  1. 运用数据进行多返回值(超级的重要,案例要求:返回数组中的最大值,以及最大值的数)
#include <stdio.h>

void FindMAX_and_count(char* max,char* count,const char* a,int size)
{
    int i = 0;
    *max = a[0];
    for ( i = 0; i < size; i++)
    {
        if(*max < a[i])
        {
            *max = a[i];
            *count = 1 ; 
        }
        else if (*max = a[i])
        {
            *count ++ ;
        }
    }
}

int main()
{
    char a[] = {22,99,44,33,22,99};
    int size = sizeof(a);
    char count;
    char max;
    FindMAX_and_count(&max,&count,a,size);

    printf("a数组中的最大值为--> %d \n",max);
    printf("a数组中最大值的数量--> %d \n",count);

    return 0 ;
}
f(a);
    char count;
    char max;
    FindMAX_and_count(&max,&count,a,size);

    printf("a数组中的最大值为--> %d \n",max);
    printf("a数组中最大值的数量--> %d \n",count);

    return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值