【问题标题】:Function does not return printf函数不返回 printf
【发布时间】:2021-03-20 12:13:19
【问题描述】:

当我运行这个程序时,它会打印问题并从用户那里得到答案,但它没有说明它是正确的还是错误的。请帮忙!

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

void
correct (char str[])
{
    char c1 = "Cristoforo";
    char c2 = "cristoforo";
    char c3 = "CRISTOFORO";
    int conf = strcmp (str, c1);
    int conf1 = strcmp (str, c2);
    int conf2 = strcmp (str, c3);
    if (conf1 == 0 || conf == 0 || conf2 == 0)
    {
        printf ("The answer is correct");
    }
    else
    {
        printf ("The answer is wrong");
    }
}

int
main ()
{
  char s[] = "What's the name of the explorere Colombus?\n";
  char r[100];
  printf ("%s", s);
  gets (r);
  void correct (r);
}

【问题讨论】:

  • 因为你根本没有调用函数correctvoid correct(r); 只是声明它,而不是调用它。
  • 注意编译器警告 - 代码还有其他问题,例如 char c1 = "Cristoforo"; 应该是 char c1[] = "Cristoforo";

标签: c printf


【解决方案1】:

void correct(r) 是声明,它的意思是——正确的是一个函数,它接受 r 类型的参数并返回 void。只需删除 void 即可调用函数。 correct(r)。并且您需要将chars 更改为char *const char*

    const char *c1 = "Cristoforo";
    const char *c2 = "cristoforo";
    const char *c3 = "CRISTOFORO";

【讨论】:

    【解决方案2】:

    由于strcmp() 使用不正确,您会收到seg 错误。第二个参数必须是const char *__s2。那么正确的是:

    int conf = strcmp(str, "Cristoforo");
    int conf1 = strcmp(str, "cristoforo");
    int conf2 = strcmp(str, "CRISTOFORO");
    

    并在调用correct(r)时删除void

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 2013-11-25
      • 2021-08-14
      相关资源
      最近更新 更多