Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

39 views
Skip to first unread message

Efraim Mostrom

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
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.
--
comp.lang.c.moderated - moderation address: cl...@plethora.net

Oleg Goldshmidt

unread,
Aug 14, 2000, 3:00:00 AM8/14/00
to
"Efraim Mostrom" <mostrom...@iname.com> writes:

> 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.

> char *buf[1024];

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

> 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.

--
Oleg Goldshmidt <ogo...@NOSPAM.netvision.net.il>
"... We work by wit, and not by witchcraft;
And wit depends on dilatory time." [Shakespeare]

Warren Dale

unread,
Aug 14, 2000, 3:00:00 AM8/14/00
to
On 11 Aug 2000 21:56:37 GMT, "Efraim Mostrom"
<mostrom...@iname.com> wrote:

>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.

>--
>comp.lang.c.moderated - moderation address: cl...@plethora.net

#include <stdarg.h>

Florian Große-Coosmann

unread,
Aug 14, 2000, 3:00:00 AM8/14/00
to
Efraim Mostrom wrote:
>
> 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

Efraim Mostrom

unread,
Aug 17, 2000, 3:00:00 AM8/17/00
to
Warren Dale <wd...@ozemail.com.au> skrev i diskussionsgruppsmeddelandet:clcm-2000...@plethora.net...
[ ... snip ... ]
>
> #include <stdarg.h>

Tnx, will try that!

/Efraim

0 new messages