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
413 views
in Technique[技术] by (71.8m points)

c - printf insufficient arguments

My question is about the behavior after a printf with a missing argument:

printf("%s blah blah %d", int); // integer was given as argument (and not int written)

I already know that if there are insufficient arguments for the format, the behavior is undefined.

The question is whether it is undefined for the printf outcome or for the whole program?

  • A crash can happen when %s tried to read from unauthorized memory address. (it happened to me)
  • Can a crash happen [long] after the printf complete? (printed some garbage string and an integer)

Edit:

To clarify I'm not asking about compilation errors or warnings, nor can a program crash when executing this line. The question is can this line make the program crash randomly after this line already executed.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It is undefined for the whole program.

In fact it is undefined before even the program exists: it is undefined what the compiler itself does in presence of the text printf("%s blah blah %d", int);


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

...