「C 语言」1 初识 C 语言

初识C语言

什么是C语言

​ 二进制语言:1010010100101

​ 汇编语言:助记符 - ADD - 10100001

​ B语言

​ C/C++/Java/Python

C 语言:–>不成熟–>成熟–>流行

国际标准:

ANSI C-C89/C90/C11/C99

第一个 C 语言程序

.c 源文件 .h 头文件head

#include<stdio.h>
//std-standard input output
//int是整型的意思-返回整型值
int main//主函数-程序的入口
{
    //输出函数print fuction
    //库函数#include <stdio.h>
    printf("hehe\n");
    //返回0
    return 0;
}

数据类型

分类

char          //字符数据类型
short         //短整型
int           //整型
long          //长整型
long long     //更长的整型
float         //单精度浮点数
double        //双精度浮点数
//C语言没有字符串类型

打印

//char-字符类型
//int-整型
int main()
{
    char ch = 'A';//申请内存空间  #好像只能用单引号#
    printf("%c\n",ch);//%c打印字符格式的数据
    int age = 20;
    print("%d\n",age);//%d打印整型十进制数
    float f = 5.0;
    printf("%f\n",f);//%f打印浮点数(单精度)
    //5.000000
    double d = 3.14;
    printf("%lf\n",d);//%lf打印浮点数(双精度)
    return 0;
}
//short int -短整型
//long-长整型
//

格式化输出

printf("%c",ch);
//%c-打印字符
//%d-打印整型
//%f-打印浮点数
//%p-打印地址
//%x-打印十六进制
//%o-

数据类型的大小

//sizeof - 大小
printf("%d\n", sizeof(char));//1
printf("%d\n", sizeof(short));//2
printf("%d\n", sizeof(int));//4
printf("%d\n", sizeof(long));//4 或 8>=4
printf("%d\n", sizeof(long long));//8
printf("%d\n", sizeof(float));//4
printf("%d\n", sizeof(double));//8

字节 bit - 比特位 byte - 字节 = 8 个比特位 0000000000000000 0000000000000001 0000000000000010

变量,常量

定义变量

变量的分类

  • 全局变量 - 定义在代码块之外的变量

  • 局部变量 - 定义在代码块之内的变量

    int num1 = 0;//全局变量
    int a = 100;
    int main()
    {
        int num2 = 100;//局部变量
        int a = 10;
        printf("%d\n",a);//10
        return 0;
    }

    局部变量的名字和全局变量的名字不要相同,相同时会以局部变量优先。

    局部变量和全局变量作用范围不同

    int main()
    {
        {
            int a = 0;//作用在大括号内
        }
        printf("%d\n",a);//错误
        return 0;
    }

变量的使用

​ 输入函数

#include<stdio.h>
int main()
{
    int num1 = 0;
    int num2 = 0;
    int sum = 0;
    scanf("%d%d",&num1,&num2);//& - 取地址符号
    sum = num1+num2;
    printf("sum=%d\n",sum);
    return 0;
}

C语言语法规定:变量定义在当前代码块的最前面。

变量的作用域和生命周期

作用域:大括号{}、整个代码

声明变量 - val变量在工程的其他源文件中定义,

#include<stdio.h>
int main()
{
    extern int val;//extern - 声明外部变量的
    printf("val=%d\n",val);
    return 0;
}

生命周期:作用域的开始到结束。

常量

分类

  • 字面常量

  • const 修饰的常变量

  • #define 定义的标识符常量

  • 枚举常量

字面常量

就。。。字面意思

常变量

const - 常属性

#include<stdio.h>

int main()
{
    const int num = 3;
    prinf("%d\n",num);
    num = 8;
    printf("%d\n",num);//error
    return 0;
}

变量,具有常属性

#include<stdio.h>

int main()
{
    int arr[10] = {0};//okk
    int n = 10;
    int arr[n] = {1};//error
    const int m = 10;
    int arr[m] = {2};//error
    return 0;
}

#define 定义的标识符常量

#include<stdio.h>

#define MAX 100

int main()
{
    int arr[MAX] = {1};
    return 0;
}

枚举常量

可以一一列举的量

enum - 枚举关键字

#include<stdio.h>

enum Sex
{
    MALE,
    FEMALE,
    SECRET
}

