Selection of C language programming: analysis of classic code examples for mastering key skills

C language is a very influential programming language, which is widely used in various computer systems and software development fields. This article will introduce you to 8 selected examples of C language programming to help you better master key skills and analyze classic codes.
insert image description here

  1. Output strings in reverse order
    In C language, strings are character arrays, and you can output strings in reverse order by traversing the array. Here is sample code:
#include <stdio.h>
#include <string.h>

void reverse_string(char *str) {
    
    
    int i, length = strlen(str);
    for (i = length - 1; i >= 0; i--) {
    
    
        printf("%c", str[i]);
    }
}

int main() {
    
    
    char str[] = "Hello, World!";
    reverse_string(str);
    return 0;
}

insert image description here

  1. Solve quadratic equations in one variable

Using C language to solve the quadratic equation in one variable, you can calculate the real root or imaginary root by judging the discriminant. Here is sample code:

#include <stdio.h>
#include <math.h>

void quadratic_solver(double a, double b, double c) {
    
    
    double delta = b * b - 4 * a * c;
    if (delta > 0) {
    
    
        double x1 = (-b + sqrt(delta)) / (2 * a);
        double x2 = (-b - sqrt(delta)) / (2 * a);
        printf("Two real roots: x1 = %lf, x2 = %lf\n", x1, x2);
    } else if (delta == 0) {
    
    
        double x = -b / (2 * a);
        printf("One real root: x = %lf\n", x);
    } else {
    
    
        printf("No real roots.\n");
    }
}

int main() {
    
    
    double a = 1, b = -3, c = 2;
    quadratic_solver(a, b, c);
    return 0;
}
  1. Find the maximum and minimum value in an array

In C language, you can traverse an array to find the maximum and minimum values. Here is sample code:

#include <stdio.h>

void find_max_min(int arr[], int size, int *max, int *min) {
    
    
    *max = arr[0];
    *min = arr[0];
    for (int i = 1; i < size; i++) {
    
    
        if (arr[i] > *max) {
    
    
            *max = arr[i];
        }
        if (arr[i] < *min) {
    
    
            *min = arr[i];
        }
    }
}

int main() {
    
    
    int arr[] = {
    
    2, 5, 8, 1, 0, -3, 12};
    int max, min;
    int size = sizeof(arr) / sizeof(arr[0]);
    find_max_min(arr, size, &max, &min);
    printf("Max: %d, Min: %d\n", max, min);
    return 0;
}
  1. Calculate factorial

Factorial is a common mathematical operation, and a simple recursive function can be implemented in C language to calculate factorial. Here is sample code:

#include <stdio.h>

unsigned long long factorial(int n) {
    
    
    if (n == 0) {
    
    
        return 1;
    } else {
    
    
        return n * factorial(n - 1);
    }
}

int main() {
    
    
    int n = 10;
    printf("%d! = %llu\n", n, factorial(n));
    return 0;
}

insert image description here

  1. Fibonacci sequence

The Fibonacci sequence is a very famous sequence, and the calculation of the Fibonacci sequence can be realized in a recursive or circular manner. Here is sample code:

#include <stdio.h>

int fibonacci(int n) {
    
    
    if (n <= 1) {
    
    
        return n;
    } else {
    
    
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

int main() {
    
    
    int n = 10;
    printf("The %dth Fibonacci number is: %d\n", n, fibonacci(n));
    return 0;
}
  1. string concatenation

In C language, you can use the string pointer and array traversal to realize the string splicing function. Here is sample code:

#include <stdio.h>
#include <string.h>

void string_concat(char *dest, const char *src) {
    
    
    int dest_len = strlen(dest);
    int src_len = strlen(src);

    for (int i = 0; i <= src_len; i++) {
    
    
        dest[dest_len + i] = src[i];
    }
}

int main() {
    
    
    char dest[50] = "Hello, ";
    const char *src = "World!";
    string_concat(dest, src);
    printf("Concatenated string: %s\n", dest);
    return 0;
}
  1. binary search

Binary search is an efficient search algorithm that works on sorted arrays. Here is sample code:

#include <stdio.h>

int binary_search(int arr[], int size, int target) {
    
    
    int left = 0, right = size - 1;
    while (left <= right) {
    
    
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) {
    
    
            return mid;
        } else if (arr[mid] < target) {
    
    
            left = mid + 1;
        } else {
    
    
            right = mid - 1;
        }
    }
    return -1;
}

int main() {
    
    
    int arr[] = {
    
    1, 3, 5, 7, 9, 11, 13};
    int target = 7;
    int size = sizeof(arr) / sizeof(arr[0]);
    int index = binary_search(arr, size, target);
    printf("Index of %d is: %d\n", target, index);
    return 0;
}
  1. dynamic memory allocation

In C language, you can use dynamic memory allocation functions (such as malloc and free) to implement dynamic arrays. Here is sample code:

#include <stdio.h>
#include <stdlib.h>

int main() {
    
    
    int n = 5;
    int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
    
    
    printf("Memory allocation failed.\n");
    return 1;
}

for (int i = 0; i < n; i++) {
    
    
    arr[i] = i * 2;
}

printf("Dynamic array: ");
for (int i = 0; i < n; i++) {
    
    
    printf("%d ", arr[i]);
}
printf("\n");

free(arr);
return 0;
}

insert image description here

The above are 8 selected examples of C language programming, hoping to help you improve your programming skills. In fact, there are many other practical techniques and methods in C language. Through continuous learning and practice, you will be able to master C language programming more deeply.

Guess you like

Origin blog.csdn.net/tuzajun/article/details/130396745