为什么gets(stdin)返回整数?等错误 [英] Why does gets(stdin) return an integer? And other errors

查看:134
本文介绍了为什么gets(stdin)返回整数?等错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C编程新手(尽管我有Java经验)。阅读了一些教程之后,我决定开始在 Coderbyte 上解决编码难题。

I am new to C programming (although I have experience with Java). After reading some tutorials, I decided to start solving coding challenges on Coderbyte.

我尝试的第一个挑战是这一个

The first challenge I tried was this one:


挑战


具有功能 FirstFactorial(num)采用 num >传递的参数并返回其阶乘。例如:如果 num = 4,则您的程序应返回(4 * 3 * 2 * 1) =24。对于测试用例,范围将介于1和18,并且输入将始终是整数。

Challenge

Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.

输入:4

输出:24

Input: 4
Output: 24

输入:8

输出:40320

Input: 8
Output: 40320

我的解决方案:

#include <stdio.h>

void FirstFactorial(int num[]) {

  int i = num -1;

  for(i ; i > 0; i--) {
    num = num * i;
    printf("%d",i);
  }

  printf("\t %d", num);
}

int main(void) {

  // disable stdout buffering
  setvbuf(stdout, NULL, _IONBF, 0);

  // keep this function call here
  FirstFactorial(gets(stdin));
  return 0;

}

输入参数的值: 8

错误消息:

main.c: In function 'FirstFactorial':
main.c:5:11: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
   int i = num -1;
           ^~~
main.c:8:15: error: invalid operands to binary * (have 'int *' and 'int')
     num = num * i;
               ^
main.c: In function 'main':
main.c:23:18: warning: passing argument 1 of 'FirstFactorial' makes pointer from integer without a cast [-Wint-conversion]
   FirstFactorial(8);
                  ^
main.c:3:6: note: expected 'int *' but argument is of type 'int'
 void FirstFactorial(int num[]) {
      ^~~~~~~~~~~~~~

exit status 1

似乎有一些问题,我有几个问题:

So there seems to be a few problems, and I have a few questions:


  1. 我从没听说过 gets( stdin)。我查找了 gets () ,而glibc文档说该函数返回 char * 。如何将其传递给采用 int 的函数?

  1. I've never heard of gets(stdin). I looked up gets(), and the glibc documentation says the function returns a char*. How can I pass it to a function that takes an int?

它看起来像例如

int i = num -1;

正在将 i 初始化为4而不是7。为什么?

is initializing i as 4 and not 7. Why?

for循环似乎正确地减小了 i i = 7、6、5、4、3、2、1)。但是这条语句:

The for loop seems to be decrementing i correctly (i = 7, 6, 5, 4, 3, 2, 1). But this statement:

num = num * i;

正在生成错误。怎么了看起来像是正常的乘法。

is generating an error. What is wrong with it? It looks like a normal multiplication.


推荐答案

为了将来的访客:

这是对Coderbytes用于便利的语言的严重滥用。 gets(stdin)永远不应该一开始就起作用:类型不起作用。

This is a horrible abuse of the language that Coderbytes uses for "convenience". gets(stdin) should never work in the first place: the types don't work out.

实际上是什么发生的情况是 Coderbytes盲目查找并替换了 gets(stdin)的第一个实例,并使用了您作为输入提供的文字字符串,将您的代码发送到编译器。

What's actually happening is Coderbytes is blindly finding-and-replacing the first instance of gets(stdin) with the literal string you've supplied as input, before sending your code to the compiler. This isn't even a preprocessor macro, it's a blind substitution on the source.

因此,尽管您实际上不应该这样做,但在Coderbytes上却是必不可少的恶作剧:似乎是将输入放入程序的唯一受支持的方法。

So while you should never do this in reality, on Coderbytes it's a necessary evil: this seems to be the only supported way to put input into your program.

此外,如果您想娱乐一下,请尝试清除其他所有内容并将其放入Coderbytes:

Also, if you want some amusement, try clearing out everything else and putting this into Coderbytes:

int main(){
    printf("%s", "This is a literal string containing gets(stdin) along with other words");
}

您会发现即使在字符串文字中也会发生替换!

You'll see that the substitution happens even inside string literals!

这篇关于为什么gets(stdin)返回整数?等错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