#include <iostream>
#include <cstdio> // 包含的头文件 using namespace std; int main(int argc, char **argv)
{
int theTargetShapeForEncoding = 7;
int theParaLocationInformation = 5; char aCharFormat[2]; // 注意声明变量的大小,最好不要太小,如果为 1 的话, 那么是会出错的。 详细的请参考下面的函数解释。
//char aCharFormat[1]; // 如果声明为 1 , 那么后面的 std::cout ... 输出是空。
snprintf(aCharFormat, sizeof(aCharFormat), "%x", theTargetShapeForEncoding);
std::cout << "XXXX aCharFormat = " << aCharFormat << std::endl; snprintf(aCharFormat, sizeof(aCharFormat), "%x", theParaLocationInformation);
std::cout << "XXXX aCharFormat = " << aCharFormat << std::endl; return 0;
}

头文件: #include <cstdio>

snprintf 函数原型:  int snprintf(char *str, size_t size, const char *format, ...)

详解:

(1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('\0');
(2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入的字符串长度。

snprintf 使用注意的更多相关文章

  1. Linux C 字符串函数 sprintf()、snprintf() 详解

    一.sprintf() 函数详解 在将各种类 型的数据构造成字符串时,sprintf 的强大功能很少会让你失望. 由于 sprintf 跟 printf 在用法上几乎一样,只是打印的目的地不同而已,前 ...

  2. [C/C++基础] C语言常用函数sprintf和snprintf的使用方法

    Sprintf 函数声明:int sprintf(char *buffer, const char *format [, argument1, argument2, …]) 用途:将一段数据写入以地址 ...

  3. snprintf/strncpy/strlcpy速度测试

    速度测试代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <un ...

  4. 关于snprintf的一个warning

    在编一段代码时用到snprintf,有个很奇怪的warning 编译提示: warning C4013: 'snprintf' undefined; assuming extern returning ...

  5. snprintf 返回值

    在平时写代码的过程中,我一个推荐带有n系列的字符串函数,如 strcat ->strncat sprintf->snprintf 我们有类似的一个函数 void dump_kid(std: ...

  6. printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换函数

    参考:http://blog.sina.com.cn/s/blog_674b5aae0100prv3.html 总览 (SYNOPSIS) #include <stdio.h> int p ...

  7. C语言snprintf函数

    int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...); 函数说明:最多从源串中拷贝n-1个字符到 ...

  8. 转:sprintf与snprintf

    sprintf与snprintf   int sprintf( char *buffer, const char *format [, argument] ... ); 除了前两个参数类型固定外,后面 ...

  9. linux下snprintf和sprinf很少被提及的区别

    函数原型:int snprintf(char *dest, size_t size, const char *fmt, ...);函数说明: snprintf函数中的第二个参数,size的解释:siz ...

随机推荐

  1. 生成 PDF 全攻略【2】在已有PDF上添加内容

    项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...

  2. C 语言学习 第五次作业总结

    第五次作业,主要学习和复习的是几种循环结构的使用. 在前一次的课堂上,同学们已经学习了分支语句的使用.分支语句和循环语句配合使用,就可以写出更多的,逻辑功能丰富的代码了. 逻辑功能的丰富,也意味着学习 ...

  3. 使用对话框 —— Dialog

      对话框就是一般的弹出窗口,主要用来提示用户,和用户交互.   创建Activity对话框 使用Activity模拟对话框.这个比较简单,主要是使用Activity自带的Dialog主题.   创建 ...

  4. C#线程池使用

    bool stop; private void button3_Click(object sender, EventArgs e) { if(button3.Text=="线程池" ...

  5. 动态获取div的高度 随着窗口变化而变化

    // 1.jq法 <script> var winHeight = $(window).height(); $("#show").css("height&qu ...

  6. Local gulp not found in.. on windows

    当出现报错时,请按如下方式安装 gulp 以下使用国内的淘宝镜像安装: $ # Step 1 $ cnpm install -g gulp $ # Step 2 $ cnpm install --sa ...

  7. 6.初探Asynctask

    AsyncTask定义了三种泛型类型 Params,Progress和Result. Params 启动任务执行的输入参数,比如HTTP请求的URL.Progress 后台任务执行的百分比.Resul ...

  8. 【poj1737】 Connected Graph

    http://poj.org/problem?id=1737 (题目链接) 题意 求n个节点的无向连通图的方案数,不取模w(゚Д゚)w Solution 刚开始想了个第二类斯特林数,然而并不知道怎么求 ...

  9. Mysql 查询练习

    Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...

  10. SmohanTimeLine.js 酷炫的时间轴效果

    展示地址 点此下载 原文出处 一.参数说明 item : '.item', //项目元素 top : 30, //与下一行的间距 pointWidth : 22, //时间点宽度 cornerWidt ...