c语言的scanf中的&_scanf()中缺少(&)的与号/地址(C语言错误)

c语言的scanf中的&

This is a very common mistake made by the programmers, while reading values using scanf(), we give the variable name but sometimes we forget to use ampersand/address of operator (&) with the scanf() function.

这是程序员经常犯的错误,在使用scanf()读取值时,我们给出了变量名,但有时我们忘记在scanf()函数中使用运算符 ( & )的&符号/地址

Note: It is not required to use ampersand/address of (&) operator always with the scanf() function. While using pointers and reading string (character array) values, we do not use it.

注意:不需要始终在scanf()函数中使用( & )运算符的&符号/地址。 在使用指针并读取字符串(字符数组)值时,我们不使用它。

The arguments of the scanf() function are the pointers types, we must provide either an address of a variable or a pointer (which contains the address of the variable).

scanf()函数的参数是指针类型,我们必须提供变量的地址或指针(包含变量的地址)。

Therefore, if we are using a pointer in scanf(), we don’t use address of (&) operator, because pointer contains the address itself.

因此,如果我们在scanf()中使用了指针,则不会使用( & )运算符的地址,因为指针本身包含地址。

Thus, the following statement will be wrong:

因此,以下陈述将是错误的:

int age; char gender;
scanf("%c%d",gender, age);

Here, gender and age are the normal variables that are used to store, retrieve the values that means age and gender will work with the values only, and scanf() needs address of the variables. So, the above written statement will be wrong.

在这里, 性别和年龄是用于存储,检索值的正常变量,这意味着年龄和性别仅适用于这些值,而scanf()需要这些变量的地址。 因此,上述书面声明将是错误的。

Then, what will the correct statement?

那么, 正确的陈述是什么?

Here is the correct statement to read ‘gender’ and ‘age’ through scanf()...

这是通过scanf()读取“性别”和“年龄”正确语句

int age; char gender;
scanf("%c%d",&gender, &ge);

We can also use the pointers, as scanf() accepts addresses,

我们也可以使用指针,因为scanf()接受地址,

Thus, this statement will also be correct:

因此,此语句也将是正确的:

//variable declarations
int age; char gender;
//pointer declarations and assignment
int *ptr_age = &age; char *ptr_gender = &gender;
scanf("%c%d",ptr_gender, ptr_age);

Example to read age and gender in C:

以C语言读取年龄和性别的示例:

#include <stdio.h>

int main()
{
	//variable declarations
	int age;  char gender;
	//input values
	printf("Enter gender, age (separate by space): ");
	scanf("%c%d",&gender,&age);

	printf("Gender: %c, Age: %d\n",gender,age);
	
	return 0;
}

Output

输出量

    Enter gender, age (separate by space): M 29
    Gender: M, Age: 29


翻译自: https://www.includehelp.com/c/missing-ampersand-address-of-in-scanf-c-language-error.aspx

c语言的scanf中的&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值