为什么不scanf函数需要字符串的符号和printf中也能正常工作(C语言)? [英] Why doesn't scanf need an ampersand for strings and also works fine in printf (in C)?

查看:168
本文介绍了为什么不scanf函数需要字符串的符号和printf中也能正常工作(C语言)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习关于C字符串了。

I am learning about strings in C now.

如何来使用scanf函数得到一个字符串,你可以做

How come to use scanf to get a string you can do

scanf("%s",str1);

和printf的,你可以做

and for printf you can do

printf("The string is %s\n", str1);

我明白,scanf函数这是因为字符串就是一个字符数组是一个指针,但对于printf的,是什么让你可以把变量名,就像你会为一个整数或浮点数?

I understand that for scanf it is because the string is just a character array which is a pointer, but for printf, how is it that you can just put the variable name just like you would for an int or float?

推荐答案

scanf函数需要变量的地址读入和字符串缓冲区已经重新presented的地址(指针指向内存中的位置,或者分解成一个指针数组)。

scanf needs the address of the variable to read into, and string buffers are already represented as addresses (pointer to a location in memory, or an array that decomposes into a pointer).

的printf 不相同,治疗%S 作为指针到字符串。

printf does the same, treating %s as a pointer-to-string.

这篇关于为什么不scanf函数需要字符串的符号和printf中也能正常工作(C语言)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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