Why is the C program giving weird output using Scanf?

我是研究僧i 提交于 2020-12-11 04:56:29

问题


I'm currently learning C programming and I have come across this weird 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.

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.


回答1:


Just use the format specifier correctly for the scanf() function and display the custom message using 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;
}



回答2:


You should look at the documentation of scanf again. 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.

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. In your case, the value was probably 0 because the literal characters were not matched in your input. 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.)




回答3:


Two problems:

  • You did not initialise array properly. It should be:

    char array[40] = "";

  • You mixed up scanf and printf with this statement:

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

    It should be broken into:

    printf( "Enter your address: " );

    scanf ("%39s", array);




回答4:


From man page of scanf:

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

Reason for undefined output:

Each conversion specification in format begins with either the character '%' or the character sequence "%n$"

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



来源:https://stackoverflow.com/questions/65072754/why-is-the-c-program-giving-weird-output-using-scanf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!