Hello World

This is a demo page to test the features of this blog. Don’t expect much here.

In the software world, everything begins with Hello World. This is the beginning of my blog. Let’s see how this will go. This page stands to test the features of this blog.

This website has been completely designed using GNU Emacs. The static web pages are written in Emacs org-mode and exported to HTML. If you are not yet a GNU Emacs user, give it a try. It’s worth learning. If you are familiar with vim, see Spacemacs. You shall feel home.

To begin with, let’s see how code blocks in different language be handled. This should be pretty smooth, and cool .

Hello World

How about showing some real content? Have a look at a few examples below.

C Programming

Let’s begin with Hello World in C Programming.

#include <stdio.h>

int
main (void)
{
  printf ("Hello World!\n");

  /* Implicit in c99+ */
  return 0;
}

Save the above code into hello.c, then compile and run.

# Compile and run
gcc hello.c -o hello && ./hello

# An alternate way for the same.  If you are
# wondering, no, you don't need any Makefile.
make hello && ./hello

Emacs lisp

Now let’s try this in elisp. The code below will print “Hello World!” to the echo area. To run the program, write the code in GNU Emacs, put the cursor at end of the paren and press C-x C-e .

(message "Hello World!")
;; Put the cursor here  ^

GUI using GTK and C

The following example shows a window with a “Hello World!” label. The code will work with GTK3.

/*
 * Hello world with GTK3 in C
 *
 * By Mohammed Sadiq <sadiq [at] sadiqpk [d0t] org>
 * Date: 2020 May 10
 * License: None, Public domain (CC0)
 *
 * Compile:
 *   gcc hello.c $(pkg-config --cflags --libs gtk+-3.0) -o hello
 *
 * Run:
 *   ./hello
 */

#include <gtk/gtk.h>

static void
activate_cb (GtkApplication *app,
             gpointer        user_data)
{
  GtkWindow *window;
  GtkWidget *label;

  window = GTK_WINDOW (gtk_application_window_new (app));
  gtk_window_set_title (window, "Hello World!");
  gtk_window_set_default_size (window, 400, 300);

  label = gtk_label_new ("Hello World!");
  gtk_container_add (GTK_CONTAINER (window), label);
  gtk_widget_show (label);

  gtk_window_present (window);
}

int
main (int    argc,
      char **argv)
{
  g_autoptr(GtkApplication) app = NULL;

  app = gtk_application_new ("org.sadiqpk.example", 0);
  g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);

  return g_application_run (G_APPLICATION (app), argc, argv);
}

The same in GTK4:

/*
 * Hello world with GTK4 in C
 *
 * By Mohammed Sadiq <sadiq [at] sadiqpk [d0t] org>
 * Date: 2020 May 10
 * License: None, Public domain (CC0)
 *
 * Compile:
 *   gcc hello.c $(pkg-config --cflags --libs gtk4) -o hello
 *
 * Run:
 *   ./hello
 */

#include <gtk/gtk.h>

static void
activate_cb (GtkApplication *app,
             gpointer        user_data)
{
  GtkWindow *window;
  GtkWidget *label;

  window = GTK_WINDOW (gtk_application_window_new (app));
  gtk_window_set_title (window, "Hello World!");
  gtk_window_set_default_size (window, 400, 300);

  label = gtk_label_new ("Hello World!");
  gtk_window_set_child (window, label);

  gtk_window_present (window);
}

int
main (int    argc,
      char **argv)
{
  g_autoptr(GtkApplication) app = NULL;

  app = gtk_application_new ("org.sadiqpk.example", 0);
  g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);

  return g_application_run (G_APPLICATION (app), argc, argv);
}

The above code is a simple example that works with both GTK3 and GTK4. If you need a feature rich template to develop GTK applications, see My GTemplate project.