C development on Linux – Comparison between C and other programming languages – II.

Introduction

Since in our first part of this article we said we expected you, the reader, to have some programming knowledge, in this part we want to help you get a on where C stands in comparison to other programming languages you might know. The choice of those languages was pretty tough because of various criteria, but in the end we stopped at C++, Perl and Python. Since programming languages can be classified in lots of ways (depending on paradigm, syntax or style, for example), we didn’t try to find languages that are in the same category as C. Instead, since the aforementioned languages are pretty popular in the Linux world, we chose them, mentioning what every language’s place is in the great scheme, what it’s generally used for and of course, the differences between them and C. This article will be structured as follows: we’ll start with important elements of every language, like variable declaration, typing or structure and compare that with how it’s done with C. Thusly we hope to give you an idea about the language before we start. The parts this article is made of will be exactly like the announced structure of this article, to make comprehension easier.

The comparison

Types, operators, variables

C++

C++ was initially named “C with classes”, which says a lot about its’ relation with C. It is widely seen as a superset of C ( thus C++ the unary increment operator ++ ) , introducing object-oriented programming features. Types are essentially used the same, with bool being introduced in C++ for boolean operations. Basically, when talking about C and C++ differences, most of those come from the OOP concepts C++ has and C does not. For example C++ has operator overloading, an OOP-specific term, which means that an operator may have different implementations depending on the data it’s operating on. For example, in C++ you can do this:

 
a << 2

Now, if a is an integer, this will do a bitwise operation on it (left shift by 2), but if a is an output stream, the above line will try to write a ‘2’ to it. This kind of behavior is one of the reason C++ is criticized for allowing poor programming practices. Variables and constants are declared the same way as in C.

Perl

With Perl it’s simpler: it only has three data types. These are scalars, arrays and hashes or associative arrays. Scalars are prefixed with a dollar sign, arrays are prefixed with a @ and hashes are prefixed with %s. The difference regarding types is that Perl is a dynamic programming language, while C is weak-typed, meaning that in C you will deal with proper declarations of variables, like “this variable is an integer” or “this other variable is a pointer to a float”. The differences regarding arithmetic operators are just that Perl has ‘**’ for exponentiation, while C needs a function to achieve that.

Python

Python’s variable declaration is context-based. For example, we want to declare a string, an integer and a float:

var1 = "String"
var2 = 100
var3 = 100.98

With C, this would have been like

char* var1 = "String";
int var2 = 100;
float var3 = 100.98;

C doesn’t have a string type as other languages do, so a string must be declared as an array of characters or a pointer to a char, while Python offers numbers, dictionaries, strings, lists and tuples as data types.

Flow control

All four languages described here have many things in common when it comes to flow control. For example, when starting an if block, none requires an ending keyword, like fi in Bash or Algol68. Speaking of if blocks, the syntax is similar, so if you used to program in C++, Perl or Python you’ll find C’s way very familiar. The same can be said about the rest of the flow control operations: for, do, while, switch/case or break/continue. Again, if you’re accustomed to the way the three languages do flow control, you’ll need a few minutes to get used to C. Some main differences could be: Perl has more keywords to deal with this than C, like redo, last or next. Python has the pass keyword for…doing nothing basically, but it’s useful when one needs an empty class. For example:

class Empty:
     pass

Functions

A simple function declaration in C would be like this:

int sum (int a, int b)
    {
        return a + b;
    }

This would be a function named sum that returns an integer from calculating the sum of its’ arguments, a and b, also integers. Note the blocks that make a clear delimitation as to where the function’s body starts and when it ends. In Perl the term function is used interchangeably with subroutine. You can, as in C, first declare a function, then define it, or declare it and define it in one shot, like we did above. You have to use the sub keyword in order to tell the Perl interpreter what you want to do. In Python our sum example would look like this:

def sum ( a, b ):
  return a + b

You perhaps noticed that we said nothing about C++. Well, as far as we’re concerned, there is no difference between C and C++ in this respect.

Pointers and arrays

A pointer is a variable that holds the memory location of another variable. As in C, pointers and arrays are tied together pretty tight and offer many advanced features to the C++ programmer. Again, there is no important difference between the two. Perl has references that offer the same functionality. For example, this piece of code defines a variable named var and a reference to it named refvar:

$var = 20;
$refvar = \$var;

In C, we would have done something like

int var;
var = 20;
int *refvar;
refvar = &var;

Perl, like Python, doesn’t allow the programmer to do direct memory manipulation. Some see this as a good thing, others not. Python offers ctypes, which is a library that offers the pointer() function for using pointers. The short story is: Python doesn’t use pointers. The long story is that Python’s way to reference variables, that is by-value only, is different to a programmer used to C.

If you used arrays on C++ or Perl, the concept is mostly the same. Perl has a different syntax, but if you already know what an array is, you’ll be alright in C. Python offers the array module that offers this functionality, because it already has lists as a basic type, except arrays are more restrictive.

Structures

In C, a struct is a record that contains a fixed, labelled set of objects, all wrapped into one. For example:

struct customer {
   int account;
   char *name;
   float balance;
};

Quoting Wikipedia, “In C++, a structure is a class defined with the struct keyword. Its members and base classes are public by default. A class defined with the class keyword has private members and base classes by default. This is the only difference between structs and classes in C++.”. In Perl until version 5, structs were a problem (or lack thereof), but now there is perldsc (Perl Data Structures Cookbook), available at perldoc.org. In Python you can always (well, almost) use tuples or dictionaries for this.

Including files

As you have seen in the first part, in C one uses preprocessor directives to include definitions from other files. This functionality is offered also by C++, with the same syntax, except header files are named $name.hpp and that you don’t need the file extension:

#include <iostream>

iostream is the C++ equivalent of stdio.h. In Perl, we have the use keyword for this, and can be used like this:

use Module;

In Python, the equivalent of use is import, used in exactly the same way as in Perl, except it doesn’t require a semicolon at the end.

Other considerations

Many of you have noticed that all languages chosen for our comparison are object-oriented, while C is not. This is not an unfair comparison, because if we would have chosen Fortran or Prolog as our terms of comparison, chances are, since many programmers today don’t use these languages, that our article wouldn’t have been very popular. On the other hand, we thought of this as a way to get your feet wet, because habit is part of the human nature and if you ever used one or more of these languages, it will be easier on you when you start learning C. So this part of the article is intended as a helping hand and if we managed to do that, that it’s ok.

C and C++

Some of you may wonder: if C++ is so similar to C, but it’s more complex and offers more, why bother with C? This has been a topic discussed more than it should have been, and we will try to give an answer of our own. First of all, C is simpler. K&R has 266 pages, while “The C++ programming language” by the creator, Bjarne Stroustrup, has 1090. ‘Nuff said. Second, existing code. Regardless of hardware architecture, operating system or purpose, C is widespread and then some. From OS kernels to GUI libraries, C is there and has no intent to leave anywhere. That isn’t to say C++ isn’t used by many developers. We’re just saying that sooner or later you’ll find yourself having to work with C code or use a language influenced by it, so C knowledge is always good-looking on a CV. Third, if you want to learn C and C++, you’re better off starting with C, because of its’ aforementioned simplicity and because once you’ll have the basics, C++ will look easier to learn. Finally, it’s all about the right tool for the job. If you need fast code, the ability to go low-level and a simple language, go with C.

Conclusion

After this second part of this article, we will start learning C programming on Linux, as we think that too many introductory parts make no sense. We will but glad to help you there.

Here is what you can expect next:



Comments and Discussions
Linux Forum