示例代码:
#include//函数声明voidprintHello();intmain(){printHello();//函数调用return0;}//函数定义voidprintHello(){printf("Hello,World!\n");}
递归:递归函数通常包含两个部分:基本情况和递归情况。基本情况用于停止递归,递归情况用于继续递归。
2控制结构
C语言提供了多种控制结构,帮助你实现复杂的逻辑和决策。
条件语句:用于根据条件执行不🎯同的代码块。if(age>18){printf("Youareanadult.\n");}else{printf("Youareaminor.\n");}循环语句:用于重复执行代码块。
//for循环for(inti=0;i<5;i++){printf("i=%d\n",i);}//while循环inti=0;while(i<5){printf("i=%d\n",i);i++;}
1线程库与并发编程
在现代计算机系统中,多线程编程是提高程序性能的重要手段。C语言提供了POSIX线程(pthreads)库,可以用来实现多线程编程。
#include#includevoid*thread_func(void*arg){printf("Hellofromthread!\n");returnNULL;}intmain(){pthread_tthread;pthread_create(&thread,NULL,thread_func,NULL);pthread_join(thread,NULL);return0;}
2动态数据结构
动态数据结构如链表和栈,可以根据程序需求灵活地调整其大小。
#include#includetypedefstructNode{intdata;structNode*next;}Node;//创建新节点Node*createNode(intdata){Node*newNode=(Node*)malloc(sizeof(Node));newNode->data=data;newNode->next=NULL;returnnewNode;}//插🤔入节点voidinsert(Nodehead,intdata){Node*newNode=createNode(data);if(*head==NULL){*head=newNode;}else{Node*current=*head;while(current->next!=NULL){current=current->next;}current->next=newNode;}}//打印链表voidprintList(Node*head){Node*current=head;while(current!=NULL){printf("%d->",current->data);current=current->next;}printf("NULL\n");}intmain(){Node*head=NULL;insert(&head,1);insert(&head,2);insert(&head,3);printList(head);return0;}
校对:李柱铭(f3J1ePQDlzHhwh44q38w4Ima2E3XrDq)


