在C ++中使用带有sprintf()的格式字符串的警告

时间:2008-12-03 19:18:31

标签: c++ types format printf

编译此行

    long int sz;
    char tmpret[128];

    //take substring of c, translate in c string, convert to int, 
    //and multiply with 1024
    sz=atoi(c.substr(0,pos).c_str())*1024;

    snprintf(tmpret,128,"%l",sz); 

我在snprintf专栏上读了两个警告:

 warning: conversion lacks type at end of format
 warning: too many arguments for format

为什么呢?指定了类型(long int sz和snprintf中的%l),snprintf中的参数只有一个。有谁能够帮我?感谢。

4 个答案:

答案 0 :(得分:8)

您的格式缺少类型,因为l是“sizeof”修饰符。应该是%ld

答案 1 :(得分:1)

无论如何,

boost::lexical_cast<string>(sz)要好得多。

答案 2 :(得分:0)

请参阅此printf format specifiers

列表

%l的评论是:

  

该论点被解释为长篇   整数的int或unsigned long int   说明符(i,d,o,u,x和X),和   作为一个广泛的角色或广泛的角色   用于说明符c和s的字符串。

答案 3 :(得分:-1)

int sprintf(char * str,const char * format,...);

它不需要长度为“str”,作为第二个参数。  字符串指针/数组名称的名称就足够了。

相关问题