在C印花INT通过的printf%S [英] printing int in c by %s in printf

查看:188
本文介绍了在C印花INT通过的printf%S的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译的Borland


  

编译器不会将其标记为任何错误。



  • 我知道这是错的,

  • 但我想知道为什么它显示了公司的名称。


    1. 它不显示此消息,如果我输入任何值小于4,

    2. 它给出了不同价值观等不同的输出,尝试一些
      像值50,100,等等。

    为什么它的显示这种奇怪的输出。


以下是运行程序。

 无效的主要()
{  int类型的= 6;  的printf(%S,一);}


解决方案

您正在使用 INT 的数据类型错误的说明。其不确定的行为。什么事情都可能发生。

未定义行为:


  

任何事情都有可能发生;标准并没有规定要求。该程序可能无法编译,或者它可能会错误地执行(或者崩溃或产生默默不正确的结果),也可能偶然做程序员意图到底是什么。


The compiler doesn't mark it as any error.

  • I know that it's wrong,
  • but I want to know why it's showing the name of the company.

    1. And it doesn't show this message if I type any value less than 4,
    2. it gives different outputs for different such values, try some values like 50, 100,etc.

    Why it's showing such strange outputs.

Here is the program.

void main()
{

  int a=6;

  printf("%s",a);

}

解决方案

You are using wrong specifier for int data type. Its undefined behavior. Any thing could happen.

Undefined Behavior:

Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended.

这篇关于在C印花INT通过的printf%S的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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