C Primer Plus:(第十六章)C预处理器和C库

预处理器:编译器机器检查源代码,并执行一些文本替换

明示常量 / 符号常量:#define

#define用来定义明示常量(mainfest constant) / 符号常量,作用域为:该指令处到#undef 或 结尾EOF

#define PX printf("X is %d.\n", x)
预处理器指令 宏 替换体 
#ifdef ONLINE_JUDGE
#else 
	freopen("test.txt","r",stdin);
#endif

memcpy()和memmove()

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10

void PrintArray(const int [], int);

//_Static_assert(sizeof(double) != 2 * sizeof(int), "The size of double is not 2 time to int in this machine.");

int main()
{
    int ori[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int aim[SIZE];
    double darr[SIZE / 2] = {11.1, 11.2, 11.3, 11.4, 11.5};

    PrintArray(ori, SIZE);
    memcpy(aim, ori, SIZE * sizeof(int));
    puts("After memcpy(aim, ori, SIZE * sizeof(int)) aim = :");
    PrintArray(aim, SIZE);
    //1 2 1 2 3 4 5 8 9 10

    memcpy(ori + 2, ori, SIZE / 2 * sizeof(int));
    puts("After memmove(ori + 2, ori, SIZE / 2 * sizeof(int)) aim = :");
    PrintArray(ori, SIZE);
    //1 2 1 2 1 2 3 8 9 10  WRONG

    memmove(ori + 2, ori, SIZE / 2 * sizeof(int));
    puts("After memmove(ori + 2, ori, SIZE / 2 * sizeof(int)) aim = :");
    PrintArray(ori, SIZE);

    memcpy(aim, darr, SIZE / 2 * sizeof(double));
    puts("After  memcpy(aim, darr, SIZE / 2 * sizeof(double)) aim = :");
    PrintArray(aim, SIZE / 2);
    PrintArray(aim + 5, SIZE / 2);
}

void PrintArray(const int arr[], int n)
{
    int i;
    for(i = 0; i < n; ++i)
    {
        printf("%d", arr[i]);
        printf("%s", i < n - 1 ? " " : "\n");
    }
}

发布了316 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/105228481