int main()
{
    enum Sex s = FEMALE;
    printf("%d\n%d\n%d",MALE,FEMALE,SECRET);//012有默认值,不可改变
    return 0;
}

字符串+转义字符+注释

字符串

"hello world!\n"
#include<stdio.h>

int main()
{
    "";//空字符串
    char arr1[] = "abc";
    printf("%s\n",arr1);//abc
    char arr2[] = {'a','b','c'};
    printf("%s\n",arr2);//abcbshjhd
    return 0;
}
arr1 = "abc";//数组 - 'a','b','c','\0'
arr2 = {'a','b','c'};// - 'a','b','c'
//'\0'的值是0,标志字符串的结束,不算字符串的内容

ASCII 码

0 - 48,

A - 65, Z - 90

a - 97, z - 121

#include<stdio.h>

int main()
{
    char arr1[] = "abc";
    char arr2[] = {'a','b','c'};
    //strlen() - 计算字符串长度 到'\0'停止 - 引<string.h>
    printf("%d\n",strlen(arr1));//3
    printf("%d\n",strlen(arr2));//随机值
    return 0;
}

转义字符

‘\’+字符

转义字符意义
\n换行
\t水平制表符
?区别三字母词(现在不用)
\’表示单引号 ‘\’'
\\斜杠
\"表示双引号 “\”"
\ddd表示 8 进制数字对应字符(d<8)
\xdd表示 16 进制数字对应的字符(d 是 16 进制)

注释

/*
*C风格的注释
不支持嵌套
*/
//C++风格的注释

Ctrl+K+C - 注释

Ctrl+K+U - 取消注释

选择语句

if (a==1)
    printf("1");//单行不用大括号
else
    printf("error");

循环语句

  • for 循环

  • while 循环

  • do while 循环

#include<stdio.h>

int main()
{
    int line = 0;
    printf("学习:\n");
    while (line<20000)//满足条件则执行
    {
        printf("一行代码\n");
        line++;
    }
    return 0;
}

函数

#include<stdio.h>

int Add (int x, int y)//Add - 函数名;int - 返回值类型
{                  //函数体
    int z = x + y;
    return z;//返回值
}

int main()
{
    int num1;
    int num2;
    scanf("%d%d",&num1,&num2);
    int sum = Add(num1,num2);//调用函数
    printf("sum = %d\n",sum);
    return 0;
}

数组

数组的定义

一组相同类型数据的集合

#include<stdio.h>

int main()
{
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    char ch[20];
    printf("%d\n",arr[4]);//5 下标从0开始
    return 0;
}

数组的使用

#include<stdio.h>

int main()
{
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    int i = 0;
    while (i<sizeof(arr)/sizeof(arr[0]))
    {
        printf("%d\n",arr[i]);
        i++;
    }
    return 0;
}

操作符

双目操作符

算术操作符

+ - * / %

移位操作符

>> «

位操作符

& ^ |

赋值操作符

= += -= *= \= &= ^= |= »= «=

关系操作符

>

>=

<

<=

==

!=

逻辑操作符

&& 逻辑与 - and

|| 逻辑或 - or

单目操作符

! 逻辑反操作

- 负值

+ 正值

& 取地址

sizeof 操作数的类型长度(以字节为单位)

~ 对一个数的二进制位按位取反

– 前置、后置–

++ 前置、后置++

* 间接访问操作符(解引用操作符)

(类型) 强制类型转换

EOF - end of file - (-1)

~ 按位取反

原码、反码、补码 负数在内存中存储时,存储的是二进制的补码 最高位 - 符号位 使用的、打印的是原码 负数:原码的符号位不变,其他位按位取反得到反码,反码加一得到补码 正数:三个码一样

#include<stdio.h>

int main()
{
    int a = 0;
    //00000000000000000000000000000000 - 原来
    //11111111111111111111111111111111 - 取反后的补码
    //11111111111111111111111111111110 - 反码
    //10000000000000000000000000000001 - 原码 - -1
    int b = ~a;
    printf("%d\n",b);//-1
    return 0;
}

++ / –

#include<stdio.h>

int main()
{
    int a = 10;
    int b = a++;//后置++,先使用,再++
    int c = ++a;//前置++,先++,再使用
    printf("a=%d b=%d c=%d\n",a,b,c);//a=12 b=10 c=12
    return 0;
}

