snprintf函数的使用(Windows和Linux下的区别)[转] - Something TO DO


Something TO DO posted @ 2017年10月15日 21:58 in C++ with tags _snprintf snprintf , 2386 阅读




#include <stdio.h>

#define COPY_LENGTH 5

int main() {
    char buff[100] = {0};
    int result = snprintf(buff, COPY_LENGTH, "ABCDEF");
    printf("%d, %s\n", result, buff);

我在Windows环境编译执行(我的编译环境是:Win7x64 ,gcc 4.8.3),执行结果为-1, ABCDE。
然后把这段代码放到Ubuntu里编译执行(我的编译环境是:Ubuntu Desktop 10.04,gcc 4.8.2),执行结果为6, ABCD。

在网上找了一下原因,在Linux C语言里其定义应该如下:

int snprintf(char *restrict buf, size_t n, const char * restrict   format, ...);

函数说明:最多从源串中拷贝 n - 1 个字符到目标串中,然后再在后面加一个 0 。所以如果目标串的大小为 n 的话,将不会溢出。


int _snprintf(char *buffer, size_t count, const char *format [, argument] ...);


所以说,_snprintf函数最多可以比源串中拷贝 n 个字符到目标串中,有可能不会在后面加0。所以会出现上面的问题。如果想要使两者相同,可以在代码中直接把最后一位设置成0,这样两个平台就是一样的结果了。当然在实际应用中,可能根据要实现的功能,如何设置要自己判断。


#include <stdio.h>

#define COPY_LENGTH 5

int main() {
    char buff[100] = {0};
    snprintf(buff, COPY_LENGTH, "ABCDE");
    buff[COPY_LENGTH - 1] = 0;
    printf("%s\n", buff);
How to Disable Lapto 说:
2022年8月10日 01:14

It happens many times that you want to use your external keyboard but struggle to have the enabled keys of your laptop. Unlike in desktop it is not a physical cord that you can unplug in order to disable the keyboard. How to Disable Laptop Keyboard Another way to disable the cordless keyboard by toggle off the Bluetooth on computer to disconnect the keyboard. Where as in laptop the keyboard comes as in-built with the device. In any circumstances you’re free to go ahead and disable the Laptop Keyboard and start using your external one.

CGBSE Question Paper 说:
2022年9月05日 19:51

CG Board Model Paper 2023 Class 10 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. CGBSE Question Paper Class 11 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

Archive Amazon Order 说:
2022年12月20日 21:33

There is no specific button or option on Amazon that allows you to totally remove purchases from your order history. However, you may archive an order to prevent it from appearing in your order history. Archive Amazon Order History This simple guide will explain how to delete Amazon purchases order history on Android, iPhone, and PC in order to erase or remove them.

2nd Inter Question P 说:
2023年2月14日 23:32

Board of Secondary Education has deferred the Class 12th board Exam should be held from May 2024. The candidates can check the Class 12th Board Model Question Papers from true site. 2nd Inter Question Paper 2024 The 12th Question Paper 2024 will likewise be reported on class 12th students can take note of the total 12th Question Paper 2024.

登录 *

loading captcha image...
or Ctrl+Enter
Host by | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee