本文介绍了为什么这个code工作用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;诠释主(){字符数组[2];
阵列[0] =Q;
阵列[1] ='A';
的printf(%S,数组);返回0;
}

如果你问我这个code不应该工作。 printf的打印阵列[2]就像字符串,但它不是一个字符串。当我执行它,它完美的作品。你能解释一下为什么?


解决方案

You just got (un)lucky: your code exhibits undefined behavior, because it lets the printf's %s parameter run off the end of the sequence of characters that is not null-terminated.

A string in C is a sequence of char, which must have an extra character with the value 0, called the null terminator. Here is a way to make your code work without undefined behavior:

char array[3];
array[0] = 'q';
array[1] = 'a';
array[2] = '\0';

这篇关于为什么这个code工作用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:33
查看更多