C语言语法

C语言语法

#include 
#include 
#include 

#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语言)