views:

2222

answers:

4

Compiling this lines

    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);

I read two warning on snprintf line:

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

Why? The type is specified (long int sz, and %l in snprintf) and the argument in snprintf is only one. Can anybody help me? Thanks.

+7  A: 

Your format lacks type, because l is a "sizeof" modifier. Should be %ld

Dmitry Khalatov
That's right! Thank you really much :D
Emilio
A: 

See this list of printf format specifiers

It's comment for %l is:

The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

Rob Walker
+1  A: 

boost::lexical_cast<string>(sz) is much nicer, anyway.

Matt Cruikshank
A: 

Hi,

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

It does not require the length of "str", as the second argument. The name of the string pointer/ array name is enough.

sprintf() is quite unsafe, as it is not bounds checked. You are better off using an alternative, if available, to avoid buffer overruns.
Gorpik