为什么我们不在printf中使用&符号(&)以及为什么我们在scanf中使用它? [英] Why we dont use ampersand(&) in printf and why we use it in scanf ?
问题描述
在扫描f中未使用&符号(&)时,表明程序已停止工作。为什么我们只在c语言的scanf中使用它...请详细解释我&用语言简单说明
我尝试了什么:
当我是尝试运行程序没有&符号(&)然后显示,程序停止工作
&是一个运算符,它解析为变量的地址,所以scan函数获取写入结果的地址(或指针)。
%是打印函数中的格式前缀。它就像一个标志,表示现在有特殊字符即将来临。
请阅读som基础编程教程。 ; - )
在C
编程语言中,函数参数按值按值。因此,被调用函数对其参数的任何修改都对调用者隐藏,例如尝试
#include < stdio.h >
void square_it( int n)
{
n = n * n;
printf( square_it内,n =%d \ n,n);
}
int main()
{
int i = 5 ;函数调用前
printf( ,i =%d \ n,i) ;
square_it(i);
printf( 函数调用后,i =%d \ n,i) ;
return 0 ;
}
所以,square_it
无法制作对调用者可见的更改。
指针是克服这种困难的有效方法:指针仍然按值传递(因此称为fucntion不能修改指针值),无论如何它们提供读取/ write 访问指向的值。尝试:
#include < stdio.h >
void square_it2( int * p)
{
* p = * p * * p;
printf( square_it2,* p =%d \ n,* p );
}
int main()
{
int i = 5 ;函数调用前
printf( ,i =%d \ n,i) ;
square_it2(& i); // 将poitner传递给i,即我的地址
printf( ,i =%d \ n,i);
return 0 ;
}
现在,虽然printf
不需要更改其参数(它是一个输出函数)scanf
确实(它是一个输入函数)。因此它需要变量指针(地址)作为参数。
首先阅读C文档。
&的用法详细说明
&是关于你对变量的处理方法。
printf从变量中获取值
scanf将值写入变量并需要变量adres这样做
when not using ampersand(&) in scan f, then showing that the program has stopped working. why we use it only in scanf in c language.. please explain me in detail & in easy language
What I have tried:
when i am trying to run program without ampersand(&) then showing, program stoppted working
The & is a operator, which resolves to "address of the variable", so the scan function gets the address (or pointer) to write the result.
The % is a formatting prefix in the print function. It is like a flag signaling that now special characters are coming.
Please read som fundamental programming tutorials. ;-)
InC
programming language, function arguments are passe by value. Hence any modification the called function makes to its arguments is hidden to the caller, try for instance
#include <stdio.h> void square_it( int n ) { n = n * n; printf("inside square_it, n = %d\n", n); } int main() { int i = 5; printf("before function call, i = %d\n", i); square_it( i ); printf("after function call, i = %d\n", i); return 0; }
So, there's no way forsquare_it
to make changes visible to the caller.
Pointers are an effective way to overcome such a difficulty: pointers are still passed by value (hence called fucntion cannot modify the pointer value), anyway they provide read/write access to the pointed value. Try:
#include <stdio.h> void square_it2( int * p ) { *p = *p * *p; printf("inside square_it2, *p = %d\n", *p); } int main() { int i = 5; printf("before function call, i = %d\n", i); square_it2( &i ); // passing a poitner to i, that is i address printf("after function call, i = %d\n", i); return 0; }
Now, whileprintf
doesn't need to change its arguments (it is an output function)scanf
does (it is an input function). Hence it needs variable pointers (addresses) as arguments.
Start by reading C documentation.
The usage of & is explain in detail
& is about what you do with a variable.
printf get a value from a variable
scanf write a value to a variable and need the variable adres to do so.
这篇关于为什么我们不在printf中使用&符号(&)以及为什么我们在scanf中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!