As a programmer with a decent background in Java and Pascal, C has certainly been a good challenge for me.
Background
At the dawn of my programming “career”, my ventures mainly entailed writing dumb scripts in Java. They did almost nothing, but were fun to play around with. Of course, I was anything but committed to my learning and did not pursue it any further than I could have.
Java
My main gripes with Java were that package management on NixOS was a pain. I found it incredibly irritating to install maven/Gradle and hope that the libraries would install properly. Of which they never did.
Some sample Java
import java.util.Scanner;
class program {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
String name = read.nextLine();
System.out.println("Hello " + name);
}
}
Pascal/Delphi
Then came Pascal, which was the first language I actually built full on projects with. I built a pretty small school project using it, which was a great learning experience, and the first project I put a lot of effort into. GoodBites .
A bit of Pascal.
// Using windows libraries
program Hello;
procedure Message(sMessage : String);
begin
ShowMessage(sString);
end;
var
sMessage,sName : String;
begin
sName := InputBox('Program','What is your name?','');
sMessage := 'Hello ' + sName;
Message(sMessage);
end.
What drew me into learning C was the fact that has been the base language for a plethora of programs, along with the Linux Kernel. The prospect of being able to create useful software like that enthralled me. I was deeply moved to start learning.
Introduction to C
At some point I realized that I actually needed to learn the language and use it if I was going to get any better at it. Hence, I picked up a book on C, The C Book . Then signed up for a course on CS50 and got to work.
The transition was pretty simple at first. C is not too different from Java at a
glance, parts of the syntax are similar such as arithmetic. I would argue that
the same for C++, although that assertion melts away when we start using vectors
or cout
and cin
.
Java and C
Initial differences I observed were minor.
Java
static void japan(){
int a = 0;
for(int i = 0; i > 10; i++){
a++;
if(i + a > 20){
System.out.println("Konbanwa! " + a);
}
}
}
C
#include <stdio.h>
static void japan(void){
int a = 0;
for(int i = 0; i > 10; i++){
a++;
if(i + a > 20){
printf("Konbanwa! %i",a);
}
}
}
Upon further inspection, the differences become obvious. C is a much “lower-level” language than Java. Java is best used for Android apps and high-level programs that do not need the speed C provides. C is much more tailored to environments where speed is important, like kernels and embedded systems. They are both great for day-to-day software though.
Java is actually built off C. Java, like other languages (Go, C#, C++), takes a lot of inspiration from C and that is why they are so similar in syntax.
Furthermore, C is a compiled language, compiled by a compiler following the
Standard, such as GCC
or clang
. Java, conversely is an interpreted language
and runs in a virtual machine
like the JVM (Java Virtual Machine).
One thing that I am taking time to become accustomed to is proper memory management in C. In my learning, I have come to realize that digging yourself into a deep access violation hole is trivial, but not so breezy to dig yourself out. I will go over this fully in an upcoming blog post.
Moving forward with C
As I have spent time working with C, I have noticed that there are a lot of my personal coding habits that I need to unlearn, and new ones that I should pick up. I find it exciting, the fact that I struggle shows me that there is a lot to learn. That motivated me to get into the nitty-gritty of programming and to break down software to learn how it works.