#include <stdio.h>main(){ char buf[10]; sprintf(buf, "The length of the string is more than 10"); printf("%s", buf);}编译并运行,屏幕上输出”The length of the string is more than 10“,同时系统提示程序已经停止。原因就是要写入的字符串的长度超过了buf的长度,造成缓冲区溢出。使用snprintf()来代替sprintf()将能够很好的解决这个问题。【实例】打印字母a的ASCII值。#include <stdio.h>main(){ char a = 'a'; char buf[80]; sprintf(buf, "The ASCII code of a is %d.", a); printf("%s", buf);}运行结果:The ASCII code of a is 97.又如,产生10个100以内的随机数并输出。#include<stdio.h>#include<stdlib.h>#include<time.h>int main(void){ char str[100]; int offset =0; int i=0; srand(time(0)); // *随机种子 for(i = 0;i<10;i++) { offset+=sprintf(str+offset,"%d,",rand()%100); // 格式化的数据写入字符串 } str[offset-1]='/n'; printf(str); return 0;}运行结果:74,43,95,95,44,90,70,23,66,84例子使用了一个新函数srand(),它能产生随机数。例子中最复杂的部分是for循环中每次调用函数sprintf()往字符数组写数据的时候,str+foffset为每次写入数据的开始地址,最终的结果是所有产生的随机数据都被以整数的形式存入数组中。
新闻热点
疑难解答