printf(“\ n”)\ n在代码VS2013的任何地方随机停止工作

时间:2015-08-24 08:09:00

标签: 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's,例如......

printf(“可从b \ n \ n \ n \ n \ n \ n \ nooks租用图书馆\ n”);

输出没有区别。

1 个答案:

答案 0 :(得分:0)

  1. int print_book(struct books book); - &gt; void print_books...
  2. 您没有从该功能返回任何内容

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