ANSI-C/GCC/LCLINT snprintf() ?? 
Author Message
 ANSI-C/GCC/LCLINT snprintf() ??

Hi!

Short code for exemple:

#include <stdio.h>
int main(int * argc, char * argv[]) {
    char *buf[1024];
    if ((snprintf(buf,1024,"string : %s string2 : %s",argv[1],argv[2])) == -1) {
            fprintf(stderr,"main : error while snprintf() \n");
            exit(1);
    }
    fprintf(stdout,"buf = %s\n",buf);
    return 0;

Quote:
}

gcc -Wall -ansi -pedantic -O -g main.c -o proggy

Errors from gcc: snprintf() implicitly defined.

I then add to the source:

extern int snprintf(char *, int, const char *, ...);

I compile it again with same flags, and gcc takes it.

But when I in LINUX RH6.1 run lclint I get a parse error with:

extern int snprintf(char *, int, const char *, ...);

I know that snprintf() is not ANSI, but when I
#define _BSD_SOURCE it gets through gcc without problem,
but lclint still talks about undefined function.

I do not want to use #define _BSD_SOURCE or #define _GNU_SOURCE
I just want to explicitly declare:

extern int snprintf(char *, int, const char *, ...);

Is lclint buggy or am I missing something here?

Very, very thankfull for help. By defining the non ANSI functions myself
I can keep track of them if I would like to port the program.

Thanks in advance,

/Efraim

--
Remove nospam from my adress to reply.
--



Tue, 28 Jan 2003 03:00:00 GMT  
 ANSI-C/GCC/LCLINT snprintf() ??

Quote:

> Hi!

> Short code for exemple:

> #include <stdio.h>
> int main(int * argc, char * argv[]) {

How come gcc does not complain about the type of argc?  It should be
int.

Quote:
>     char *buf[1024];

Should be "char buf[1024]", I suppose

Quote:
> extern int snprintf(char *, int, const char *, ...);

The second argument to snprintf() is of type size_t, not int.

Any self-respectiung lint should complain that argc is not used in
your program.

Does it help if you fix those things?

By the way, just -Wall -pedantic -ansi might not be strict enough
(-Wall is a misnomer). You might want to use

    -W \
    -pedantic \
    -Wall \
    -Wtraditional \
    -Wshadow \
    -Wid-clash-32 \
    -Wpointer-arith \
    -Wcast-qual \
    -Wcast-align \
    -Wconversion \
    -Wstrict-prototypes \
    -Wmissing-prototypes \
    -Wmissing-declarations \
    -Wnested-externs \

to get more warnings and an approximation to a lint with gcc.

--

"... We work by wit, and not by witchcraft;
 And wit depends on dilatory time." [Shakespeare]
--



Fri, 31 Jan 2003 03:00:00 GMT  
 ANSI-C/GCC/LCLINT snprintf() ??
On 11 Aug 2000 21:56:37 GMT, "Efraim Mostrom"

Quote:

>Hi!

>Short code for exemple:

>#include <stdio.h>
>int main(int * argc, char * argv[]) {
>    char *buf[1024];
>    if ((snprintf(buf,1024,"string : %s string2 : %s",argv[1],argv[2])) == -1) {
>            fprintf(stderr,"main : error while snprintf() \n");
>            exit(1);
>    }
>    fprintf(stdout,"buf = %s\n",buf);
>    return 0;
>}

>gcc -Wall -ansi -pedantic -O -g main.c -o proggy

>Errors from gcc: snprintf() implicitly defined.

>I then add to the source:

>extern int snprintf(char *, int, const char *, ...);

>I compile it again with same flags, and gcc takes it.

>But when I in LINUX RH6.1 run lclint I get a parse error with:

>extern int snprintf(char *, int, const char *, ...);

>I know that snprintf() is not ANSI, but when I
>#define _BSD_SOURCE it gets through gcc without problem,
>but lclint still talks about undefined function.

>I do not want to use #define _BSD_SOURCE or #define _GNU_SOURCE
>I just want to explicitly declare:

>extern int snprintf(char *, int, const char *, ...);

>Is lclint buggy or am I missing something here?

>Very, very thankfull for help. By defining the non ANSI functions myself
>I can keep track of them if I would like to port the program.

>Thanks in advance,

>/Efraim

>--
>Remove nospam from my adress to reply.
>--


#include <stdarg.h>
--



Fri, 31 Jan 2003 03:00:00 GMT  
 ANSI-C/GCC/LCLINT snprintf() ??
Quote:

> Hi!

> Short code for exemple:

> #include <stdio.h>
> int main(int * argc, char * argv[]) {
>     char *buf[1024];
>     if ((snprintf(buf,1024,"string : %s string2 : %s",argv[1],argv[2])) == -1) {
>             fprintf(stderr,"main : error while snprintf() \n");
>             exit(1);
>     }
>     fprintf(stdout,"buf = %s\n",buf);
>     return 0;
> }

Is this a typo while building this example?
You wrote "char *buf[1024]" in case of "char buf[1024]". snprintf is defined
at least in glibc 2.0 but returns correct values only in version 2.1+.
The return of the error value -1 should work in all cases.

Florian
--



Fri, 31 Jan 2003 03:00:00 GMT  
 ANSI-C/GCC/LCLINT snprintf() ??

[ ... snip ... ]

Quote:

> #include <stdarg.h>

Tnx, will try that!

/Efraim
--



Mon, 03 Feb 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. snprintf() and -ansi switch

2. Newbie: separate big .cs file into small .cs files

3. ANSI standard C -- who's right Borland or gcc

4. enforcing ansi-ness on gcc

5. gcc != ansi c?

6. Ansi Compatibility with GCC

7. : GCC + Sun = not ANSI compliant

8. ANSI C function atexit and gcc

9. ANSI gcc valid on SunOS4.1.3?

10. C compiler -- cc/gcc -ansi/lint/CC ?

11. void main (Re: gcc is not an ansi compiler)

12. Gcc Is Not An Ansi Co

 

 
Powered by phpBB® Forum Software