Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

barbie (2653)

  reversethis-{ku. ... m} {ta} {eibrab}

Leader of [] and a CPAN author []. Co-organised YAPC::Europe in 2006 and the 2009 QA Hackathon, responsible for the YAPC Conference Surveys [] and the QA Hackathon [] websites. Also the current caretaker for the CPAN Testers websites and data stores.

If you really want to find out more, buy me a Guinness ;)

Memoirs of a Roadie []
CPAN Testers Reports []
YAPC Conference Surveys []
QA Hackathon []

Journal of barbie (2653)

Thursday April 21, 2005
04:49 AM

C For Perl Programmers

[ #24304 ]
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?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • char *path;
    sprintf( path, "%s/%s/%s", x, y, z );
    mysub( key, path );
    • That's some pretty hazardous example code right there.
      • Agreed. And it isn't complete code. The flaw I was highlighting was that the sprintf syntax is different between Perl and C.
    • I really hope this isn't for work :-)
      • It is, but it isn't the code I was using. It was my misunderstanding of sprintf syntax that I was highlighting :)
    • 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.
      • Randal L. Schwartz
      • Stonehenge
      • 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 :)
    • 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 [] is one lint.

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


    • 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 :)
      • 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.


  • 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.or []

    • 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 :)