【发布时间】:2021-03-12 07:50:53
【问题描述】:
我目前正在学习 C 编程,我遇到了这个奇怪的输出。
// Program will try functionalities of the scanf function
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
char array[40];
scanf("Enter your address: %39s",array); //39 + 1/o null character at the end of the character array.
printf("The address is : %s", array);
return 0;
}
它应该接受输入地址并输出相同。
PS C:\Users\G\Documents\C programs> gcc scanff.c -o scanff -Wall -pedantic -std=c99
PS C:\Users\G\Documents\C programs> ./scanff
jhgjhg
The address is : ■ a
那个黑块是什么?谁能解释一下! 谢谢。
【问题讨论】:
-
array表示基地址,所以使用%u或%p作为printf("%u", array);使用scanf是错误使用scanf("%s",array); -
@csavvy 通过地址,我的意思是输出我在字符变量中输入的字符串并输出相同的内容。不是变量的地址。此外,对于数组 AFAIK,我们可以使用 scanf(%s, array)