简体   繁体   English

为什么 C 程序使用 Scanf 给出奇怪的 output?

[英]Why is the C program giving weird output using Scanf?

I'm currently learning C programming and I have come across this weird output.我目前正在学习 C 编程,我遇到了这个奇怪的 output。

// 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;
}

It should accept input address and output the same.它应该接受输入地址和 output 相同。

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

What is that black block?那个黑块是什么? Could someone explain.有人可以解释一下。 Thanks.谢谢。

Just use the format specifier correctly for the scanf() function and display the custom message using printf() .只需为scanf() function 正确使用格式说明符并使用printf()显示自定义消息。

Have a look at the following code:看看下面的代码:

// Program will try functionalities of the scanf function
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char const *argv[])
{
    char array[40];
    printf("Enter your address: ");
    scanf("%39s",array);   //39 + 1/o null character at the end of the character array.
    printf("The address is : %s", array);
    return 0;
}

You should look at the documentation of scanf again.您应该再次查看 scanf 的文档。 The format string should contain format specifications and non-format characters which are to be matched literally.格式字符串应包含格式规范和要按字面匹配的非格式字符。 You seem to assume that you can prompt using scanf() , that's not correct.您似乎认为可以使用scanf()进行提示,这是不正确的。

You might have more success with this (untested) code:使用此(未经测试的)代码,您可能会取得更大的成功:

int main(int argc, char const *argv[])
{
    char array[40];
    printf("Enter your address: ");
    scanf("%39s",array);   //39 + 1/o null character at the end of the character array.
    printf("The address is : %s\n", array);
    return 0;
}

It is also good practive to check the result of the scanf() call to see whether the expected number of values was read.检查scanf()调用的结果以查看是否读取了预期数量的值也是一种很好的做法。 In your case, the value was probably 0 because the literal characters were not matched in your input.在您的情况下,该值可能为 0,因为您的输入中的文字字符不匹配。 When a format specification isn't used, the value of the destination variable is unchanged (in this case undefined, as the array contains the bytes that happen to be on the stack where the variable is allocated.)当不使用格式规范时,目标变量的值不变(在这种情况下未定义,因为数组包含恰好位于分配变量的堆栈上的字节。)

Two problems:两个问题:

  • You did not initialise array properly.您没有正确初始化array It should be:它应该是:

    char array[40] = "";

  • You mixed up scanf and printf with this statement:您将 scanf 和 printf 与以下语句混合在一起:

    scanf("Enter your address: %39s",array);

    It should be broken into:它应该分为:

    printf( "Enter your address: " );

    scanf ("%39s", array);

From man page of scanf :scanf的手册页:

int scanf(const char *format, ...);

Reason for undefined output:未定义 output 的原因:

Each conversion specification in format begins with either the character '%' or the character sequence "%n$" format中的每个转换规范都以字符“%”或字符序列“%n$”开头

Solution : Change scanf("Enter your address: %39s",array);解决方法:更改scanf("Enter your address: %39s",array); to scanf("%39s",array);scanf("%39s",array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM