C语言面试全方位详解

发布时间:2023-05-21

一、基础知识

1、C语言数据类型 C语言数据类型分为基本数据类型和派生数据类型,基本数据类型包括intfloatdoublechar等,派生数据类型包括数组、结构体、共用体和指针。

#include <stdio.h>
int main() {
    int a = 10;
    float b = 3.14;
    double c = 2.718281828;
    char d = 'a';
    int arr[5] = {1, 2, 3, 4, 5};
    struct Student {
        char name[20];
        int age;
    } stu;
    union Test {
        int a;
        float b;
        char c[10];
    } test;
    int *p = &a;
    printf("%d\n", a);
    printf("%.2f\n", b);
    printf("%.9lf\n", c);
    printf("%c\n", d);
    printf("%d\n", arr[0]);
    printf("%d\n", sizeof(stu));
    printf("%d\n", sizeof(test));
    printf("%d\n", *p);
    return 0;
}

2、变量和常量 变量是程序运行过程中值可以改变的量,常量是值不可改变的量。在C语言中,常量可以使用#defineconst关键字来定义。

#include <stdio.h>
#define MAX_NUM 100
const float PI = 3.1415926;
int main() {
    int num = 10;
    float radius = 5.0;
    float area = PI * radius * radius;
    printf("%d\n", MAX_NUM);
    printf("%.2f\n", area);
    printf("%d\n", num);
    return 0;
}

3、运算符 C语言支持多种运算符,包括算术运算符、关系运算符、逻辑运算符、位运算符和赋值运算符等。

#include <stdio.h>
int main() {
    int a = 10, b = 3;
    printf("%d\n", a + b);   // 13
    printf("%d\n", a - b);   // 7
    printf("%d\n", a * b);   // 30
    printf("%d\n", a / b);   // 3
    printf("%d\n", a % b);   // 1
    printf("%d\n", a < b);   // 0
    printf("%d\n", a > b);   // 1
    printf("%d\n", a == b);  // 0
    printf("%d\n", a && b);  // 1
    printf("%d\n", a | b);   // 11
    printf("%d\n", a <<= 1); // 20
    return 0;
}

二、指针和数组

1、指针 指针是用来存储地址的变量,其本身也有一个地址。指针变量需要通过&运算符获取地址,通过*运算符获取地址所存储的值。

#include <stdio.h>
int main() {
    int a = 10;
    int *p;
    p = &a;
    printf("%d\n", *p);
    printf("%p\n", p);
    printf("%p\n", &p);
    return 0;
}

2、数组 数组是用来存储一组相同类型的数据的结构,使用[]运算符可以访问数组元素。数组的下标从0开始。

#include <stdio.h>
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int i, sum = 0;
    for (i = 0; i < 5; i++) {
        sum += arr[i];
    }
    printf("%d\n", sum);
    return 0;
}

三、函数和指针

1、函数 函数是一段完成特定任务的代码块,函数可以接收参数,也可以返回值。

#include <stdio.h>
int add(int a, int b) {
    return a + b;
}
int main() {
    int result = add(10, 20);
    printf("%d\n", result);
    return 0;
}

2、指针 指针可以作为函数的参数和返回值,可以用来实现动态内存分配。

#include <stdio.h>
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
int main() {
    int a = 10, b = 20;
    printf("Before swap: a = %d, b = %d\n", a, b);
    swap(&a, &b);
    printf("After swap: a = %d, b = %d\n", a, b);
    return 0;
}

四、结构体和共用体

1、结构体 结构体是由多个不同类型的数据组成的自定义类型,可以使用.运算符访问结构体成员。

#include <stdio.h>
struct Student {
    char name[20];
    int age;
    float score;
};
int main() {
    struct Student stu = {"John", 18, 95.5};
    printf("name: %s\n", stu.name);
    printf("age: %d\n", stu.age);
    printf("score: %.1f\n", stu.score);
    return 0;
}

2、共用体 共用体是一种特殊的数据类型,内部的所有成员共用同一块内存空间,只能存储其中的一个成员。

#include <stdio.h>
union Test {
    int a;
    float b;
    char c[10];
};
int main() {
    union Test test;
    test.a = 123;
    printf("%d\n", test.a);
    printf("%.2f\n", test.b); 
    test.b = 3.14;
    printf("%d\n", test.a);
    printf("%.2f\n", test.b);
    test.c[0] = 'h';
    test.c[1] = 'i';
    test.c[2] = '\0';
    printf("%s\n", test.c);
    return 0;
}

五、内存管理

1、动态内存分配 动态内存分配是指在程序运行时根据需要动态地为变量分配内存空间,可以使用malloccallocrealloc函数来分配和释放内存。

#include <stdio.h>
#include <stdlib.h>
int main() {
    int *p = (int *)malloc(5 * sizeof(int));
    int i;
    for (i = 0; i < 5; i++) {
        p[i] = i + 1;
    }
    for (i = 0; i < 5; i++) {
        printf("%d ", p[i]);
    }
    printf("\n");
    free(p);
    return 0;
}

2、内存管理函数 可以使用memsetmemcpy函数对内存进行管理,其中memset可以将内存中的某一区域赋为相同的值,memcpy可以将一个内存区域的值复制到另一个内存区域。

#include <stdio.h>
#include <string.h>
int main() {
    char str[20] = "hello";
    char dest[20];
    memset(dest, 0, sizeof(dest));
    memcpy(dest, str, sizeof(str));
    printf("%s\n", dest);
    return 0;
}