【问题标题】:Scan an already printed value扫描已打印的值
【发布时间】:2017-02-25 19:42:03
【问题描述】:

我正在尝试使用“scanf”函数将其作为输入后使用的数字。例如,如果我输入 2,我想知道如何在代码的后期使用它(也许用它调用另一个函数)。

#include<stdio.h>
int main(){

    int Input;
    scanf("%d",Input);
    printf("%d", Input);
    //here is the place where I want to use the Input

    return 0;
}

在代码示例中,在命令 printf 之后我应该如何进一步开发代码。

【问题讨论】:

  • 如果该程序有效,您已经获取了该号码并将其提供给printf。只需将其提供给不同的功能即可。
  • scanf 的调用是错误的。您必须传递一个指向Input 的指针,而不是Input 的值。
  • 我怀疑这是一个XY 问题,你到底想达到什么目的?
  • 如果不打印值,我可以使用扫描的值来调用函数吗? @jxh
  • 仅此而已??哦... :) @sergej 谢谢。

标签: c printf scanf


【解决方案1】:

我不确定这是否能回答您的问题。您不必打印该值即可使用它。从 scanf 获取后,您可以在任何地方轻松使用它。

#include<stdio.h>

int inc(int input) {
    int val = input + 1;
    return val;
}

int main(){

    int Input;
    scanf("%d",&Input); //dont forget &
    //now you have the input saved, do anything you want with Input
    printf("%d",Input); // you can print the value you scanned
    int out = inc(Input); //you can put it in a new function
    printf("%d %d",Input,out); //you can output it again, "printf" is also another function

    return 0;
}

【讨论】:

  • 我想要的东西..谢谢你的帮助..我是新来的绳索... :) @AyonNahiyan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 2019-07-27
  • 2018-02-08
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2015-08-14
相关资源
最近更新 更多