Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用scanf()获取整数_C - Fatal编程技术网

如何使用scanf()获取整数

如何使用scanf()获取整数,c,C,当我编译时,引入一个整数,并期望main中的number int变量具有引入的值,但未设置该值 我的代码中有这个,但我无法使用scanf函数设置diag变量。diag已经是一个指针,因此在调用scanf时不需要运算符的地址: 但是那样做有点愚蠢。您无法检查错误的输入。 更好: diag已经是一个指针,因此在调用scanf时不需要运算符的地址: 但是那样做有点愚蠢。您无法检查错误的输入。 更好: 在整个编码过程中使用scanf的最佳方法是记住以下规则 #include <stdio.h>

当我编译时,引入一个整数,并期望main中的number int变量具有引入的值,但未设置该值

我的代码中有这个,但我无法使用scanf函数设置diag变量。

diag已经是一个指针,因此在调用scanf时不需要运算符的地址:

但是那样做有点愚蠢。您无法检查错误的输入。 更好:

diag已经是一个指针,因此在调用scanf时不需要运算符的地址:

但是那样做有点愚蠢。您无法检查错误的输入。 更好:

在整个编码过程中使用scanf的最佳方法是记住以下规则

#include <stdio.h>

int main(void)
{
    int foo;

    if (!pedir_diag(&foo)) {
        fputs("Input error :(\n\n", stderr);
    }
    else {
        // have fun with foo
    }
}
scanf所做的是,它将具有一定长度限制的输入内容存储到内存地址。因此,要么:

scanf("%[direction_of_what_to_scan][special format character]",
    Pointer_on_some_memory_location);

两者都是同一事物的不同实现,它们在使用指针这一部分上有所不同,C是众所周知的,现在甚至是适用的

#include <stdio.h>

int main(void)
{
    int foo;

    if (!pedir_diag(&foo)) {
        fputs("Input error :(\n\n", stderr);
    }
    else {
        // have fun with foo
    }
}
scanf所做的是,它将具有一定长度限制的输入内容存储到内存地址。因此,要么:

scanf("%[direction_of_what_to_scan][special format character]",
    Pointer_on_some_memory_location);


两者都是同一事物的不同实现,它们在使用指针方面有所不同,而C是众所周知的,甚至现在也适用于指针。

应该是scanf%i,diag;?你想把输入值存储到地址diag的int中吗?它不起作用。它不起作用并不是对问题的描述。我是说他说的那种特殊情况。尝试使用void函数int*float*等将任何类型设置为返回值。无论如何,我将在标题中对其进行更改。您应该编辑以包含一个显示您实际使用该函数的方式的类型,并包含您希望获得的输入以及改为获得的输入。或者,任何编译器错误。它不应该是scanf%i,diag;?你想把输入值存储到地址diag的int中吗?它不起作用。它不起作用并不是对问题的描述。我是说他说的那种特殊情况。尝试使用void函数int*float*等将任何类型设置为返回值。无论如何,我将在标题中对其进行更改。您应该编辑以包含一个显示您实际使用该函数的方式的类型,并包含您希望获得的输入以及改为获得的输入。或者,任何编译器错误。从不知道bool在C中可用,Thanks@Mayur它基本上是一个定义bool的整数类型。但是如果我的记忆正确,从C99开始定义bool,true和false。我从来不知道bool在C中可用,Thanks@Mayur它基本上是一个定义bool的整数类型。但是如果我的记忆正确的话,从C99开始定义bool,true和false。
scanf("%[direction_of_what_to_scan][special format character]",
    Pointer_on_some_memory_location);
int n1;
scanf("%d",&n); //which means the address of variable n1 in memory
int *n2 = &n1;
scanf("%d",n) // &doesn't need to be used cause n2 is now already pointer to an integer