c - printf ("\n")\n 在我的代码 VS2013 中的任何地方随机停止工作

标签 c visual-studio-2013 printf

//testing some structs
#include <stdio.h>

struct books {
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
} book;

//Function Declairations
int print_book(struct books book);

int main() {
    struct books book1;
    struct books book2;

    strcpy(book1.title, "Spot goes to the beach");
    strcpy(book1.author, "Mr Biggles");
    strcpy(book1.subject, "A story of a stupid little dog that goes to the beach and chases birds.");
    book1.book_id = 684687654;

    strcpy(book2.title, "The Cat in the Hat");
    strcpy(book2.author, "Dr Seuse");
    strcpy(book2.subject, "A mischeviouse cat come to visit and causes such a mess");
    book2.book_id = 5754454;

    printf("Available books for hire from the library\n");

    print_book(book1);
    print_book(book2);
}

int print_book(struct books book) {
    //prints the details of a books struct parsed as a parameter
    printf("\nBook Title: %s", book.title);
    printf("\nBook Author: %s", book.author);
    printf("\nBook Subject: %s", book.subject);
    printf("\nBook ID: %d", book.book_id);
    printf("\n");
}

嘿伙计们,由于某种原因\n随机停止工作。我决定在底部添加另一个 \n 只是为了将输出与最后控制台窗口中的“按任意键继续...”分开,但它现在不起作用。

我什至尝试过转到第 28 行并插入更多 \n 例如...

printf("Available b\n\n\n\n\n\n\nooks for hire from the library\n");

输出没有差异。

最佳答案

  1. int print_book(struct books book);void print_books(...)

    您没有从该函数返回任何内容

  2. 您没有包含 string.h,因此编译器不知道 strcpy() 的声明,并假设它返回int并通过默认提升获取任意数量的参数。所以你的代码会调用未定义的行为。它可能会在这次运行,但不会在其他时间或在其他系统上运行。参见

关于c - printf ("\n")\n 在我的代码 VS2013 中的任何地方随机停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177336/

相关文章:

c - 当传递的参数不足时 printf 会做什么?

c - Malloc 无法分配内存

c - 错误时返回 -1 的替代方法

c - 如何在 Linux 中正确安装 gsl 库?

c - 如何在C中打印main中的函数返回语句?

f# - 覆盖 ToString 和 `%A` 说明符

c - 获取网络中设备的 IP 地址

c# - 通用输出目录、Web 应用程序和 IIS Express

c++ - While 循环的变量未被识别为已初始化

c# - async/await 单元测试代码覆盖率