(类型) 强制类型转换

int main()
{
    int a = (int)3.14;
    return 0;
}

三目操作符

条件操作符

exp1 ? exp2 : exp3

exp1为真 - exp2

exp1为假 - exp3

#include<stdio.h>

int main()
{
    int a = 10;
    int b = 20;
    int max = a>b?a:b;
    printf("%d\n",max);
    return 0;
}

逗号表达式

,

其他操作符

[] 下表引用操作符

() 函数调用操作符

. 结构体成员

-> 结构体指针成员

常见关键字

auto  break  case  char  const  continue  default  do  double  else  enum  extern float  for  goto  if  int  long  register  return  short  signed  sizeof  static  struct  switch  typedef  union  unsigned  void  volatile  while 

auto - 自动

#include<stdio.h>

int main()
{
    int a = 10;//局部变量 - 自动变量(自动销毁)
    return 0;
}

计算机储存数据

寄存器 高速缓存 内存 硬盘

register - 定义寄存器变量

#include<stdio.h>

int main()
{
    register int a = 10;//建议定义a为寄存器变量
    return 0;
}

int定义的变量是有符号的 - signed int

unsigned int -无符号数

struct - 结构体关键字

typeof -

union - 联合体、共用体

typedef - 类型定义

#include<stdio.h>

typedef unsigned int u_int32;

int main()
{
    u_int32 a = 18;
    printdf("%d",a);//18
    return 0;
}

static - 静态

static - 修饰局部变量 - 生命周期变长

static - 修饰全局变量 - 作用域变小 (只在本身的源文件中使用)

static - 修饰函数 - 改变了函数的连接属性(外部连接属性->内部连接属性)

#include<stdio.h>

void test()
{
    int a = 1;
    static int b = 1;//b是静态局部变量
    a++,b++;
    printf("a=%d b=%d\n",a,b);//a=22222、b=23456
}

int main()
{
    int i = 0;
    while (i<5)
    {
        test();
        i++;
    }
    return 0;
}

#define - 定义常量和宏

对一些量的替换

#include<stdio.h>

#define MAX 100//定义标识符常量

int main()
{
    int arr[MAX] = {1};
    return 0;
}
#include<stdio.h>

#define MAX(X,Y) (X>Y?X:Y)//定义宏

int main()
{
    int x = 10, y = 20;
    int max = MAX(x,y);
    printf("%d",max);
    return 0;
}

指针

地址 - 空间

32位/64位 - 32/64根地址线

指针 变量

#include<stdio.h>


int main()
{
    int a = 10;
    int *p = &a;//取出a的地址
    //p - 用来存放地址的变量 - 指针变量
    printf("%p",p);
    return 0;
}
#include<stdio.h>

int main()
{
    int a = 10;
    pritf("%d\n",a);
    int*  p = &a;//取地址
    *p = 20;//* - 解引用操作符
    printf("%d\n",a);
    return 0;
}

指针变量的大小

32位 - 4 64位 - 8

结构体

结构体的创建与初始化

表示复杂对象

#include<stdio.h>

struct Book
{
   char name[20];
   short price;
};//; - 必不可少

int main()
{
    //利用结构体 
    struct Book  b1 = {"C语言程序设计",55};
    printf("书名:%s\n",b1.name);
    printtf("%d",b1.price);
}

结构体的指针

#include<stdio.h>

struct Book
{
   char name[20];
   short price;
};

int main()
{
    struct Book b1 = {"C语言程序设计",55};
    struct Book* pb = &b1;
    printf("%p\n",pb);
    printf("%s\n",(*pb).name);
    printf("%s\n",pb->name);
    return 0;
}

结构体变量.成员

结构体指针->成员

#include<stdio.h>
#include<string.h>

struct Book
{
   char name[20];
   short price;
};

int main()
{
    struct Book b1 = {"C语言程序设计",55};
    struct Book* pb = &b1;
    printf("书名:%s 价格:%d\n",pb->name,pb->price);
    b1.price = 15;//price是变量,可以直接更改
    //b1.name = "C++" - error
    strcpy(b1.name, "C++");//引入string.h
    //数组名实际上是数组的地址
    printf("书名:%s 价格:%d\n",pb->name,pb->price);

    return 0;
}
#C #笔记
0%