int snprintf(char *restrict buf, size_t n, const char * restrict format, ...); 函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0。所以如果目标串的大小为n的话,将不会溢出。 函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值。
1 2 3 4 5 6 7 8 9
char s1[6] = "12345"; char s2[6] = "67890"; int res; res = snprintf(s1,sizeof(s1),"abcdefg"); //写入长度大于原来长度,写入abcde和\0,要求6 printf("s1:%s, res:%d\n",s1,res); //欲写入长度7,这个值是strlen,不包含\0 res = snprintf(s1,sizeof(s1),"abc"); //写入长度少于原来长度,则相当于替换 printf("s1:%s, res:%d\n",s1,res); res = snprintf(s2,4,"%s","abcdefg"); //指定4,包含了\0 printf("s2:%s, res:%d\n",s2,res);
Point p(1,4); //改变 p p[0] = 7; p[1] = 9; Point p1 = -p; Point p2(4,12); Point p3 = p - p2; if(p2==p3) cout<<"equal"<<endl; else cout<<"not equal"<<endl; Point p4 = ++p3; Point p5 = --p3;
// 就是 d_func()->init(); voidQCoreApplicationPrivate::init() { ...... Q_ASSERT_X(!QCoreApplication::self, "QCoreApplication", "there should be only one application object"); QCoreApplication::self = q; ...... }