Showing posts with label C-programming. Show all posts
Showing posts with label C-programming. Show all posts

Tuesday, September 20, 2016

Calling a simple C function from D - strcmp

By Vasudev Ram


D => C

It's quite useful to be able to communicate between modules of code written in different languages, or, in other words, to be able to call code in one language from code in another language. Some of the reasons why it is useful, are:

- some functions that you need may already be written in different languages, so it may be quicker to be able to call cross-language from function f1 in language L1 to function f2 in language L2, than to write both f1 and f2 in L1 (or in L2) from scratch;

- the function f2 that you need may not be as easy to write in language L1 as it is in language L2 (or you don't have people who can do it in L1 right now);

- f2 may run faster when written in L2 than in L1;

- etc.

I was looking into how to call C code from D code. One source of information is this page on the D site (from the section on the language spec):

Interfacing to C

Here is an example of calling a simple C function, strcmp from the standard C library, from a D program, adapted from information on that page:
/*
File: call_strcmp.d
Purpose: Show how to call a simple C string function like strcmp from D.
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
*/

extern (C) int strcmp(const char* s, const char* t);

import std.stdio;
import std.string;

int use_strcmp(char[] s)
{
    // Use toStringz to convert s to a C-style NULL-terminated string.
    return strcmp(std.string.toStringz(s), "mango");
}

void main(string[] args)
{
    foreach(s; ["apple", "mango", "pear"])
    {
        // Use dup to convert the immutable string s to a char[] so
        // it can be passed to use_strcmp.
        writeln("Compare \"", s.dup, "\" to \"mango\": ", use_strcmp(s.dup));
    }
}
I compiled it with DMD (the Digital Mars D compiler):
$ dmd call_strcmp.d
and ran it:
$ call_strcmp
Compare "apple" to "mango": -1
Compare "mango" to "mango": 0
Compare "pear" to "mango": 1
The output shows that the C function strcmp does get called from the D program, and gives the right results, -1, 0, and 1, for the cases where the first argument was less than, equal to, or greater than the second argument (in sort order), respectively.

Of course, not all kinds of calls from D to C are going to be as easy as this (see the Interfacing reference linked above), but its nice that the easy things are easy (as the Perl folks say :).

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes



Tuesday, October 2, 2012

Brian Kernighan's home page w/ his articles, books and source code


Brian Kernighan is still one of my favorite computer authors, even after many years.

He is of course very well-known to a lot of software people, but I'm writing this post because there will still be people (particularly people new or recent to software) who don't know about him and his work, and also because of the useful links to his books, below (some of them with downloadable source code).

He is the co-author of the classic books "The C Programming Language" by Kernighan and Ritchie (well-known as just "K&R") and "The UNIX Programming Environment" by Kernighan and Pike. I read both of those books years ago near the beginning of my programming career, and even today I find there are very few books of that caliber - concise yet dense with information, clear writing, etc.

He was one of the early people to work on UNIX and C at Bell Labs, and had made a lot of contributions to many areas.

The AWK programming language is named partly after him - the K is for Kernighan (*).

Brian Kernighan's home page at Princeton University. He is currently a professor there after working at Bell Labs for many years.

Wikipedia page on Brian Kernighan.

Brian Kernighan page at Bell Labs.

(*) And D is for Digital :), the name of a new introductory book on computers (hardware, software and communications) by Kernighan. I just saw it. It looks like a good present for the non-technical person(s) in your life.

There are some interesting articles at Kernighan's pages at Princeton University and Bell Labs (two of the links above).

Also, the pages about his books have links to downloadable source code for some of the programs in those books. See the links below:

The Practice of Programming.

The C Programming Language.

The Unix Programming Environment.

The AWK Programming Language.

Inspired by nature.
- dancingbison.com | @vasudevram | jugad2.blogspot.com

Friday, July 27, 2012

The C Book - free book on the C language

By Vasudev Ram


Saw this via a Zed Shaw site.

The C Book

It's a free book to learn C programming. There is both an online HTML version and a downloadable PDF version. Scanned initial parts of the book briefly, it seems good, though it is a bit dated - won't cover the latest additions to the C standard.

Yes, I know C is old, and there are tons of other resources already about it, but it's still a great language and I'll always like it (it was one of the first languages I learnt, and I used it a lot, and owed my living a good amount to it), and there are still lots of people who will still be new to C and want to learn it, so I'm blogging about it. So there :)

- Vasudev Ram - Dancing Bison Enterprises