C语言语法
最编程
2024-08-12 09:05:34
...
C语言语法
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 5
// 定义一个名为add的函数,用于计算两个整数的和
int add(int a, int b) {
int sum = a + b;
return sum; // 返回计算结果
}
void *thread_function(void *thread_id) {
long tid;
tid = (long)thread_id;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main() {
printf("Hello, World!\n");
int x = 10;
if (x > 5) {
printf("x>5\n");
} else {
printf("x<不大于>5\n");
}
int result = add(3, 5); // 调用add函数,并将返回值赋给result
printf("3 + 5 = %d\n", result);
int *ptr; // 声明一个指向整数的指针
int var = 5;
printf("ptr=%d\n",ptr);
ptr = &var; // ptr指向变量var的地址
printf("ptr=%d\n",*ptr);//*ptr 取变量指针值
printf("var=%d\n",var);
int num = 2;
switch (num) {
case 1:
printf("num is 1\n");
break;
case 2:
printf("num is 2\n");
break;
case 3:
printf("num is 3\n");
break;
default:
printf("num is not 1,2or3\n");
}
int i;
for (i = 0; i < 5; i++) {
printf("this i value:%d\n", i);
}
// int arr[] = {10, 20, 30};
// int *ptr=arr; // 指向数组的第一个元素
// ptr++; // 指针加1,指向数组的第二个元素
pthread_t threads[NUM_THREADS];
int rc;
long t;
for (t = 0; t < NUM_THREADS; t++) {
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, thread_function, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}
上一篇: 探索C语言递归技巧:新手入门指南
推荐阅读
-
C语言内存管理的重要性
-
"解析 C 标准库的功能和特点"
-
C++实现:视频教程演示YUV420与RGB相互转换(第二部分)
-
我在家闲着无聊时发现,Java绝对是世界上最出色的编程语言!
-
Go语言中的Map(集合)教程
-
OpenCV-based Object Tracking in C++/Python (使用C++/Python进行基于OpenCV的目标追踪)
-
KCF代码运行并显示效果(C++)
-
使用opencv3和C++编程语言实现简单目标跟踪的Tracker
-
在线代码执行工具(支持PHP,Java,C++等语言)-访问 http://www.it1352.com/Onlinetools
-
C/C++多线程:参数传递的方法