【问题标题】:undefined reference to `scanf_s'对“scanf_s”的未定义引用
【发布时间】:2015-03-17 22:05:49
【问题描述】:

我有一个课程作业要快速完成,这需要我能够以某种方式调试代码。为了完成任务,我必须能够运行给我的程序,并使用断点一步一步地指导程序。我们得到的程序是 ATM 的基本视图,并且有许多错误。

请不要修复代码中的错误,但是当我不断收到错误 'undefined参考scanf_s' 代码如下:

/* This program has been altered purposefully
   so that it contains problems.
   Use the program to complete P2-Debugging a program
   Remember to take screenshots of you do the following:

   Adding a breakpoint at an appropriate point
   Stepping into functions (F11) and over each line of code (F10)
   Changing variables and altering other code, e.g. changing messages
   Watching the values of variables.
   Add comments to the code before taking screenshots.
   Fix the problems if you can. Otherwise, just add comments to the code
   indicating where you think the problems are and what the solution might be.
   Place all evidence into one Word Document and submit it.
   Can you add other improvements?
*/
#include <stdio.h>

int getOption()
{
    int option = 0, nl;
    printf("\nWelcome to the ATM\n");

    printf("\nMenu\n");
    printf("\n1. Withdraw Cash\n");
    printf("\n2. Show Balance\n");
    printf("\n3. Exit\n");
    printf("\nEnter a number from 1 to 3:");
    option = scanf_s("%d%c", &option, &nl);

    return option;
}

//function to allow you to withdraw cash
int withdrawCash()
{
    float amount;
    int nl, option;

    printf("\nHow much money do you want?");
    amount = scanf_s("%d%c", &option, &nl);
    return option;
}

//function to show you your balance
int getBalance()
{
    float balance = 10000;
    int nl, option;

    printf("\nHow much money do you want?");
    balance = scanf_s("%d%c", &option, &nl);
    return balance;
}

//function to update your balance
int updateBalance(float balance, float amount)
{
    int nl, option;
    balance = balance - amount;
    return balance;
}


// main function - start here
int main(void)
{
    int ch;
    int opt = 0;
    int amount = 0;
    int balance = 0;
    float newbal = 0.0;

    opt = getOption();
    printf("\nYou chose option %d\n", opt);
    if (opt == 1)
    {
        amount = withdrawCash();
        newbal = updateBalance(10000, amount);
        printf("\nHere is your %d, your balance is:\n", amount, newbal);
    }
    if (opt == 2)
    {
        balance = getBalance();
        printf("\nHere is your balance: %d\n", balance);
    }

    printf("\nThank you. Please take your card.\n");
    ch = getchar();

    return 0;
}

【问题讨论】:

  • 你在哪个平台上? scanf_s 是一个 Windows 函数。
  • 我认为这应该由您自己解决以通过您的课程。
  • 请注意,scanf_s() 是 C11 标准的可选附件 K 中的一个函数。唯一实现标准函数近似的平台是 Microsoft(并且近似不是那么好——请参阅 Do you use the TR 24731 safe functions?)。
  • 我可以在 Eclipse 中使用 MinGW 和 GCC 的替代功能吗?
  • 您可以使用scanf(),但您需要查看调用顺序的差异(尽管实际上,在上下文中,我认为没有任何差异)。而且您可能不得不避免“承认不使用 Microsoft C 库。

标签: c scanf c11 tr24731


【解决方案1】:

其中之一:

  • 使用定义了scanf_s() 的Microsoft 编译器。
  • 请改用 ISO C90/C99 标准库函数 scanf()
  • 使用带有可选 ISO C11 Annex K 库支持的编译器。
  • 添加#define scanf_s scanf

但是请注意,鉴于格式说明符,您传递给 scanf_s 的参数不正确 - 它们对于 scanf 是正确的 - 这可能会建议一个首选解决方案(而且它不是最后一个 ;-))。

【讨论】:

  • "%s""%c""%[" 格式说明符用作需要更改的参数数量时,使用#define scanf_s scanf 会产生问题。
  • @chux :这是真的,除了在问题中的代码中,在使用 %c 的地方没有提供必要的附加参数,所以看起来 scanf 用于任何案子。最后一个建议不是很严肃的建议。
【解决方案2】:

类似代码存在链接器问题,并且错误地使用了scanf_s()

scanf_s() 需要 2 个参数,每个 "%s""%[""%c"char * 地址和 rsize_t 大小。

// scanf_s("%d%c", &option, &nl);
scanf_s("%d%c", &option, &nl,  (rsize_t) 1);

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 2022-01-22
    • 2016-04-04
    • 2015-11-03
    • 2011-08-11
    • 2019-07-22
    • 2015-07-20
    • 2019-06-22
    • 2012-04-02
    相关资源
    最近更新 更多