1.宏定义字符串
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define mac "58"
int main()
{
char *p="123";
char str1[20];
char* p1=str1;
strcpy(p1,p);
p1+=2;
strcpy(p1,mac);
char str[]="asc,"mac" \r\n";
char m=*mac;
/* Write C code in this online editor and run it. */
printf("%s\n",str);
printf("%s\n",str1);
printf(""mac"\n");
printf("%s%c",mac,m);
return 0;
}
输出:
asc,58
1258
58
585
本质:宏定义字符串本身就是一个字符串指针。指针加双引号是错误的,但是宏定义的的字符串给宏加双引号就可以解宏。
大家可以慢慢尝试,利用各种方法去测试和加深理解。
2.子进程
#include “stdio.h”
int main(int argc,char **argv){
int id=fork();//创建子进程
if(id==0){
printf("This is a child_process!");
return -1;//结束字进程
}
printf("This is a Father_process!");
}
本质:子进程会将父进程的程序复制一份当成自己的去执行,main函数的返回则会结束进程。
父进程中执行fork()会返回 进程id,而子进程中fork会返回0.
子进程一般用来处理父进程忙碌,让子进程去处理。子进程是镜像了一个父进程。
3.线程
/*int pthread_create(pthread_t *thread, //线程数据结构对象
const pthread_attr_t *attr, //指明线程的栈大小,属性等等
void *(*start_routine) (void *),//线程程序函数入口
void *arg);//传递给线程的数据
*/
#include "stdio.h"
#include "pthread.h"
//线程中的方法
void * thread_function(void *arg){
printf("This is a thread!");
}
int main(int argc,char **argv){
int ret=0;
pthread_t thread1;
ret=pthread_create(&thread1,null,thread_function,null);
if(ret==0){
printf("create fail!");
}
Sleep(5);//等待线程执行完成
printf("This is a test!");
}
本质:线程就是程序的一个分支,比如一边要处理通信数据,一边要处理显示数据,如果等处理完通信数据再去处理显示数据,那么通信数据达到M级别那么显示就会卡住,但是使用ui线程就可以一边执行线程,一边执行主程序。
系统中有一个时间轮,主程序执行一段时间(一般1ms),再执行线程(1ms)两个相互轮换占用cpu资源。线程间互不影响,线程1挂掉不会影响线程2核主程序(进程)。