C For Perl Programmers

barbie on 2005-04-21T09:49:58

The following piece of C code took much much longer to debug that I expected:

mysub( key, sprintf( "%s/%s/%s", x, y, z ) );

The compiler reported a syntax error before the ';'. If you've been doing C regularly recently, it will be an easy spot. If you're a now ex-C programmer, who hasn't been a C programmer for over 5 years and in that time has exclusively been a Perl programmer, will wonder why it doesn't JFDI ... like Perl does :)

The problem for non C programmers, is that Perl and C use sprintf in different ways. The correct syntax is the first comment reply.

It took me far too long to figure that one out. It was made all the more frustrating by the fact Ubuntu doesn't have 'lint', Synaptic couldn't find one and even Google drew a blank. I could find lint checkers for virtually every other language and things like DNS, but not for C. This was once one of the key tools in my programming arsnal, alongside vi, make and cc, and would have helped me solve the above problem in seconds, but now seems to have vanished from sight. I was expecting to find a GNU version, but have drawn a complete blank. Anyone know of a decent lint checker for C?


Answer

barbie on 2005-04-21T09:52:16

char *path;
sprintf( path, "%s/%s/%s", x, y, z );
mysub( key, path );

Re:Answer

mary.poppins on 2005-04-21T12:44:44

That's some pretty hazardous example code right there.

Re:Answer

barbie on 2005-04-22T10:05:34

Agreed. And it isn't complete code. The flaw I was highlighting was that the sprintf syntax is different between Perl and C.

Re:Answer

Matts on 2005-04-21T12:54:10

I really hope this isn't for work :-)

Re:Answer

barbie on 2005-04-22T10:08:16

It is, but it isn't the code I was using. It was my misunderstanding of sprintf syntax that I was highlighting :)

Re:Answer

merlyn on 2005-04-21T14:13:13

So, you want to poke some random part of your program with a m/d/y string? Whoo hoo! I think you wanted:
char path[1024];
sprintf ( path ... );
And yes, the fixed size buffer there is the root of all evilness of C programs.

Re:Answer

barbie on 2005-04-22T10:26:33

I'm in the UK, so it would have been a d/m/y string if that was the case ;) I only use x/y/z just as an example, that doesn't resemble the real string I was formatting. You are quite right about fixed size buffers, I was only using a pointer to indicate a string. Makes me realise how lucky we are programming Perl :)

And the complete version:

Aristotle on 2005-04-23T10:33:19

char *path;
if( asprintf( &path, "%s/%s/%s", x, y, z ) == -1 )
    errx( "Memory allocation error" );
mysub( key, path );
free( path );

That assumes you have errx and asprintf of course, but they’re reasonably portable.

Splint

Dom2 on 2005-04-21T11:05:06

Splint is one lint.

The gcc folks reckon that -Wall should be good enough though.

-Dom

Re:Splint

barbie on 2005-04-21T11:22:26

Neat. Thanks. I've not really used gcc actively (accept under the covers of library installs and make), so I guess I ought to look at what all the options mean. But Splint looks to be exactly what I was after, so I'll be giving that a try in future :)

Re:Splint

Dom2 on 2005-04-21T12:22:43

You'll probably go mad before you understand all of gcc's options. :-)

In practise, I just compile with "-g -pipe -Wall", and it complains about quite a lot. The "-pipe" probably isn't necessary these days, but I'm a creature of habit.

-Dom

ubuntu lint

tinman on 2005-04-21T17:15:08

As far as Ubuntu goes, you're absolutely right. The Ubuntu repo doesn't seem to have a copy.

The thing to do then (he said, confidently) is to add more repositories to your list. Namely, a few Debian repositories. Now, I probably should tell you that this step isn't recommended (and people on the forums actively advise against it). The reason is that it's easy to upgrade something "core" and bork the system.

splint doesn't fall into this category, however. I added Marillat's archive (http://ubuntuguide.org/#extrarepositories) and installed splint. Only splint, mind. Don't pay any attention to the other upgrade notifications :) I haven't programmed C since umm... 1998, but my installed copy of splint seemed to make all the right noises. The marillat repo also has a deb version of Acrobat Reader 7, if you are so inclined.

Re:ubuntu lint

barbie on 2005-04-22T12:01:43

I have a load of debian repositories (9 in all) and none of them had lint or splint. However, now that I've added your suggested repository, I found splint straight away. Thanks :)