FFmpeg开发必备的C语言

HelloWorld

vi HelloWorld.c
#include <stdio.h>
  
int main(int argc,char* argv[]){
        printf("HelloWorld!\n");
        return 0;
}
clang -g -o helloworld HelloWorld.c

 ls -alt helloworld
 
./helloworld

在这里插入图片描述

常用基本类型

  • short、 int、 long
  • float、double
  • char
  • void
#include <stdio.h>
  
int main(int argc,char* argv[]){
        int a = 100;
        float b = 1.23;
        char c = 'C';

        printf("Hello World!\n");
        printf("a = %d\n",a);
        printf("b = %f\n",b);
        printf("c = %c\n",c);
        return 0;
}
clang -g -o helloworld1 HelloWorld.c

./helloworld1

打印:

Hello World!
a = 100
b = 1.230000
c = C

常量与变量

  • int a = 0; //变量,可以再赋值
  • const int len = 256; //常量定义

内存管理:
在这里插入图片描述

指针与数组

  • 指针就是内存地址:void* 、 char*
  • 数组 如:char c[2] 、 int arr[10]

指针:

  • 它就是内存中的一个地址
  • 指针本身运算
  • 指针所指内容的操作
    在这里插入图片描述
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
        int *a,*b;
        a = (int*)malloc(sizeof(int)); //在栈中开辟内存
        b = (int*)malloc(sizeof(int));

        *a = 1;
        *b = 2;

        int c[3] = {0,1,2};

        printf("addr of a:%p,%p,%d\n",&a,a,*a);
        printf("addr of b:%p,%p,%d\n",&b,b,*b);
        printf("addr of c:%p,%p,%d,%d,%d\n",&c,c,c[0],c[1],c[2]);

        return 0;
}
gcc -g -o testpoint testpoint.c 
./testpoint

打印结果:

addr of a:0x7ffee1cb1830,0x7fa99b402b60,1
addr of b:0x7ffee1cb1828,0x7fa99b402b70,2
addr of c:0x7ffee1cb184c,0x7ffee1cb184c,0,1,2

操作系统时如何管理内存的?

  • 栈空间
  • 堆空间
  • 内存映射

内存的分配与释放

  • 分配内存 void* mem = malloc(size);
  • 释放内存 free(mem);

内存泄漏与野指针

  • 不断向系统申请内存
  • 申请的内存不用,也不释放
  • 占用别人的内存称为野指针

函数指针

  • 返回值类型(*指针变量名)([形参列表]);
int func(int x);//声明一个函数
int(*f)(int x);//声明一个函数指针
f = func;//将func函数的首地址赋给指针f

函数指针示例:

 vi testfunc.c
#include<stdio.h>

int sum(int a,int b)
{
        return (a + b);
}

int sub(int a,int b)
{
        return (a- b);
}

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

        int(*f)(int,int);

        f = sum;
        int result = f(3,5);

        f = sub;
        int result2 =f(result,3);
        printf("3+5 = %d\n",result);
        printf("8-3 = %d\n",result2);

        return 0;
}
clang -g -o testfunc testfunc.c
./testfunc

打印:

3+5 = 8
8-3 = 5

结构体

vi testst.c 
#include<stdio.h>
  
struct st{
        int a;
        int b;
};

int main(int argc,char* argv[])
{
        
        struct st sst;
        sst.a = 10;
        sst.b = 20;
        printf("struct content is :%d,%d\n",sst.a,sst.b);
        
        return 0;

}
clang -g -o testst testst.c 
./testst

打印结果:

struct content is :10,20

枚举

vi testenum.c
#include<stdio.h>
  
enum e_type{
        red = 0,
        green,
        blue

};

int main(int argc,char* argv[]){
        enum e_type et;

        et = red;
        printf("the color is: %d\n",et);

        et = blue;
        printf("the color is: %d\n",et);

        return 0;
}
clang -g -o testenum testenum.c 
./testenum

打印结果:

the color is: 0
the color is: 2

文件操作

  • 文件类型 FILE* file;
  • 打开文件FILE* fopen(path,mode);
  • 关闭文件fclose(FILE*)
#include<stdio.h>
  
int main(int argc,char* argv[])
{
        FILE* file;
        char buf[1024] = {0,};
        file = fopen("1.txt","a+");//在末尾添加,游标指向末尾

        fwrite("hello world!",1,13,file);

        rewind(file);//游标指向最前

        fread(buf,1,13,file);
        fclose(file);

        printf("buf : %s\n",buf);

        return 0;
}
clang -g -o testfile testfile.c 
./testfile
buf : hello world!

C语言编译器

gcc/clang -g -O2 -o test test.c -I... -L... -l
  • -g: 输出文件中的调试信息
  • -O:对输出文件做指令优化
  • -o: 输出文件
  • -I: 指定头文件
  • -L:指定库文件位置
  • -l :指定使用哪个库

编译过程

  • 预编译
  • 编译
  • 链接,动态链接/静态链接
vi add.h
int add(int a,int b);
vi add.c
int add(int a,int b){
	return (a + b);
}
vi add.c
#include<stdio.h>
#include "add.h"

int main(int argc,char* argv[])
{
	printf("add = %d\n",add(2,3));
	return 0;
}

clang -g -c add.c
libtool -static -o libmylib.a add.o
clang -g -o testlib testlib.c -I. -L. -lmylib
./testlib

打印结果:

add = 5

或者:

clang -g -c testlib.c
clang -o testlib1 testlib.o -L . -lmylib
 ./testlib1

打印结果:

add = 5

C语言调试器

调试器原理

  • 编译输出带调试信息的程序
  • 调试信息包含:指令地址、对应源代码和行号
  • 指令完成后,回调

gdb和lldb

命令gdblldb
设置断点bb
运行程序rr
单步执行nn
跳入函数ss
跳出函数finishfinish
打印内容pp
lldb testlib
b main
 break list
r
s
p a
p b
finish
n
c #continue执行完之后的
quit # 退出

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

 cd testlib.dSYM/Contents/Resources/DWARF
 
 dwarfdump testlib 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值