为什么 C 程序使用 Scanf 给出奇怪的输出? [英] Why is the C program giving weird output using Scanf?

查看:59
本文介绍了为什么 C 程序使用 Scanf 给出奇怪的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 C 编程,我遇到了这个奇怪的输出.

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.

推荐答案

只需为 scanf() 函数正确使用格式说明符,并使用 printf() 显示自定义消息代码>.

Just use the format specifier correctly for the scanf() function and display the custom message using printf().

看看下面的代码:

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

这篇关于为什么 C 程序使用 Scanf 给出奇怪的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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