什么是“&"在"scanf(“%f",& variableName);"中在C编程中? [英] What is "&" in "scanf("%f", &variableName);" in C programming?

查看:79
本文介绍了什么是“&"在"scanf(“%f",& variableName);"中在C编程中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习C编程语言,由于&"而陷入困境,下面是示例代码.

I am currently studying C programming language and I was stuck because of "&", below is the sample code.

int main()
{
     float degrees, radians;
     printf("Enter an angle in degrees: ");
     scanf("%f", &degrees);
     radians = 0.0174532925 * degrees;
     printf("%.2f degrees is %.2f radians.\n", degrees, radians);
     return(0);
}

请查看粗体和斜体代码.我想知道&"的用途.因为当我尝试运行不带&"的代码时以度为单位,它确实可以运行,但是在生成答案时会挂断电话.

Please take a look the bold and italic code. I want to know what is the use of "&". Because when I try to run the code that has no "&" in degrees it does run but when generating the answer it will hang-up.

感谢您的即时答复.谢谢!

Appreciate your immediate answer. Thank you!

推荐答案

&符是C地址运算符;它返回给定变量的内存地址.

The ampersand (&) is the C Address Operator; it returns the memory address of the given variable.

scanf函数从控制台读取数据,进行解析,并将结果存储在所提供变量的内存地址中.理解&"号的关键是scanf系列函数实际上会在内存中更改作为参数提供的变量.由于我们正在使用的是内存中的直接变量而不是副本,因此我们需要传递该变量的内存地址-因此使用&号.

The scanf function reads data from the console, parses it, and stores the result in the memory address of the variable provided. The key to understanding the ampersand is that the scanf family of functions actually changes, in memory, the variable provided as an argument. Since we're working with the direct variable in memory and not a copy, we need to pass that variable's memory address -- hence the ampersand (&).

在示例代码中,行 scanf(%f",& Degrees)可以读为将浮点数(%f")值读入度数的内存地址中".

In your sample code the line scanf("%f", &degrees) can be read as "Read a float ("%f") value into the memory address of degrees".

您可以通过添加以下代码行来查看作为scanf参数传递的内存地址: printf(度数的内存地址:%p",& degrees); 返回声明.

You can view the memory address being passed as an argument to scanf by adding the following line of code: printf("memory address of degrees: %p", &degrees); before the return statement.

有关更多详细信息,请参阅以下内容: http://see-programming.blogspot.com/2013/10/difference-between-asterisk-and.html http://beej.us/guide/bgc/output/html/multipage/scanf.html

Have a look at the following for more details: http://see-programming.blogspot.com/2013/10/difference-between-asterisk-and.html http://beej.us/guide/bgc/output/html/multipage/scanf.html

这篇关于什么是“&"在"scanf(“%f",& variableName);"中在C编程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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