Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

string - printing int in c by %s in printf

Compiled in Borland C++ compiler

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);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your program behaviour is undefined since %s is not a valid format specifier for an int.

Exactly why it gives you the values it does is down to conjecture. The compiler also reserves the right to eat your cat. And your dog given that void main() is not compliant with later C standards.

C intentionally does not guard against your doing this as that would compromise the performance of the language. It's difficult (although not impossible as C is statically typed) for a compiler to warn you of this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...