C Language: A Super Practical Computing Programming Language

This article mainly introduces some code examples of C language, and the explanations are very detailed, which can provide certain reference value for everyone’s study or work. Friends in need can refer to it!

Here are some code examples of C language:

Basic Syntax Example

// Hello, World! program
#include <stdio.h> 
int main() {
    printf("Hello, World!\n");
    return 0;
}
// Basic data types example
#include <stdio.h> 
int main() {
    int num = 10;
    float pi = 3.14;
    char letter = 'A';
    printf("num = %d\n", num);
    printf("pi = %.2f\n", pi);
    printf("letter = %c\n", letter);
    return 0;
}
// Control flow statements example
#include <stdio.h> 
int main() {
    int score = 85;
    if (score >= 60) {
        printf("Passed\n");
    } else {
        printf("Failed\n");
    }
    int i;
    for (i = 0; i < 5; i++) {
        printf("%d ", i);
    }
    printf("\n");
    int j = 0;
    while (j < 3) {
        printf("%d ", j);
        j++;
    }
    printf("\n");
    int k = 0;
    do {
        printf("%d ", k);
        k++;
    } while (k < 2);
    printf("\n");
    return 0;
}

Algorithm Example

// Quick sort algorithm example
#include <stdio.h>

// Swap the values of two elements
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// Partition operation
int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j < high; j++) {
        if (arr[j] <= pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

// Quick sort recursive function
void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pivotIndex = partition(arr, low, high);
        quickSort(arr, low, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, high);
    }
}

// Test quick sort
int main() {
    int arr[] = {10, 7, 8, 9, 1, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    quickSort(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
// Binary search algorithm example
#include <stdio.h>

// Binary search recursive function
int binarySearch(int arr[], int low, int high, int target) {
    if (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == target) {
            return mid;
        } else if (arr[mid] > target) {
            return binarySearch(arr, low, mid - 1, target);
        } else {
            return binarySearch(arr, mid + 1, high, target);
        }
    }
    return -1;
}

// Test binary search
int main() {
    int arr[] = {1, 3, 5, 7, 9, 11, 13, 15};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target = 7;
    int result = binarySearch(arr, 0, n - 1, target);
    if (result == -1) {
        printf("Element not found\n");
    } else {
        printf("Element found at index %d\n", result);
    }
    return 0;
}

Mathematical Calculation Example

// Calculate the sum of 1+2+3+...+n
#include <stdio.h> 
int main() {
    int n;
    printf("Please enter an integer:\n");
    scanf("%d", &n);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    printf("1+2+3+...+%d=%d\n", n, sum);
    return 0;
}
// Check if a number is a leap year
#include <stdio.h> 
int main() {
    int year;
    printf("Please enter a year:\n");
    scanf("%d", &year);
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        printf("%d is a leap year\n", year);
    } else {
        printf("%d is not a leap year\n", year);
    }
    return 0;
}

Data Structure Operation Example

// Linked list node structure definition
typedef struct ListNode {
    int data;
    struct ListNode *next;
} ListNode;

// Reverse linked list
ListNode* reverseList(ListNode* head) {
    ListNode *prev = NULL;
    ListNode *current = head;
    while (current != NULL) {
        ListNode *nextNode = current->next;
        current->next = prev;
        prev = current;
        current = nextNode;
    }
    return prev;
}

// Test linked list reversal
int main() {
    // Build a simple linked list 1 -> 2 -> 3 -> 4 -> 5
    ListNode *head = (ListNode *)malloc(sizeof(ListNode));
    head->data = 1;
    ListNode *node2 = (ListNode *)malloc(sizeof(ListNode));
    node2->data = 2;
    head->next = node2;
    ListNode *node3 = (ListNode *)malloc(sizeof(ListNode));
    node3->data = 3;
    node2->next = node3;
    ListNode *node4 = (ListNode *)malloc(sizeof(ListNode));
    node4->data = 4;
    node3->next = node4;
    ListNode *node5 = (ListNode *)malloc(sizeof(ListNode));
    node5->data = 5;
    node4->next = node5;
    node5->next = NULL;

    // Reverse linked list
    ListNode *newHead = reverseList(head);

    // Output the reversed linked list
    ListNode *current = newHead;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    return 0;
}

String Operation Example

// String reversal
#include <stdio.h>
#include <string.h>

void invert(char *s) {
    int i, j, k;
    char t;
    k = strlen(s);
    for (i = 0, j = k - 1; i < k / 2; i++, j--) {
        t = *(s + i);
        *(s + i) = *(s + j);
        *(s + j) = t;
    }
}

int main() {
    char str[200];
    printf("Enter a string: ");
    scanf("%s", str);
    invert(str);
    printf("%s\n", str);
    return 0;
}
// String concatenation
#include <stdio.h>
#include <string.h>

void strccat(char a[], char b[]) {
    int i, j;
    for (i = 0; a[i]; i++);
    for (j = 0; a[i + j] = b[j]; j++);
}

int main() {
    char a[100], b[100];
    gets(a);
    gets(b);
    strccat(a, b);
    puts(a);
    return 0;
}

Note: For reference only

Leave a Comment